Lock job before assigning an agent to it.

This avoids a problem where the same batch of tasks is sent to more than
one agent to process.
Merge pull request #320 from guidow/per_job_locks

Lock job before assigning an agent to it.