Skip to main content

Recipes

Register the task executor

WorkerHooks connects the worker's poll loop to your task-processing logic. Typically this is registered by @quik/scheduler's database executor, but any handler can be registered:

import { WorkerHooks } from '@quik/queue-worker';

WorkerHooks.register('execute', async (batchSize) => {
// claim and run up to `batchSize` due tasks
});

React to configuration reload and restart requests

WorkerHooks.register('reloadConfig', async () => {
// re-read config that affects task execution
});

WorkerHooks.register('restart', async () => {
// re-initialize resources (e.g. reconnect to external services)
});

Control a running worker from the main thread

Send messages to the worker's parentPort to trigger the corresponding hooks or a graceful shutdown:

worker.postMessage({ type: 'reload-config' });
worker.postMessage({ type: 'restart' });
worker.postMessage({ type: 'shutdown' }); // waits for the in-flight tick to finish, then exits

Run multiple workers

Spawn several worker_threads, each with a distinct workerData.workerName, to increase queue throughput. Each worker polls independently on scheduler.queue.intervalMs.

Checklist

  • shutdown waits for any in-flight tick to complete before closing the database connection and exiting — it does not abort a running batch.
  • Keep scheduler.queue.batchSize aligned with how many tasks a single tick can safely process within scheduler.queue.intervalMs.