Coder Social home page Coder Social logo

jimdillon / tke-cron-master-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tkeitmobility/tke-cron-master-builder

0.0 1.0 0.0 29 KB

Simple module that initiates all of the 'cron-master' jobs defined in a given folder.

JavaScript 100.00%

tke-cron-master-builder's Introduction

tke-cron-master-builder

Simple module that initiates all of the 'cron-master' jobs defined in a configurable folder. The module also initiates heartbeat logging to a configurable MongoDB collection for all of the given Cron Jobs, and takes care of cleaning said collection.

NOTE: 'cron-master' library expected and required as a co-dependency. Also, the environment variable RAYGUN_API_KEY must be set

Running the Module

loadJobs

Grabs the Cron Job config files in the specified directory, initiates and starts them:

const path = require('path');

const cleanUpOpts = {
  frequency: '* * 0 * * *', // midnight
  retentionPeriod: '5 days' // items more than five days old are removed
};

const jobsDir = path.join(process.cwd(), './cron-jobs');

const cronBuilder = require('tke-cron-master-builder')(
  jobsDir,
  'cron-logs', // a mongodb collection name
  cleanUpOpts
);

// load all jobs in cronConfigDir
cronBuilder.loadJobs();

getHandlers

Perhaps you want to attach handlers to a job without loading it via loadJobs, this will allow you:

const CM = require('cron-master');
const cronBuilder = require('tke-cron-master-builder')(
  jobsDir,
  'cron-logs',
  cleanUpOpts
);

const job = new CM.CronMasterJob({
  // will trigger the TIME_WARNING event if job runs more than 2 minutes
  timeThreshold: 2 * 60 * 1000,

  meta: {
    name: 'my-cron-job'
  },

  // these are passed to the node-cron module used internally by CronMasterJob
  cronParams: {
    cronTime: '* * * * *',
    onTick: (job, done) => {
      console.log(new Date(), 'tick happened for our cron job!');
      done();
    }
  }
});

const handlers = cronBuilder.getHandlers(job);

// Bind event handlers as needed
job.on(CM.TICK_COMPLETE, handlers.onTickComplete);
job.on(CM.TICK_STARTED, handlers.onTickStarted);
job.on(CM.OVERLAPPING_CALL, handlers.onOverlappingCall);
job.on(CM.TIME_WARNING, handlers.onTimeWarning);

attachEventHandlers

Similar to getHandlers, but this attaches event handlers to the passed job for you:

const CM = require('cron-master');
const cronBuilder = require('tke-cron-master-builder')(
  jobsDir,
  'cron-logs',
  cleanUpOpts
);

const job = new CM.CronMasterJob({
  // will trigger the TIME_WARNING event if job runs more than 2 minutes
  timeThreshold: 2 * 60 * 1000,

  meta: {
    name: 'my-cron-job'
  },

  // these are passed to the node-cron module used internally by CronMasterJob
  cronParams: {
    cronTime: '* * * * *',
    onTick: (job, done) => {
      console.log(new Date(), 'tick happened for our cron job!');
      done();
    }
  }
});

// attach all our handlers
cronBuilder.attachEventHandlers(job)

Parameters

cronConfigsDir {String} (required): Complete path to config files for cron-master Cron Jobs

collection {String} (required): Name of Collection to store Cron Logs in

logCleanUpOpts {object} (optional): Options for Cleaning Up Cron Job Logs (i.e. removing the logs from MongoDB)

logCleanUpOpts.frequency {String} (optional): Frequency of clean up Cron. Defaulted to '* * 0 * * *', or every night at midnight, when omitted.

logCleanUpOpts.retentionPeriod {String} (optional): Human readable retention period for Logs. Defaulted to '7 days' when omitted. NOTE: 'timestring' library is used for parsing retentionPeriod

MongoDB Logs

The 'tke-logger' library is used for console logging. Therefore, Logs of type warn and error are also written to MongoDB. These are in the general 'bunyan' JSON log format.

The Cron Job heartbeat logs that are written to MongoDB are on the following format:

    "error": Error Object,
    "finishTime": Date Object,
    "lastStatus": "Success" || "Failed" || "In Process",
    "name": String Path to Cron Config File,
    "nextSchedule": Date Object,
    "startTime": Date Object

Logs are inserted at the start of each cron iteration and updated on job complete or failure. There are also console warnings for a job overlapping itself and for taking too long to complete.

tke-cron-master-builder's People

Contributors

evanshortiss avatar j-burlison avatar jainmnsh avatar mattheh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.