feature: add possibility to inject functions that will be called before and/or after each job #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I've added the possibility to "inject" two different functions into
_job_pool_worker. One will be run before the job begins, and the other one will be run after. These functions are optional (defaults to ""), but if provided will be called for each job.Motivation
This improves the job management, as the function
job_pool_waitwaits for ALL queued jobs to finish.We can inject functions that add new jobs according to how/which previous jobs finished, kill all jobs if one fails, add our own logging, etc. (and this, dynamically, without waiting for all queued jobs to finish).
I think there is a wide range of possibilities.
Implementation
I added two new parameters to
job_pool_init:job_pool_function_pre_job(optional) a function that will be called before each jobjob_pool_function_post_job(optional) a function that will be called after each job.Those variables can't be local variables, passed as parameters to
_job_pool_start_workersthen_job_pool_workerasjob_pool_waitwould need to pass those two functions again to_job_pool_start_workers.Those functions are aware of
_job_pool_worker's local variables andjob_pool.sh's global variables, allowing us to use them (see code sample).Further implementation?
I think
job_pool_waitcould accept parameters to redefine the two functions after waiting for each previous job to finish, but it is just an idea and I didn't investigate more in that direction.Code Sample
Sample program demonstrating function injection