import { Worker, Message } from 'hyperq';
const queue = // Use one of the available brokers above
const action = async (message: Message, w: Worker): Promise<void> => {
const msg = message.body;
try {
// ...do your thing
} catch(e) {
await message.retry(); // Message goes back to the queue instantly
}
await message.delete(); // Message is deleted forever
};
const start: () => void = async () => {
const worker = new Worker(queue, action, {} //Options);
const exit = () => {
worker.exit();
console.log('Exiting...');
}
process.on('SIGTERM', exit);
process.on('SIGINT', exit);
console.log('Running...');
await worker.run();
process.exit(0);
};
start();
const queue = // Use one of the available brokers above
await queue.push("My message"); // You can serialize more complex information using JSON
- Backoff
Time in milliseconds (ms) that the worker backoff from pooling messages from queue when there is no messages to pool
- MyWorker.ts
import { Worker, Message, Queue } from 'hyperq';
export default (queue: Queue) => {
const action = async (message: Message, w: Worker): Promise<void> => {
const msg = message.body;
try {
// ...do your thing
} catch(e) {
await message.retry(); // Message goes back to the queue instantly
}
await message.delete(); // Message is deleted forever
};
return new Worker(queue, action, {});;
}
- MyWorker.spec.ts
import { Worker, MemoryQueue } from 'hyperq';
import MyWorker from './myWorker';
test('process msg', async () => {
const queue = new MemoryQueue();
await queue.push("my_msg");
const run = MyWorker(queue).run();
worker.exit();
await run;
expect(yourThing).toEqual(isDone);
});