Skip to main content

Scheduler Moolet

You can schedule jobs at regular intervals by editing the $MOOGSOFT_HOME/config/moolets/scheduler.conf configuration file:

# The Scheduler is used to run scheduled jobs at regular
# intervals throughout the lifetime of moog_farmd. Only this
# moolet, which cannot subscribe to the MooMS bus and
# listen to events, is allowed to submit scheduled jobs.
#
# To start up successfully it must have the name and threads
# values set to "Scheduler" and 1 respectively.
{
        name                    : "Scheduler",
        classname               : "CScheduler",
        run_on_startup          : false,
        metric_path_moolet      : false,
        moobot                  : "Scheduling.js",
        threads                 : 1
}

Moolets are capable of supporting multiple Moobots. By configuring a Moolet to run multiple Moobots, you can customise the functions of the default Moobot. You can use this feature to customise the actions for Workflow Engine. To do this, locate the Moobot property in the Moolet configuration file and add a comma-separated list of the Moobots you want to run. See the extract Scheduler parameters above and notice the default "moobot" property which contains one Moobot.

To load the scheduler module:

var scheduler   =MooBot.loadModule('Scheduler');

Run jobs using, for example:

// A job that fails and does not restart.
scheduler.scheduleJob(this, "knockOnce", 5, 5, false);

This calls a method in the same js file called knockOnce:

function knockOnce()
{
    logger.warning("Knock knock");
    throw new Error("Failed to knock.");
}

The scheduledJob method has two possible parameter sets:

  • scheduleJob(this, functionName, start_delay , interval );

  • scheduleJob(this, functionName, start_delay, interval, true | false) ;

Parameter

Description

first

always this

second

the name of the function to call to run the job

third

is the delay from starting farmd to the first run (in seconds)

fourth

the interval between runs (in seconds)

fifth

decides whether the job will run again if it failed previously

Scheduling Frequency

When executing multiple jobs we recommend that you try and offset potential workload, by for example staggering the initial run of multiple jobs a few seconds apart or scheduling jobs at slightly offset frequencies.

Constraints
  1. Must be single threaded.

  2. Only one per Moogfarmd process.

  3. Has to be called Scheduler.

  4. Use a Moobot module function as a scheduled job - which involves some rarer JavaScript as the scheduler won’t recognise function names from modules (it can’t find them)

    For example, in your scheduler moobot you might have:

    MooBot.loadModule('AutoClose.js');
    var autoClose=new AutoClose();
    
    // Bind the module function locally to the module function.
    
    var autoCloseAlertFunction = autoClose.closeAgedAlerts.bind(autoClose);
    
    // Schedule execution
    
    scheduler.scheduleJob(this, "autoCloseAlertFunction" , 60, autoCloseAlertFrequency , true);