@lordmelique/job-scheduler

A simple module for managing delayed task jobs using redis pub/sub with AWS SQS


Keywords
scheduler, sqs, redis, pub/sub, pub, sub
License
ISC
Install
npm install @lordmelique/job-scheduler@1.0.6

Documentation

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);
  }
})();