Scheduler

Scheduler

A class implementing a scheduler.

It fills the same purpose than setInterval() but its behavior is more adapted to asynchronous tasks. Notably it can limit the concurrency of asynchronous tasks running in parallel.


Constructor

# new Scheduler(fct, delay, options)

Constructs a Scheduler.

Parameters:
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:

  • startImmediate: If true a new task will be triggered as soon as the start() method is called. Defaults to ´false`.
  • concurrency: The maximum number of concurrent tasks. See the concurrency attribute. Defaults to 1.
  • maxPending: The maximum number of pending tasks. See the maxPending attribute. Defaults to 0.
Example
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.

Type:
  • number

# delay :number

(Read-only) The delay between two triggering of the scheduler, in milliseconds.

Type:
  • number

# 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.

Type:
  • number

# started :boolean

(Read-only) Whether or not the scheduler is actually started.

Type:
  • boolean

# startImmediate :boolean

(Read-only) Whether or not a triggering of the task should occur immediately when calling start() or not.

Defaults to false.

Type:
  • boolean

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.

# 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.