Skip to content

Task Scheduler

This page introduces the KXS Task Scheduler service class, which manages background and time-dependent recurring task execution.

KX Sensors includes a core service called the Task Scheduler, which manages the execution of background and time-dependent operations. It provides a flexible and reliable mechanism for scheduling and executing recurring tasks.

Tasks are launched as NTASKs from the Task Scheduler service class. This service class should be configured within your node manifest:

services:
  - sc: TS
    enabled: true
    portOffset: 0
    num: 1
    prefix: ""
    threads: 0
    taskset: ""
    tls: 0
    auth: false

Once the Task Scheduler service is started and tasks are properly defined in configuration, NTASKs are launched automatically at their configured times. If a task fails to start, the framework automatically retries it.

Configuration

Scheduled tasks are configured via the file config/taskScheduler.yaml.

The parameters for controlling scheduled tasks are defined in the meta file meta/taskScheduler.yaml:

taskSchedule:
  m-description: Configures the KX Sensors task scheduler service
  values:
    m-type: dict
    m-required: true
    m-value:
      startTS:
        m-type: timestamp
        m-description: Time at which to dispatch the scheduled task, in UTC
        m-required: true
      freq:
        m-type: integer
        m-description: Frequency at which to dispatch the scheduled task, in minutes (must divide a day evenly or be a whole number of days; invalid values are rounded up)
        m-required: true
      load:
        m-type: symbols
        m-description: Scripts to load on the scheduled task
        m-required: true
      init:
        m-type: symbol
        m-description: Monadic initialization function to run on the scheduled task
        m-required: true
      enabled:
        m-type: boolean
        m-description: Indicates whether or not the scheduled task is enabled
        m-required: true

The Task Scheduler periodically checks whether tasks are ready to dispatch. This period is defined in config/systemParams.yaml:

- name: tsChkFreq
  type: int
  description: Interval (in seconds) used to check if scheduled tasks are ready to dispatch
  value: 60
  dynamic: false

Sample configuration

The following example shows a configuration bundled with the core product. It defines the archiver task, which performs compression and deletion of aged KX Sensors application log files:

taskSchedule:
  values:
    <default>:
      startTS: 00:00:00
      freq: 1440
      enabled: true
    archiver:
      load: [ src/task/archiver.q ]
      init: .archiver.run

In this configuration:

  • The <default> key defines default values for all tasks, including:
    • startTS: tasks run at UTC midnight.
    • freq: tasks run once daily.
    • enabled: tasks are enabled.
  • The archiver task:
    • Loads the script src/task/archiver.q.
    • Executes the function .archiver.run when dispatched.

Configure task frequency

Changes in v3.3.1

freq supports intervals longer than a day.

For frequencies longer than a day, startTS also sets the date, and tasks dispatch on multiples of the frequency counted from startTS. Include the date in startTS, because a time-only value doesn't set the run days. If startTS is in the past, the task runs at the next scheduled time after the current time, not immediately. For frequencies of a day or less, only the time of day in startTS is used.

A frequency that is longer than a day must be a whole number of days, and a frequency of a day or less must divide a day evenly. When freq doesn't meet these rules, the scheduler rounds it up to the next valid frequency. For example, a freq of 10081 (one week plus one minute) becomes 11520 (eight days). Frequencies are fixed numbers of minutes, so a "monthly" schedule runs every 30 days (43200) rather than on a calendar month.

Next steps