Skip to content

2.11 Scheduled tasks

Marjan Nikolovski edited this page Apr 9, 2021 · 1 revision

Purpose

A recurring task is a task which is set to automatically repeat for the same Client based on a pattern you create.

Signals offers an implementation for the Scheduled tasks aspect out of the box:

  • Signals.Aspects.BackgroundProcessing.FluentScheduler

Only one implementation can be active at a time.

How to use

When we implement a process of types RecurringProcess or NoOverlapRecurringProcess, a task scheduling registry automatically registers all those processes. RecurrencePatternConfiguration has multiple implementations:

  • TimePartRecurrencePatternConfiguration: recurrence defined in TimeSpan less than 1 day
  • DailyRecurrencePatternConfiguration: recurrence defined in number of days
  • WeeklyRecurrencePatternConfiguration: recurrence defined in number of weeks
  • WorkdayRecurrencePatternConfiguration: recurrence defined in workdays of week
  • WeekendRecurrencePatternConfiguration: recurrence defined in weekend days of week
  • MonthlyRecurrencePatternConfiguration: recurrence defined in number of months
  • MonthlyNamedRecurrencePatternConfiguration: recurrence defined in number of months, in specific ordered day of week
public class MyRecurringProcess : NoOverlapRecurringProcess<VoidResult>
{
    public override RecurrencePatternConfiguration Profile => new DailyRecurrencePatternConfiguration(1).At(1, 00, 00);

    /// <summary>
    /// Handle recurring process
    /// </summary>
    /// <returns></returns>
    public override VoidResult Sync()
    {
        return Ok();
    }
}

Configuration

We configure the communication channel aspect by using an instance of Signals.Aspects.BackgroundProcessing.ITaskRegistry which we pass in the ApplicationBootstrapConfiguration instance (web or background) at startup.

Examples

Using Signals.Aspects.BackgroundProcessing.FluentScheduler

services
    .AddSignals(config =>
    {
        config.TaskRegistry = new FluentRegistry();
    });

Extending scheduled tasks

  1. Install package Signals.Aspects.BackgroundProcessing
  2. Create class with implementation of Signals.Aspects.BackgroundProcessing.ITaskRegistry
/// <summary>
/// Task registry
/// </summary>
public class MyTaskRegistry : ITaskRegistry
{
    /// <summary>
    /// Schedule task instance
    /// </summary>
    /// <param name="task"></param>
    /// <param name="config"></param>
    void ScheduleTask(ISyncTask task, RecurrencePatternConfiguration config);

        /// <summary>
        /// Schedule task by type
        /// </summary>
        /// <param name="config"></param>
        void ScheduleTask<TTask>(RecurrencePatternConfiguration config) where TTask : ISyncTask;

    /// <summary>
    /// Start task execution for all scheduled tasks
    /// </summary>
    void Start();

    /// <summary>
    /// Stop task execution for all scheduled tasks and remove them
    /// </summary>
    void Stop();
}
  1. Use our implementation of ITaskRegistry when configuring our application
public static IServiceProvider AddSignals(this IServiceCollection services)
{
    services
        .AddSignals(config =>
        {
            config.TaskRegistry = new MyTaskRegistry();
        });
}
Clone this wiki locally