1.7.4 • Published 4 years ago

cluster-scheduler v1.7.4

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

cluster-scheduler

npm version

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 processes
const cluster = require("cluster");

const { Scheduler, getLogger, Job, READY } = require("cluster-scheduler");
const logger = getLogger("index");

/*
 * 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 = job => {
  logger.info(`Processing job '${job.name}'`);

  // Do something that takes awhile
  setTimeout(() => {
    process.send({
      workerStatus: READY,
      results: job.config.greeting // this is the argument passed to the `handleResult` function below
    });
  }, 2000);
};

/*
 * 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 = results => {
  logger.info(`Results are in: ${results}`);
};

if (cluster.isMaster) {
  // Start the scheduler on the master process, with one worker per underlying core
  const scheduler = new Scheduler({});
  logger.info(`Scheduler started. Spawning ${scheduler.numWorkers} processes.`);

  // Schedule a job now and every 2s
  scheduler
    .schedule(
      new Job({
        name: "sample-job",
        config: { greeting: "hello" },
        callback: handleResult
      })
    )
    .now()
    .and()
    .every({ s: 2 });
} else {
  // For worker processes, listen for messages (jobs) from the master process
  logger.info(`Worker started on PID ${process.pid}`);
  process.on("message", processJob);
}

Note: You can find this source code in samples/main.js

Run your code

node main.js
1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.7.4

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.2

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago