Would like to handle jobs in batch
See original GitHub issueHi !
Something i was wondering about, could we add batch support for worker ? Let say i have 500 jobs which each of them fetch data to an external API or database for example. Batching them in a single request would help reduce the load on these external resources and also reduce latency in general. Example :
const zbBatchWorker = zbc.createBatchWorker(
'test-worker',
'demo-service',
{
batch: 50, // # of jobs per batch
timeout: 1000, // or 1 sec
},
async (payloads, complete) => {
const ids = jobs.map((j) => j.id);
const users = await fetch(`someUrl/user?ids=${ids.join(',')}`);
for(const [key] of Object.keys(ids)) {
const index = Number(key);
const user = users[index];
try {
// ...Do some work...
if(...) {
complete.success(index, ...)
} else {
complete.failure(index, ...)
}
} catch(error) {
complete.error(index, ...)
}
}
/*
When the batch is done,
this commit each job and
call the right procedure (grpc)
base on 'complete'
*/
await complete.done();
}
);
I can pass the number of jobs per batch or a timeout after which the batch is handle anyway. That said, it could be link to the maxActiveJobs propertie.
Issue Analytics
- State:
- Created 4 years ago
- Comments:20 (13 by maintainers)
Top Results From Across the Web
Would like to handle jobs in batch · Issue #134 - GitHub
Let say i have 500 jobs which each of them fetch data to an external API or database for example. Batching them in...
Read more >What Is a Batch Job? – BMC Software | Blogs
Batch jobs are frequently used to automate tasks that need to be performed on a regular basis, like payroll, but don't necessarily need...
Read more >Work management concepts: Batch jobs - IBM
Batch jobs run in the system background, freeing the user who submitted the job to do other work. Several batch jobs can be...
Read more >Handle Errors in your Batch Job… Like a Champ!
Fact: Batch Jobs are tricky to handle when exceptions raise. The problem is the huge amounts of data that these jobs are designed...
Read more >How to run a batch job from an application screen or logic in ...
Batch jobs can also be triggered to run from a screen or business logic. Watch this video to learn how. In case you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think it’s exactly what i want in term of functionality. Thanks !
Will be out this week in the 0.23.0-alpha.1 release.
Here are the docs from the README:
The
ZBBatchWorker
Job WorkerThe
ZBBatchWorker
Job Worker batches jobs before calling the job handler. Its fundamental differences from the ZBWorker are:success
,failure
,error
, andforwarded
methods attached to them.You can use the batch worker if you have tasks that benefit from processing together, but are not related in the BPMN model.
An example would be a high volume of jobs that require calls to an external system, where you have to pay per call to that system. In that case, you may want to batch up jobs, make one call to the external system, then update all the jobs and send them on their way.
The batch worker works on a first-of batch size or batch timeout basis.
You must configure both
jobBatchMinSize
andjobBatchMaxTime
. Whichever condition is met first will trigger the processing of the jobs:You should be sure to specify a
timeout
for your worker that isjobBatchMaxTime
plus the expected latency of the external call plus your processing time and network latency, to avoid the broker timing your batch worker’s lock and making the jobs available to another worker. That would defeat the whole purpose.Here is an example of using the
ZBBatchWorker
: