Use transacation to wrap this up more safely.

Also reverse the logic so the job gets created first in the code
and then removed. Given that it's now in a transaction this is
a minor point, but it reads more reasonably.
Merge pull request #22 from mydrive/master

Wrap up requeue and remove failed job in a transaction for safety