Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

[Query] Worker callbacks option #68

Open
slahiri opened this issue May 30, 2016 · 4 comments
Open

[Query] Worker callbacks option #68

slahiri opened this issue May 30, 2016 · 4 comments

Comments

@slahiri
Copy link

slahiri commented May 30, 2016

I was going through the worker code and the options take a parameter as callback. Looking at the process code, it tries to find the callback for a particular job.data.name. @scttnlsn if you can help explain the use-case of this functionality?

@scttnlsn
Copy link
Owner

The worker callbacks are keyed by name and should correspond to the values in job.data.name. The callbacks perform the actual computation/work and by naming the callbacks, a single worker can perform various tasks based on the name of the enqueued job. Does that answer your question?

@slahiri
Copy link
Author

slahiri commented Jun 10, 2016

Still trying to figure out. Can you share an example?

@scttnlsn
Copy link
Owner

Example from the README:

A job named reverse is enqueued. This sets job.data.name to the string "reverse" in the job document. A worker function is then registered corresponding to the name:

worker.register({
  reverse: function (params, callback) {
  }
});

You could register as many functions as you need though:

worker.register({
  reverse: ...,
  capitalize: ...,
  lowercase: ...,
  // etc.
});

And enqueue jobs to be processed by those functions respectively:

queue.enqueue('reverse', { text: 'foobar' });
queue.enqueue('capitalize', { text: 'foobar' });
queue.enqueue('lowercase', { text: 'FOOBAR' });

@slahiri
Copy link
Author

slahiri commented Jun 17, 2016

This is brilliant. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants