Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Dynamic jobs #33

Closed
corzani opened this issue Aug 31, 2020 · 6 comments
Closed

[feat] Dynamic jobs #33

corzani opened this issue Aug 31, 2020 · 6 comments

Comments

@corzani
Copy link

corzani commented Aug 31, 2020

Hi,
is there a way to create a dynamic set of jobs? How can I pass custom parameters to a job?
I was wondering if a similar approach to the following one can be used...

const currencies = ['USD', 'EUR','GBP'];
const currencyJob = currency => () => {
// job
console.log(currency)
};

const jobs = currencies.map(currency => ({task: currencyJob(currency), interval: '2m'}));
const bree = new Bree({jobs});

// start...

Thanks

@ggb88
Copy link

ggb88 commented Sep 6, 2020

Just from my little investigation, would using bree.add(..) work?

@corzani
Copy link
Author

corzani commented Sep 7, 2020

Just from my little investigation, would using bree.add(..) work?

Hi @ggb88 , first of all, thanks...
As far as I understood, add doesn't let you specify different parameters either. In my example, every job is started with different currencies because they are treated as a function and not as an external file dynamically loaded/required.
By adding a lazy approach you can execute your task only when you need them (like the current implementation) and at the same time having the best from a functional approach (parametrized jobs, lazy loading...). Let's say you have 1000 jobs, every job differs only by its parameters and not its implementation. How would you implement it with this current implementation?
Consider my example, if you have 1000 currencies that code would work, it's not optimal regarding the interval bit... but it's working.

@corzani
Copy link
Author

corzani commented Sep 7, 2020

I did find a possible solution, workerData to pass parameters to the dynamically created job list. It would have been more "elegant" having functions but that would work well anyway. Thanks @ggb88

@corzani corzani closed this as completed Sep 7, 2020
@fernandolguevara
Copy link

@niftylettuce this could be a very useful/great feature for the library, there is a better way to do this?

@tamaker
Copy link

tamaker commented Nov 29, 2020

I did find a possible solution, workerData to pass parameters to the dynamically created job list. It would have been more "elegant" having functions but that would work well anyway. Thanks @ggb88

@corzani Can you share a rough example of how you're achieving this? I'm exploring options along the same lines as your approach. Thanks!

@corzani
Copy link
Author

corzani commented Nov 30, 2020

@tamaker
When you create a task you need to specify also the workerData property.

    {
      name: 'task1',
      worker: {
        workerData: {
          foo: 'something',
          fie: 'something else'
        }
      }

Considering the case above you can retrieve those properties from your job (task1.js) using worker threads.

const { workerData } = require('worker_threads');
console.dir(workerData); // This is what you need

See https://nodejs.org/api/worker_threads.html#worker_threads_worker_workerdata

I did notice the following method when I was looking for an example (2 mins ago)...
Check => https://github.com/breejs/bree#using-functions-for-jobs
So, it seems you can specify a function... I didn't even noticed that when I was reading the docs... my bad :)

So currently you might have 2 ways to do it...

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

No branches or pull requests

4 participants