snowpi-notcron

Simple persistent intervals module for use with Keystone

Usage no npm install needed!

<script type="module">
  import snowpiNotcron from 'https://cdn.skypack.dev/snowpi-notcron';
</script>

README

Snowpi NotCron

Persistent and Non-Persistent interval manager for use with Keystone

Install

npm install snowpi-notcron

//or add to package.json
"dependencies": {
    "snowpi-notcron": "~0.0.x"
}

Use non-persisting intervals

Use a simple object to control your intervals

var notcron = require('snowpi-notcron');


var minute = notcron.simple.setInterval(function(){},2500); //create a new interval
notcron.simple.clearInterval(minute); // clear minute interval
notcron.simple.clearIntervals(); //clear all intervals

console.log(notcron.simple); // all your intervals


Use Persistence

You must set the correct Model to use for persistence and start the saved intervals.

option 1: use a valid mongoose model

var notcron = require('snowpi-notcron');

keystone.start({
    onStart: function() {
        // add any valid mongoose model object
        notcron.init({'model':mongoose.model('Settings')}).start();
    }
);

option 2: use a String value

var notcron = require('snowpi-notcron');

//add a string value to use keystone.list
notcron.set('model','Settings');

keystone.start({
    onStart: function(){
        notcron.init().start();
    }
);

We expect 2 fields: setting and value. Here is a sample Model:

var Settings = new keystone.List('Settings', {
    track:true,
});

Settings.add({
    setting: {type:String, index:true, initial:true },
    value: { type: String,  label:'value', initial:true}
});
Settings.register();

Create a new persisting interval
var notcron = require('snowpi-notcron');


var fn = {
        module: '/path/to/custom/module/sayHello.js',
        fn : 'moduleFunction',
        args: ['arg1','arg2'],
        callback: 'callback',
}

var whenDoneCreatingInterval = function() {
    /**
    * do something 1 time when start succeeds
    * does not run as part of the interval
    * */
    console.log(notcron.persist['minute'])
    
}

//now create the persistent interval
notcron.persist.set(60000,fn,'minute').start(whenDoneCreatingInterval)


persist only accepts string values in the fn Object with exception of args which is an array of String / Boolean values or a String / Boolean value

fn.module can be a path to your own module or an installed node module. To use your own module the path must start with a forward slash /. You can set a base path to be prepended:

notcron.set('baseDir','/home/snowpi/projects/myproject')

fn.fn is the name of a module function you want to run defined as a String

fn.args is a simple array with your function arguments in the order they should be applied.

fn.callback is the name of a module function you want to run as a callback defined as a String

//yields
var module = require (requestedModule)
notcron.persist['minute'] = startInterval(function(){module.moduleFunction('arg1','arg2',module.callback)},60000);

Interaction

notcron.persist.clear = 'minute'  // kill permanently

var myrepeater = notcron.persist['minute']

myrepeater.kill() // clear now && kill on restart  
myrepeater._interval // interval object/ID
myrepeater.time // interval time
myrepeater.job // 'minute' in our example
myrepeater.programs // Array of functions that will execute

Special non-persistent methods

myrepeater.clear() //kill until restart
myrepeater.join(myFunction) // adds a function to the current interval
myrepeater.scratch(myFunction) // remove a function from interval

Non-persistent methods will not survive a restart