Skip to content

Instantly share code, notes, and snippets.

@jwulf
Last active March 4, 2020 04:10
Show Gist options
  • Select an option

  • Save jwulf/b789f4da4df827e7007c5e4e7e1b938f to your computer and use it in GitHub Desktop.

Select an option

Save jwulf/b789f4da4df827e7007c5e4e7e1b938f to your computer and use it in GitHub Desktop.
A code example from the article https://joshwulf.com/blog/2020/03/zb-batch-worker/
export const JobBuffer = ({
handler,
timeout,
batchSize,
worker,
}: {
handler: ZBBatchWorkerTaskHandler<any, any, any>
timeout: number
batchSize: number
worker: ZBBatchWorker<any, any, any>
}) => {
let jobs: any[] = []
let t
const execute = (batch: BatchedJob[]) => {
t = undefined
jobs = []
worker.debug(`Executing batched handler with ${batch.length} jobs`)
try {
handler(batch, worker)
} catch (e) {
worker.error(
`An unhandled exception occurred in the worker task handler!`
)
worker.error(e.message)
worker.error(e)
}
}
return {
batch: batch => {
if (!t) {
t = setTimeout(() => execute([...jobs]), timeout * 1000)
}
jobs = [...jobs, ...batch]
if (jobs.length >= batchSize) {
clearTimeout(t)
execute([...jobs])
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment