Skip to content

Latest commit

 

History

History
103 lines (73 loc) · 2.22 KB

README.md

File metadata and controls

103 lines (73 loc) · 2.22 KB

GitHub Workflow Status (branch) Coveralls branch

Basic Usage:

Available Brokers

Worker

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();

Send messages

const queue = // Use one of the available brokers above
await queue.push("My message"); // You can serialize more complex information using JSON

Options

  • Backoff

Time in milliseconds (ms) that the worker backoff from pooling messages from queue when there is no messages to pool

Testing

Jest

  • 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);
});

Fully Functional Examples