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).
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
scheduler¶
The class for operations with scheduled tasks in INFINITY.JS.
Methods:
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:
stringThe internal name for the task.
-
plan:
stringThe time schedule for the task (see below).
-
scriptName:
stringPath of the script to be executed, relative to the main program folder (
infinity.current.root), like forinfinity.include(). -
priority:
infinity.scheduler.priority, optionalThe thread priority for the script according to
infinity.scheduler.priority(default:normal). Only supported on Windows, ignored on other platforms.
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 * * 1this will run the task at 6 o'clock in the morning on every monday0 6this will run the task every day at 6 o'clock in the morning*/5will run the task every 5 minutes* * * * * * 30will run the task every minute on the 30th second0 8,14,20will run the task at 8, 14 and 20 o'clock every day0 0 1-3will run the task at midnight of the first three days each month0 0 1 1,4,7,10will run the task at midnight on the first day of January, April, July and October*/5 6 * * * * 0 3will 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:
stringThe internal name of the task to be removed.
Return type: boolean
Example:
myScheduler.remove('testTask');
infinity.scheduler.priority¶
Thread priority of a task. Only supported on Windows.
Values:
-
idle:
0Stands for the task running only when the system is idle.
-
lowest:
1Stands for the task having priority above
idlebut belowlower. -
lower:
2Stands for the task having priority above
lowestbut belownormal. -
normal:
3Stands for the task having no special prioritizing needs.
-
high:
4Stands for the task having priority above
normalbut belowhigher. -
higher:
5Stands for the task having priority above
highbut belowtimeCritical. -
timeCritical:
6Stands for the task having the highest possible priority.
Example:
infinity.loadModule('infinity.scheduler');
let priority = infinity.scheduler.priority.idle;