RepetitiveTask
RepetitiveTask provides a base layer for building time-based repetitive tasks, such as background jobs that run forever.
Example
Simple task that always runs
const RepetitiveTask = { console } const monitor = 60 * 1000monitorstart
Condition based task
const irrigation = const RepetitiveTask = { return irrigation } { return irrigation } const gardener = 12 * 60 * 60 * 1000gardenerstart
API - Usage
new RepetitiveTask(interval)
Creates a new RepetitiveTask
instance.
interval
(Number): The interval (in seconds) for the repetitive task.
NOTE: Tasks don't run on a strict interval. The specified interval is actually the amount of time between the end of one run and the beginning of the next run. This ensures that a long running task will never result in multiple tasks running simultaneously.
RepetitiveTask#start()
Starts the repetitive task.
RepetitiveTask#stop() Returns: Promise
Stops the repetitive task. Stopping the task may be asynchronous since a task may be running at the time of the call. The promise will resolve when the task has completed.
API - Extending
In order for RepetitiveTask to be useful, you must extend it with your actual task. The following methods are available to customize the behavior of the task.
RepetitiveTask#_createLogger(name) Returns: Object
Creates a logger for the repetitive task.
The built-in logger writes JSON messages to stdout, but can be replaced with any logger that supports the following API:
info([data], message)
error([data], message)
The optional data
parameter is an object with additional properties to include in the log.
RepetitiveTask#_getTask() Returns: Promise
Gets a task to work on. Depending on the type of task being performed, you may need to determine if there is a task to run at any given time. For example, you may need to pull an item off of a queue and process it. In this case, _getTask()
should read from the queue and return the item, if any.
Returning a falsy value indicates that there is no task to perform right now.
RepetitiveTask#_getLogData(task) Returns: Object
Gets additional data to log about the task. The object returned from _getLogData()
will be passed to the logger for the "processing task" message.
task
(Mixed): The task object returned from_getTask()
.
RepetitiveTask#_process(task) Returns: Promise
Processes the task. This is the main function for the repetitive task and is the only method that must be implemented for the task to run.
task
(Mixed): The task object returned from_getTask()
.
License
Copyright RepetitiveTask contributors. Released under the terms of the ISC license.