@lordmelique/job-scheduler

1.0.6 • Public • Published

job-sceduler

Installation

npm install @lordmelique/job-scheduler

const {Scheduler, Consumer} = require('job-scheduler')

Scheduler

First of all we need to declare scheduler and give the job names which will be scheduled by this scheduler.

const scheduler = new Scheduler({
  redis: {
    port: 6378,
  },
  enqueueMaxTries: 5,
  retryInterval: 10,
  types: ['myjob'],
});

Then we need to add consumer that will consume AWS SQS queue and fire events.

const consumer = new Consumer({
  jobName: 'myjob',
  sqsUrl: 'https://sqs.<REGION>.amazonaws.com/<ACCOUNT_ID>',
  message: function handleMessage(message) {
    console.log(message.Body)
  },
  error: function handleError(err) {
    console.log(err);
  },
  processingError: function handleProcessingError(err) {
    console.log(err);
  },
})

Now we are ready to schedule jobs with scheduler. Lets generate jobs with random Ids and TTLs

(async function() {
  await sch.wait(5);
  try {
    for (let i = 0; i < 10; i++) {
      await sch.scheduleEvent({
        type: 'myjob',
        id: parseInt(Math.random() * 200),
        ttl: parseInt(Math.random() * 10) + 5,
      });
    }

  } catch (e) {
    console.log(e);
  }
})();

Package Sidebar

Install

npm i @lordmelique/job-scheduler

Weekly Downloads

2

Version

1.0.6

License

ISC

Unpacked Size

9.73 kB

Total Files

10

Last publish

Collaborators

  • lordmelique