SimpleCronWorkerService 1.0.1

dotnet add package SimpleCronWorkerService --version 1.0.1                
NuGet\Install-Package SimpleCronWorkerService -Version 1.0.1                
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="SimpleCronWorkerService" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleCronWorkerService --version 1.0.1                
#r "nuget: SimpleCronWorkerService, 1.0.1"                
#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 SimpleCronWorkerService as a Cake Addin
#addin nuget:?package=SimpleCronWorkerService&version=1.0.1

// Install SimpleCronWorkerService as a Cake Tool
#tool nuget:?package=SimpleCronWorkerService&version=1.0.1                

SimpleCronWorkerService

NuGet Release - Build, pack and publish NuGet License: MIT

A simple and easy way to implement worker services scheduled by a CRON expression

Getting started

Package Install

Installing is as easy as: dotnet add package SimpleCronWorkerService or Install-Package SimpleCronWorkerService depending on your setup.

Create your CronWorkerService

  1. Create your Worker class which must inherit from the abstract class CronWorkerService imported from SimpleCronWorkerService

    using SimpleCronWorkerService;
    
    namespace WorkerServiceSample
    {
        public class Worker : CronWorkerService
        {
        }
    }
    
  2. In the constructor, as the first parameter, you will receive a CronWorkerServiceSettings<> object as the type of your worker class. These settings must be passed to the base constructor : base(cronWorkerServiceSettings).

    using SimpleCronWorkerService;
    
    namespace WorkerServiceSample
    {
        public class Worker : CronWorkerService
        {
            private readonly ILogger<Worker> _logger;
    
            public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger)
                :base(cronWorkerServiceSettings)
            {
                _logger = logger;
            }
        }
    }
    
  3. You have to override the method protected override Task DoWork(CancellationToken stoppingToken) with the logic that you want your Worker to execute.

    using SimpleCronWorkerService;
    
    namespace WorkerServiceSample
    {
        public class Worker : CronWorkerService
        {
            private readonly ILogger<Worker> _logger;
    
            public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger)
                :base(cronWorkerServiceSettings)
            {
                _logger = logger;
            }
    
            protected override Task DoWork(CancellationToken cancellationToken)
            {
                // ... Your logic
            }
        }
    }
    

Example

using SimpleCronWorkerService;

namespace WorkerServiceSample
{
    public class Worker : CronWorkerService
    {
        private readonly ILogger<Worker> _logger;

        public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger)
            :base(cronWorkerServiceSettings)
        {
            _logger = logger;
        }

        protected override Task DoWork(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Running... at {0}", DateTime.UtcNow);

            return Task.CompletedTask;
        }
    }
}

Register your CronWorkerService

In your service container, you can add your worker using SimpleCronWorkerService with the method Services.AddCronWorkerService<>

The type <> should be your Worker class.

using SimpleCronWorkerService;
 ...
// Add services to the container.
builder.Services.AddCronWorkerService<Worker>(options =>
{
    // Run every minute
    options.CronExpression = @"* * * * *";
    options.TimeZone = TimeZoneInfo.Local;
});

Inside this method, the options are passed, these options are of type CronWorkerServiceSettings<T>

public class CronWorkerServiceSettings<T> : ICronWorkerServiceSettings where T : CronWorkerService
{
    public string CronExpression { get; set; } = @"* * * * *";
    public TimeZoneInfo TimeZone { get; set; } = TimeZoneInfo.Utc;
    public bool CronExpressionIncludeSeconds { get; set; } = false;
}

The CronExpression is a string and we are using the Cronos library. For more information about this syntax, please visit the Cronos documentation. By default, it is the expression for every minute ("* * * * *").

The TimeZone sets the time zone in which you want your worker to run. By default, it is UTC.

The CronExpressionIncludeSeconds is a boolean used if you are going to use the seconds part of the expression (Cronos documentation). By default, it is false.

Samples

Contributing

Please fork this repo then create a PR from the fork into the original one.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 624 5/11/2023
1.0.0 169 5/11/2023
0.0.3 159 5/10/2023
0.0.2 157 5/10/2023