Constructor
# new Scheduler(fct, delay, options)
Constructs a Scheduler.
Name | Type | Default | Description |
---|---|---|---|
fct |
function | The asynchronous function to call when the scheduler is triggered. |
|
delay |
number | The delay between two triggering of the scheduler, in ms. |
|
options |
object | null | (Optional) An object that can contain additional options:
|
- Source:
import { Scheduler, asyncRoot, sleep } from 'modern-async'
asyncRoot(async () => {
let i = 0
const scheduler = new Scheduler(async () => {
const taskNbr = i
i += 1
console.log(`Starting task ${taskNbr}`)
await sleep(10) // waits 10ms
console.log(`Ending task ${taskNbr}`)
}, 100) // a scheduler that triggers every 100ms
// the default configuration uses a maximum concurrency of 1 and doesn't allow pending
// tasks, which mean that if a task takes more time to complete than the delay it will be skipped
scheduler.start() // starts the scheduler
await sleep(1000) // waits 1s, during that time the task should trigger ~ 9 times
scheduler.stop() // stops the scheduler
console.log('Scheduler stopped')
// no "Starting task" console message may appear from here but you could still see a
// "Stopping task" as there could have a task that started before we stopped the
// scheduler
})
Members
# concurrency :number
(Read-only) The maximum number of asynchronous tasks that can run in parallel.
This parameter only matters in the event where some tasks may take more time to execute
than the delay. If the concurrency allows it the new task will be run concurrently. If not
it may be scheduled to be executed depending on the configuration of the maxPending
parameter.
Defaults to 1.
- number
- Source:
# delay :number
(Read-only) The delay between two triggering of the scheduler, in milliseconds.
- number
- Source:
# maxPending :number
(Read-only) The maximum number of tasks that can be pending.
In the event where one of the tasks triggered by the scheduler takes more time to execute than
the delay the next task may or may not be run concurrently depending on the configuration of
the concurrency
parameter. If the maximum concurrency was already reached the new task can
be scheduled to be executed as soon as the previous task finished.
This parameter indicates the maximum amount of tasks that can be pending at any time. If a task should be scheduled and the maximum amount of pending tasks is already reached that new task will be skipped.
This behavior helps to prevent cases that would lead to a infinite amount of tasks to be pending. This could happen in extreme cases where the tasks would take systematically more time to execute than the delay.
Defaults to 0.
- number
- Source:
# started :boolean
(Read-only) Whether or not the scheduler is actually started.
- boolean
- Source:
# startImmediate :boolean
(Read-only) Whether or not a triggering of the task should occur immediately when calling start()
or not.
Defaults to false.
- boolean
- Source:
Methods
# start()
Starts the scheduler.
Calling this method can trigger a task immediately depending on the configuration
of the startImmediate
parameter.
If this method is called while the scheduler is already started it will have no effect.
- Source:
# stop()
Stops the scheduler.
If, for any reason, there were pending tasks in the scheduler they will be cancelled. On the other hand if they are still one or more tasks that are running they will continue to run until they terminate.
This method is safe to call in a task if necessary.
If this method is called while the scheduler is already stopped it will have no effect.
- Source: