RecurrentTasks 6.6.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RecurrentTasks --version 6.6.0                
NuGet\Install-Package RecurrentTasks -Version 6.6.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RecurrentTasks" Version="6.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RecurrentTasks --version 6.6.0                
#r "nuget: RecurrentTasks, 6.6.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RecurrentTasks as a Cake Addin
#addin nuget:?package=RecurrentTasks&version=6.6.0

// Install RecurrentTasks as a Cake Tool
#tool nuget:?package=RecurrentTasks&version=6.6.0                

RecurrentTasks

This lightweight library allows you to run simple background tasks with specified intervals in your ASP.NET application.

Each task is a separate Task, which sleeps in background for a while, wakes up, perform some job and sleeps again.

Ideal, when you don't need to run many/heavy tasks and don't want to use "big" solutions with persistence and other bells and whistles.

Written for NET Core (support for ASP.NET 5 and ASP.NET Core 1.0 and 2.0 is dropped since v6, use v5.0.0 release if you need support for old frameworks).

Build status NuGet codecov

Main features

  • TargetFrameworks: netstandard2.0, netcoreapp3.1, net6.0, net8.0
  • Start and Stop your task at any time;
  • IHostedService implemented for NET Core 2.0 (and above) app lifetime support
  • CancelationToken may be used for Stopping;
  • First run (after Start) is delayed at random value (10-30 sec, customizable) to prevent app freeze during statup;
  • Run "immediately" (without waiting for next scheduled time);
  • Change run interval while running;
  • Single-execution-at-a-time: A task already running will wait before running again (timer for "next" run will start only after "current" run completes);
  • RunStatus property contains:
    • last/next run times;
    • last run result (success / exception);
    • last success run time;
    • last exception;
    • total failed runs counter.

Usage

1. Create new task class

public class MyFirstTask : IRunnable
{
    private ILogger logger;

    public MyFirstTask(ILogger<MyFirstTask> logger)
    {
        this.logger = logger;
    }
    
    public Task RunAsync(ITask currentTask, IServiceProvider scopeServiceProvider, CancellationToken cancellationToken)
    {
        // Place your code here
    }
}

You can add any parameters to constructor, while they are resolvable from DI container (including scope-lifetime services, because new scope is created for every task run).

By default, new instance of IRunnable is created for every task run, but you may change lifetime in AddTask (see below). Use IServiceProvider passed to RunAsync to obtain scope-wide services if you force your task be singleton.

2. Register and start your task in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTask<MyFirstTask>(o => o.AutoStart(TimeSpan.FromMinutes(5)));
    ...
}

And voila! Your task will run every 5 minutes. Until your application ends, of course.

AddTask adds your MyFirstTask to DI container with transient lifetime (new instance will be created for every task run). Pass desired lifetime to AddTask() to override:

services.AddTask<MyFirstTask>(
  o => o.AutoStart(TimeSpan.FromMinutes(5)),
  ServiceLifetime.Singleton)`.

Run immediately

Anywhere in you app:

// obtain reference to your task
var myTask = serviceProvider.GetService<ITask<MyFirstTask>>();

// poke it
if (myTask.IsStarted)
{
    myTask.TryRunImmediately();
}

Installation

Use NuGet package RecurrentTasks

Dependencies

  • Microsoft.Extensions.DependencyInjection.Abstractions
  • Microsoft.Extensions.Hosting.Abstractions
  • Microsoft.Extensions.Logging.Abstractions

All above: versions v2.0.0 / v3.1.0 / v6.0.0 / v8.0.0 according to TargetFramework used.

Testing

Tests can be run with dotnet test.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on RecurrentTasks:

Package Downloads
Logging.ExceptionSender

Catches all unhandled exceptions and sends email (or Telegram message) with details and stacktrace to you.

NetTelegramBot.Framework

Simple framework for building Telegram bots.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on RecurrentTasks:

Repository Stars
chsakell/aspnet-core-signalr-angular
Real-time applications using ASP.NET Core, SignalR & Angular
Version Downloads Last updated
7.0.1 3,851 4/26/2024
7.0.0-beta 119 3/5/2024
6.6.0 3,517 11/28/2023
6.5.0 19,775 1/21/2022
6.4.3 495 1/10/2022
6.4.2 3,622 9/6/2021
6.4.0 7,677 11/12/2020
6.3.0 7,020 12/16/2019
6.2.0 766 11/26/2019
6.1.0 11,773 8/12/2019
6.0.2 6,995 10/18/2018
5.0.0 21,418 2/14/2018
4.0.0 6,771 8/23/2017
3.2.0 6,407 2/7/2017
3.1.0 2,035 11/1/2016
3.0.0 1,906 10/31/2016
2.4.1 1,737 7/18/2016
2.4.0 1,501 7/15/2016
2.3.0 1,537 6/28/2016
2.2.0 1,587 5/24/2016
2.1.0 1,522 5/17/2016
2.0.0 1,107 2/5/2016
1.0.0 1,084 2/4/2016