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.