cluster-scheduler
This is a nodejs package for scheduling tasks to be distributed among cluster workers that are optionally CPU-bound.
Use this package
Here is a sample of how you can use this npm package.
Install the package
With yarn
...
yarn add cluster-scheduler
With npm
...
npm i cluster-scheduler
Use the package in your code
main.js
// Include this for forking processesconst cluster = ; const Scheduler getLogger Job READY = ;const logger = ; /* * Declare a function that a worker will use for processing a job. This function will be * called by a worker when it receives a message from the master process. * * This function takes one argument, which is the `Job` object scheduled below */const processJob = { logger; // Do something that takes awhile ;}; /* * Declare a function to handle the result of the job. This function will be called * upon receiving a message back from a cluster worker. * * This function accepts one argument, which is a arbitrary object sent as the `results` * property in the message sent by the worker above */const handleResult = { logger;}; if clusterisMaster // Start the scheduler on the master process, with one worker per underlying core const scheduler = {}; logger; // Schedule a job now and every 2s scheduler ; else // For worker processes, listen for messages (jobs) from the master process logger; process;
Note: You can find this source code in samples/main.js
Run your code
node main.js