Use locking in assign_tasks_to_agent

We were experiencing some parallelism problems where two or more
instances of assign_tasks_to_agent were running at the same time for the
same agent and ended up assigning more tasks than they should for that
agent.

In theory, database transactions should take care of that, but somehow
they don't.  Weird.
Write time to lockfile
Set job state to running when starting it
Avoid division by zero
Don't use relationship to get child queues

The top queue is not actually a database object, and as such, its
relationship attributes won't work as expected
Actually call method