Skip to main content

IMoBroScheduler

The IMoBroScheduler schedules one-off and recurring tasks, such as polling sensor values or fetching data from an external API.

Behavior​

Keep the following in mind when scheduling tasks:

  • Tasks run on background threads. Different tasks may run at the same time, so state shared between tasks must be thread-safe.
  • A task never overlaps with itself. If an execution takes longer than its interval, the next execution is delayed until the current one has finished. A slow task therefore lowers the effective update rate instead of piling up. Cron tasks skip executions they missed while the previous one was still running.
  • An unhandled exception stops the plugin. Any exception escaping a scheduled task is reported via IMoBroService.Error, which stops the plugin and sets its state to Error. Catch expected failures (e.g. a temporarily unavailable sensor or API) inside the task.
  • Tasks are synchronous. All functions accept an Action. To call asynchronous code, block on it inside the task (see example).
  • All tasks are removed when the plugin is re-created, e.g. after the user changes the plugin settings. Init() then runs again on the new instance, so tasks don't need to be re-scheduled manually. See Plugin Lifecycle.

Example: Failure-Tolerant Task​

_scheduler.Interval(UpdateMetrics, TimeSpan.FromSeconds(2), TimeSpan.Zero);

private void UpdateMetrics()
{
try
{
_mobro.UpdateMetricValue("cpu_usage", ReadCpuUsage());
}
catch (IOException e)
{
// a single failed read should not stop the plugin => log and try again on the next execution
_logger.LogWarning(e, "Failed to read CPU usage");
}
}

Calling Asynchronous Code​

_scheduler.Interval(() => UpdateRatesAsync().GetAwaiter().GetResult(), TimeSpan.FromMinutes(5), TimeSpan.Zero);

Functions​

OneOff(Action, TimeSpan)​

Schedules a task that is executed only once.

Parameters​

NameTypeDescription
actionActionThe action to execute.
delayTimeSpanThe delay before the execution.

Example​

_scheduler.OneOff(LoadInitialData, TimeSpan.FromSeconds(2));

Interval(Action, TimeSpan, TimeSpan)​

Schedules a recurring task that is executed at a fixed interval.

Parameters​

NameTypeDescription
actionActionThe action to execute.
intervalTimeSpanThe interval between executions.
delayTimeSpanThe delay before the first execution.

Example​

// first execution after 5 seconds, then every 2 seconds
_scheduler.Interval(UpdateMetrics, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5));

Cron(Action, string, TimeZoneInfo, TimeSpan)​

Schedules a recurring task based on a cron expression.
The expression uses the Quartz format, which starts with a seconds field. See the Quartz cron documentation for details.

Parameters​

NameTypeDescription
actionActionThe action to execute.
cronstringThe cron expression.
timeZoneTimeZoneInfoThe time zone the cron expression is evaluated in.
delayTimeSpanThe delay before the cron schedule is activated.

Example​

// every 15 minutes
_scheduler.Cron(UpdateExchangeRates, "0 0/15 * * * ?", TimeZoneInfo.Local, TimeSpan.Zero);

Pause()​

Temporarily pauses all scheduled tasks, e.g. while an external dependency is unavailable.


Resume()​

Resumes all previously paused tasks.


Clear()​

Removes all scheduled tasks. Executions that are currently running are not interrupted.

Example​

// switch to a different polling interval
_scheduler.Clear();
_scheduler.Interval(UpdateMetrics, TimeSpan.FromSeconds(10), TimeSpan.Zero);