Move setting job state to event handler in Task

There are now several places in the code that could mark a task as
failed or done.  Since the state column for Job is a sort of
denormalization anyway (it could be calculated from a count of certain
values in the tasks table), it makes sense to be the code to keep it up
to date in an event handler.  An SQL-Trigger would be better, but
SQL-Alchemy does not seem to support this in a cross-platform compatible
way.