Skip to content

infinity.scheduler

Provides functionality for (CronJob-like) execution timing of scripts, as long as the infinity instance is running.

Module: infinity.scheduler

Classes:
Enums:

Example:

main.ts:

infinity.loadModule('infinity.scheduler');

let myScheduler = new infinity.scheduler();
myScheduler.add('testTask', '* * * * * * */10',  'task.js'); 

while(!infinity.terminated){
  myScheduler.processTasks();
  infinity.sleep(500);
}

task.ts:

infinity.loadModule('infinity.fileSystem')
infinity.loadModule('infinity.date')

let entry = infinity.date.formatLocal('dd.mm.yyyy - hh:mm:ss', infinity.date.now('Europe/Berlin')) + ': task done\n';

infinity.file.writeString(infinity.current.root + '../log.txt', entry, infinity.encoding.utf8, true);

log.txt after execution:

08.04.2021 - 15:00:20: task done
08.04.2021 - 15:00:30: task done
08.04.2021 - 15:00:40: task done

Classes

scheduler

The class for operations with scheduled tasks in INFINITY.JS.

Example:

infinity.loadModule('infinity.scheduler');
let myScheduler = new infinity.scheduler();

Methods


constructor()

Lets you create an INFINITY.JS scheduler object instance. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.scheduler');
let myScheduler = new infinity.scheduler();

add()

Adds a task to the scheduler object instance.

Signature:

add( name: string, plan: string, scriptName: string, priority?: infinity.scheduler.priority ): void

Parameters:

  • name: string

    The internal name for the task.

  • plan: string

    The time schedule for the task (see below).

  • scriptName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the script to be executed.

  • priority: infinity.scheduler.priority, optional

    The process priority for the script according to infinity.scheduler.priority.

The plan parameter follows the format used by unix/linux cronjobs. It consists of several fields separated by a space:

<minute> <hour> <day> <month> <weekday> <year> <seconds> <limit>

The fields in order from left to right are:

  • minute (range: 0-59)
  • hour (range: 0-23)
  • day of the month (range: 1-31)
  • month of the year (range: 1-12)
  • day of the week (range: 1-7, 1 = monday, 2 = tuesday, etc.)
  • year (range: 1900-3000)
  • seconds (rand: 0-59, default: 0)
  • execution limit (range: 0-0xffffffff, default: 0 = unlimited)

Each of these fields may contain an asterisk as a wildcard, which would match every possible value of that field (e.g. a * in the hour field would mean that the task is run every hour). A field may also contain a comma-separated list of values or a range of values separated by a dash (e.g. 8,14,20 in the hour field would run the task at eight o'clock in the morning and at two and eight o'clock in the afternoon/evening). It's also possible to append a slash with a value after an asterisk or range, indicating that the task should run at a certain interval (e.g. */4 in the hours field would run the task every four hours).

If a value is omitted, an asterisk will be used as a default, except where noted (the default for seconds and limit is 0).

Here's some examples for the plan parameter:

  • 0 6 * * 1 this will run the task at 6 o'clock in the morning on every monday
  • 0 6 this will run the task every day at 6 o'clock in the morning
  • */5 will run the task every 5 minutes
  • * * * * * * 30 will run the task every minute on the 30th second
  • 0 8,14,20 will run the task at 8, 14 and 20 o'clock every day
  • 0 0 1-3 will run the task at midnight of the first three days each month
  • 0 0 1 1,4,7,10 will run the task at midnight on the first day of January, April, July and October
  • */5 6 * * * * 0 3 will run the task three times every 5 minutes from 6 o'clock in the morning, every day

Example:

myScheduler.add('testTask', '*/5 1-3 1 1,4,7,10',  'task.js'); 

clear()

Clears all tasks from the scheduler object instance.

Signature:

clear(): void

Example:

myScheduler.clear();

free()

Frees the memory previously occupied by of the INFINITY.JS scheduler object instance.

Signature:

free(): void

Example:

myScheduler.free();

processTasks()

Initializes the check for tasks inside the scheduler object instance and starts the execution of due tasks.

Signature:

processTasks(): boolean

Return type: boolean

Example:

while(!infinity.terminated){
  myScheduler.processTasks();
  infinity.sleep(500);
}

remove()

Removes the specified task from the scheduler object instance.

Signature:

remove( name: string ): boolean

Parameters:

  • name: string

    The internal name of the task to be removed.

Return type: boolean

Example:

myScheduler.remove('testTask');

Enums

infinity.scheduler.priority

Values:

  • idle: 0

    Stands for the task running only when the system is idle.

  • lowest: 1

    Stands for the task having priority above idle but below lower.

  • lower: 2

    Stands for the task having priority above lowest but below normal.

  • normal: 3

    Stands for the task having no special prioritizing needs.

  • higher: 4

    Stands for the task having priority above normal but below highest.

  • highest: 5

    Stands for the task having priority above higher but below timeCritical.

  • timeCritical: 6

    Stands for the task having the highest possible priority.

Example:

infinity.loadModule('infinity.scheduler');
let priority = infinity.scheduler.priority.idle;