Configuration.task()
Description
Creates a timer pipeline layout or a signal pipeline layout for a periodic job or a signal.
A timer pipeline or a signal pipeline has the following input/output:
- INPUT - Nothing.
- OUTPUT - Discarded.
A task pipeline layout can be used in 3 scenarios:
- When used without an argument, the job will be started only once at startup.
- When used with a number or a time-unit-suffixed string, the job will be created periodically. Valid time unit suffixes include: s for seconds, m for minutes, h for hours.
- When used with a signal name such as "SIGINT" or "SIGHUP", the job will be created when the signal is received.
At creation of a task pipeline, onStart() is usually used to generate the initial Event(s) driving the filters on the pipeline. The pipeline should eventually output a StreamEnd event to indicate the completion of the job. The next recurrence of the job, if any, will not happen until the current one is completed.
After calling this method on a Configuration, the newly created pipeline layout would become current. Any filter-creating methods called after this point would append filters to that pipeline layout, until another pipeline-layout-creating method is called.
Syntax
pipy().task().filterA().filterB()// ...pipy().task(interval).filterA().filterB()// ...pipy().task(signal).filterA().filterB()// ...
Parameters
task(intervalOrSignal?)
Can be either:
- Nothing (undefined) - Create a pipeline only at startup time.
- A time duration - Create a pipeline regularly every specified amount of time. Can be a number in seconds or a string with one of the time unit suffixes such as s, m or h.
- A signal name - Create a pipeline when receiving a signal, e.g. "SIGHUP", "SIGINT".
The same Configuration object.
Example
pipy({_g: { counter: 0 },})// Start a job at startup.task().onStart(() => (console.log('Started.'),new StreamEnd // Indicate end of the job by outputting a StreamEnd event))// Start a job every 5 seconds.task('5s').onStart(() => new Message).handleStreamStart(// Print 'Hello!' at the beginning of the job() => console.log(`Hello! Counter is ${++_g.counter}`)).replaceMessage(// Indicate end of the job by outputting a StreamEnd eventnew StreamEnd)// Start a job when SIGINT is received.task('SIGINT').onStart(() => new Message).handleStreamStart(// Gracefully shut down, which is also the default behaviour for SIGINT() => pipy.exit()).replaceMessage(// Indicate end of the job by outputting a StreamEnd eventnew StreamEnd)