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

Each due task is executed in its own thread with its own javascript runtime (modules have to be loaded inside the task script). A task is only started if the previous run of the same task (name) has finished; otherwise the run is skipped. Due tasks are only detected while processTasks() is called regularly. Inside a task script, infinity.terminated returns the value of the main program; since 3.11 it is also true while free() stops the running tasks (before, free() blocked forever if a task waited for it).

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:

Throws an error if the plan is invalid.

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. Task runs that are currently executing are not stopped.

Signature:

clear(): void

Example:

myScheduler.clear();

free()

Frees the memory previously occupied by the INFINITY.JS scheduler object instance. Sets infinity.terminated of the running tasks to true and waits until they have finished. The scheduler object must not be used anymore after calling free(): its methods throw an error (since 3.11; before, an access violation).

Signature:

free(): void

Example:

myScheduler.free();

processTasks()

Checks the tasks of the scheduler object instance and starts the execution of due tasks in separate threads. Returns immediately without waiting for the tasks. Call it regularly (e.g. every 500 milliseconds) in the main loop of your program.

Signature:

processTasks(): void

Example:

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

remove()

Removes the specified task from the scheduler object instance. Returns true if a task with this name has been found and removed, otherwise false. A currently running execution of the task is not stopped.

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

Thread priority of a task. Only supported on Windows.

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.

  • high: 4

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

  • higher: 5

    Stands for the task having priority above high 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;