dotnetasyncscheduler - DotnetAsyncScheduler 0.0.2-alpha.0.11

Task scheduler for .NET Core / Full

PM> Install-Package DotnetAsyncScheduler -Version 0.0.2-alpha.0.11 -Source https://www.myget.org/F/dotnetasyncscheduler/api/v3/index.json

Copy to clipboard

> nuget.exe install DotnetAsyncScheduler -Version 0.0.2-alpha.0.11 -Source https://www.myget.org/F/dotnetasyncscheduler/api/v3/index.json

Copy to clipboard

> dotnet add package DotnetAsyncScheduler --version 0.0.2-alpha.0.11 --source https://www.myget.org/F/dotnetasyncscheduler/api/v3/index.json

Copy to clipboard
<PackageReference Include="DotnetAsyncScheduler" Version="0.0.2-alpha.0.11" />
Copy to clipboard
source https://www.myget.org/F/dotnetasyncscheduler/api/v3/index.json

nuget DotnetAsyncScheduler  ~> 0.0.2-alpha.0.11
Copy to clipboard

> choco install DotnetAsyncScheduler --version 0.0.2-alpha.0.11 --source https://www.myget.org/F/dotnetasyncscheduler/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "dotnetasyncscheduler" -SourceLocation "https://www.myget.org/F/dotnetasyncscheduler/api/v2"
Install-Module -Name "DotnetAsyncScheduler" -RequiredVersion "0.0.2-alpha.0.11" -Repository "dotnetasyncscheduler" -AllowPreRelease
Copy to clipboard

Browse the sources in this package using Visual Studio or WinDbg by configuring the following symbol server URL: https://www.myget.org/F/dotnetasyncscheduler/api/v2/symbolpackage/


DotNetAsyncScheduler

Releases

Prerelease versions from Master

CI-Release Build status available in MyGet-Feed

Requirements / Dependencies

  • .NET Core 2.0 or .NET Framework 4.7.2

  • Microsoft.Extensions.DependencyInjection.Abstractions

  • Microsoft.Extensions.Logging.Abstractions

  • JetBrains.Annotations

Implementation Goals

  • Simple triggering of async Jobs based on Schedules
  • High integration with Dependency Injection for Jobs and Schedules via Microsoft.Extensions.DependencyInjection.Abstractions
  • Split from Jobs and Schedules
  • Allow implementing of restrictions (e.g. Mutex, Slow-Start)
  • Use async-await and CancellationToken
  • One job cannot run in parallel
  • Intelligent error handling (e.g. allow immediate restart in Scheduler)
  • Allow stopping / restarting and chaning of jobs/schedules on the fly
  • Use logging via Microsoft.Extensions.Logging.Abstractions

What we don't have:

  • Any kind of persistence of job runs or history (e.g. in Database)
  • No serialization (yeay) of jobs or job parameters
  • Jobs with parameters

Execution model

In each cycle (cycle delay can be configured), the Scheduler pulls all Schedules of jobs, which are currently not running. If a schedule returns a priority number greater than 0, the job is marked for execution. The marked jobs are then ordered by the priority number. One after the other, the jobs are started, when the job restrictions allow this.

Example

Scheduler

//setup your DI
var serviceProvider = new ServiceCollection()
    .RegisterAsyncScheduler()
    .BuildServiceProvider();

var scheduler = serviceProvider.GetService<Scheduler>();
if (scheduler == null)
{
    throw new NullReferenceException("Unable to get Scheduler");
}

var jobManager = scheduler.JobManager;

jobManager.AddJob<SimpleTask>(new TimeSlotSchedule
{
    StartTime = DateTime.UtcNow + TimeSpan.FromSeconds(20)
});
jobManager.AddJob<ExampleTask2>(new IntervalSchedule(TimeSpan.FromSeconds(25)));
jobManager.AddJob<DownloadTask>(new IntervalSchedule(TimeSpan.FromSeconds(10)));
jobManager.AddJob<EndlessLoopTask, ScheduleOnce>();
scheduler.AddRestriction(new ConcurrentJobRestriction
{
    MaximumParallelJobs = 10
});
scheduler.AddRestriction(new MutexRestriction(typeof(SimpleTask), typeof(DownloadTask)));

var schedulerTask = scheduler.Start(_cancellationTokenSource.Token);
await schedulerTask;

Hint: There is no stop method. Use the cancellationToken to stop the scheduling.

Job

public class DownloadTask : IJob
{
    private readonly ILogger<DownloadTask> _logger;

    public DownloadTask(ILogger<DownloadTask> logger)
    {
        _logger = logger;
    }

    public async Task<object> Start(CancellationToken cancellationToken)
    {
        var httpClient = new HttpClient();
        _logger.LogInformation("Starting download ...");
        await httpClient.GetAsync("https://www.google.com", cancellationToken);
        _logger.LogInformation("Download finished ...");
        return "Download success";
    }
}

IJob

A job should highly use async methods to avoid blocking other jobs. If the job's main action uses not-async CPU intensive work, consider to spawn a new task/thread via Task.Run inside your Job.

The job may end with an exception in case of errors. The return value is logged using ToString().

Schedules

Schedules define, when a Job is executed: If schedule returns int>0, the Job is started (when no restrictions apply). Jobs with higher numbers are started first, which is only relevant, when Restrictions (e.g. Mutex, MaxJobNumber) are applied. A Schedule might handle retry in case of failures.

As Schedules can be created via DI, they may connect to a database / config file to read current configuration state. In case of IO-Operations in Schedule consider increasing the LoopDelay.

Predefined Schedules

There are several examples of schedule implementation available:

  • ScheduleOnce: Schedule immediately, when not executed or failed
  • ScheduleOnceWithRetryDelay: Schedule immediately. Retry on failure with certain configurable delay
  • ScheduleEndless: Always re-schedule, when finished
  • IntervalSchedule: Schedule based on a TimeSpan interval
  • IntervalScheduleWithRetryDelay: Same as IntervalSchedule, but with retry for failed jobs
  • TimeSlotSchedule: Schedule based on a Datetime after which it should start

Restrictions

Restrictions can be used to prevent Jobs from being started based on the current context (other running jobs). They are executed shortly before a Job is started.

Feel free to implement your own restrictions.

Predefined Restrictions

  • ConcurrentJobRestriction: Restrict number of parallel jobs
  • MutexRestriction: Restrict certain jobs from running in parallel
  • SlowStartRestriction: Don't start all jobs at once (experimentell)

Disclaimer

Use at your own risk.

Alternatives

  • Hangfire
  • Quartz.NET
  • Fluentscheduler
  • .NETFramework 4.7.2
    • JetBrains.Annotations (>= 2020.1.0)
    • Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.6)
    • Microsoft.Extensions.Logging.Abstractions (>= 3.1.6)
  • .NETCoreApp 2.1
    • JetBrains.Annotations (>= 2020.1.0)
    • Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.6)
    • Microsoft.Extensions.Logging.Abstractions (>= 3.1.6)
  • .NETCoreApp 2.1: 2.1.0.0
  • .NETFramework 4.7.2: 4.7.2.0

Owners

Brunni

Authors

Michael Brunner

Project URL

https://github.com/Brunni/DotNetAsyncScheduler

License

Unknown

Info

60 total downloads
4 downloads for version 0.0.2-alpha.0.11
Download (34.31 KB)
Download symbols (18.49 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
1.0.1-alpha.0.1 42.14 KB Thu, 20 Oct 2022 17:42:49 GMT 4
1.0.0 40.15 KB Sat, 26 Dec 2020 19:41:14 GMT 5
0.1.2-alpha.0.7 40.21 KB Tue, 22 Dec 2020 11:12:21 GMT 3
0.1.2-alpha.0.6 39.66 KB Wed, 16 Dec 2020 16:17:25 GMT 2
0.1.2-alpha.0.5 36.49 KB Tue, 15 Dec 2020 16:47:48 GMT 2
0.1.2-alpha.0.4 34.97 KB Mon, 14 Dec 2020 20:24:27 GMT 2
0.1.2-alpha.0.3 34.98 KB Sun, 13 Dec 2020 22:59:56 GMT 2
0.1.2-alpha.0.2 34.89 KB Sun, 13 Dec 2020 22:52:13 GMT 2
0.1.2-alpha.0.1 34.62 KB Sun, 13 Dec 2020 22:23:58 GMT 5
0.0.2-alpha.0.14 34.63 KB Sun, 13 Dec 2020 22:18:26 GMT 3
0.0.2-alpha.0.13 34.63 KB Sun, 13 Dec 2020 22:16:40 GMT 3
0.0.2-alpha.0.12 34.31 KB Sun, 13 Dec 2020 22:01:52 GMT 2
0.0.2-alpha.0.11 34.31 KB Sun, 13 Dec 2020 21:53:55 GMT 4
0.0.2-alpha.0.10 34.31 KB Sun, 13 Dec 2020 21:44:55 GMT 2
0.0.2-alpha.0.9 32.69 KB Sun, 29 Nov 2020 19:33:38 GMT 5
0.0.2-alpha.0.8 32.41 KB Sun, 29 Nov 2020 18:22:10 GMT 2
0.0.2-alpha.0.7 30.64 KB Sun, 29 Nov 2020 17:51:02 GMT 2
0.0.2-alpha.0.6 30.65 KB Fri, 28 Aug 2020 22:28:41 GMT 2
0.0.0-alpha.0.18 30.63 KB Fri, 28 Aug 2020 22:12:24 GMT 3
0.0.0-alpha.0.17 30.65 KB Fri, 28 Aug 2020 22:10:04 GMT 3
0.0.0-alpha.0.12 30.69 KB Fri, 28 Aug 2020 15:47:10 GMT 2