0.0.7 • Published 9 years ago

chronoscript v0.0.7

Weekly downloads
3
License
-
Repository
github
Last release
9 years ago

Chronoscript

Simple scheduler for node.

Global

Install this globally and you'll have access to the chrono command anywhere on your system.

$ npm install -g chronoscript
# ...
$ chrono
# chrono ./chronofile.js
# or
$ chrono  ./someFolder/chronofile.js

Chronofile

This file is where you indicate the several actions you want to perform.

You can use the command chrono --init to create a template chronofile.

The chronofile must have a specific structure as shown in the example bellow:

module.exports = function(chrono) {

    var dateFormat = 'DD/MM/YYYY HH:mm:ss';

    chrono.script({
        startDate: '01/06/2015 12:30:00',
        endDate: '12/06/2020 00:45:35',
        interval: '10 s',
        triggers: ['trigger1', 'trigger2']
    }, function(info, triggers) {

        //script action
        console.log('script: ' + info.date.format(dateFormat));

        //some async tasks
        setTimeout(function() {

            //call triggers
            triggers(); //triggers(['trigger1', 'trigger2'])
        }, 2000);
    });

    chrono.trigger({
        name: 'trigger1',
    }, function(info) {

        //trigger action
        console.log('trigger1: ' + info.date.format(dateFormat));
    });

    chrono.trigger({
        name: 'trigger2',
    }, function(info) {

        //trigger action
        console.log('trigger2: ' + info.date.format(dateFormat));
    });
};

Examples can be found here.

Local

$ npm install chronoscript
var chrono = require('chronoscript');

var dateFormat = 'DD/MM/YYYY HH:mm:ss';

chrono.script({
    startDate: '01/06/2015 12:30:00',
    endDate: '12/06/2020 00:45:35',
    interval: '10 s',
    triggers: ['trigger']
}, function(info, triggers) {
    console.log('script: ' + info.date.format(dateFormat));
    triggers();
});

chrono.trigger({
    name: 'trigger',
}, function(info) {
    console.log('trigger: ' + info.date.format(dateFormat));
});

chrono.start();

API

script(options, action)

options

Required:

  • startDate (String) - start date of the script, format: dd/MM/yyyy hh:mm:ss

    Note: If the startDate has already passed, its added the interval till startDate is valid, for example:

    current date: 11/06/2015 00:00:00

    interval: 1 w

    startDate: 03/06/2015 00:00:00, turns to 17/06/2015 00:00:00

  • interval (String) - time between executions

    Ranges:

    • 'x w' - x weeks
    • 'x d' - x days
    • 'x h' - x hours
    • 'x m' - x minutes
    • 'x s' - x seconds

      You can also combine the ranges, for example: '2 h 30 m' for an interval of 2 hours and 30 minutes.

Optional:

  • endDate (String) - end date of the script, format: dd/MM/yyyy hh:mm:ss
  • triggers (Array) - name of triggers

action(info, triggers)

info:

  • info.date (moment object) - current execution datetime
  • info.n (Number) - number of executions

triggers (function) - start script triggers

You can also pass an Array with the names of the specific triggers you want to start, example:

triggers(); //start all triggers
triggers(['trigger1']); //start trigger1
triggers(['trigger1', 'trigger2']); //start trigger1 and trigger2

trigger(options, action)

Required:

  • name (String) - name of the trigger

action(info)

info:

  • info.date (moment object) - current execution datetime
  • info.n (Number) - number of executions

License

MIT