master: Refactor chunk_do_jobs

Modify the code to make it easier to implement custom goals by splitting
the huge function into smaller and extracting static variables from this
function.

Change-Id: I1620d8f0c43cd57fb3b520b4239153e695aa1500