Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Workers

Ahmet edited this page May 12, 2018 · 14 revisions

You can use two type workers in this package.

  • Custom worker classes that can use in main context.
  • Web Workers that available global context.

If you are target optimal performance, we recommendation the web workers.

Custom Workers

class SendEmail {
  retry = 2;
  timeout = 1200;

  handle(args, user, order, price) {
    return new Promise((resolve, reject) => {
      if (something) {
        resolve(true);
      } else {
        resolve(false);
      }
    });
  }

  before(args) {
    // do something in same context
  }

  after(args) {
    // do something in same context
  }
}

Note: The worker classes has two events. before and after

Worker Register:

Queue.workers({ SendEmail });

Worker Dependencies:

const user = new User();
const order = new Order();
const price = 9;

Queue.deps({ SendEmail: [user, order, price] });

Web Workers

You should create file for each web workers.

hello_world_worker.js

onmessage = (e) => {
  // do something
  postMessage('Hello World!');
};

Worker Register:

function myHandler(e) {
  // do something

  return true;
}

// worker uri can be nested path in your project.
// example: workers/hello_world_worker.js
const HelloWorldWorker = { uri: 'hello_world_worker.js', handler: myHandler };

Queue.workers({ HelloWorldWorker });

Note: We will call the handler when worker done.

Clone this wiki locally