Skip to content

This project contains the code for the nuget Package to get scheduled tasks in an asp.net core application.

License

Notifications You must be signed in to change notification settings

kirkone/KK.DotNet.BackgroundTasks.Scheduled

Repository files navigation

KK.DotNet.BackgroundTasks.Scheduled

This project contains the code for the NuGet Package to get scheduled tasks in an asp.net core application.

Usage

INFO: For detailed usage information please have a look in the KK.DotNet.BackgroundTasks.Scheduled.Web.Sample project.

Task

You can add multiple tasks in your projekt. The task has to implement the IScheduledTask interface.
The options for the task are provided by the DI system and you can request the options with IScheduledTaskOptions<SampleTask> as shown below.
The ExecuteAsync Method will be executed when the task is triggert.

public class SampleTask : IScheduledTask
{
    public SampleTask(
        IScheduledTaskOptions<SampleTask> options
    )
    {
        this.Options = options;           
    }

    public IScheduledTaskOptions<IScheduledTask> Options { get; }

    public async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // Stuff to do...
    }
}

INFO You shoud use the provided CancellationToken in your code to support cancelling of your task.

TIPP If you want to use Scoped Services inside have a look here: https://stackoverflow.com/a/58989132/9277073

You have to register the task in your public void ConfigureServices of the Startup.cs. You can use the provided AddScheduledTask Method:

services.AddScheduledTask<SampleTask>();

The Task will be added as a Singleton with this method.
If you does not want this you can also register it by yourself:

services.AddScoped<IScheduledTask, SampleTask>();

Options

The options are provided as an IScheduledTaskOptions of type YourSampleTask through the DI system

Property Description Default
Schedule A CronTab sting which describes the execution schedule nothing, must be set by the user. For the format have a look here
CronFormat A Cronos.CronFormat value, this must only be set when you ant to use seconds in your schedule Cronos.CronFormat.Standard, more information here

In your public void ConfigureServices of the Startup.cs you can add options like so:

services.AddSingleton<IScheduledTaskOptions<SampleTask>>(
    new ScheduledTaskOptions<SampleTask>
    {
        Schedule = "*/10 * * * * *",
        CronFormat = Cronos.CronFormat.IncludeSeconds
    }
);

SchedulerHost

After the registration of all your tasks there is only one thing left to do.
Register the SchedulerHostedService like so:

services.AddHostedService<SchedulerHostedService>();

NuGet

Feed Name Status
NuGet.org KK.DotNet.BackgroundTasks.Scheduled NuGet Badge
Azure DevOps KK.DotNet.BackgroundTasks.Scheduled KK.DotNet.BackgroundTasks.Scheduled package in KK.DotNet.BackgroundTasks.Scheduled feed in Azure Artifacts

You can add the package for example with the following dotnet command:

dotnet add package KK.DotNet.BackgroundTasks.Scheduled

Pre-releases of this Package are pushed to an internal feed an Azure DevOps. There is no public access to this feeds at the moment.

The build environment for this project is on Azure DevOps and can be found here dev.azure.com/kirkone/KK.DotNet.BackgroundTasks.Scheduled

Build

Name Status
KK.DotNet.BackgroundTasks.Scheduled CI Build Status

Release

Name Status
KK.DotNet.BackgroundTasks.Scheduled CD
Alpha Alpha
Beta Beta
Release Release

Authors

  • Kirsten Kluge - Initial work - kirkone
  • paule96 - Refactoring and understanding awesome interface stuff - paule96
  • TiltonJH - Unbelievable knowledge about awesome interface stuff - TiltonJH

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

About

This project contains the code for the nuget Package to get scheduled tasks in an asp.net core application.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published