Job.Scheduler
3.1.8
dotnet add package Job.Scheduler --version 3.1.8
NuGet\Install-Package Job.Scheduler -Version 3.1.8
<PackageReference Include="Job.Scheduler" Version="3.1.8" />
<PackageVersion Include="Job.Scheduler" Version="3.1.8" />
<PackageReference Include="Job.Scheduler" />
paket add Job.Scheduler --version 3.1.8
#r "nuget: Job.Scheduler, 3.1.8"
#addin nuget:?package=Job.Scheduler&version=3.1.8
#tool nuget:?package=Job.Scheduler&version=3.1.8
Job Scheduler
A simple job scheduling library relying on the async/await pattern in C#.
Type of Jobs
One Time Job
By implementing the IJob
interface you tell the scheduler that you just want this job to be executed once and directly
upon being scheduled.
Recurring Job
By implementing the IRecurringJob
the scheduler will run indefinitely your job with the given delay between execution.
Delayed Job
By implementing the IDelayedJob
you tell the scheduler to wait a delay before executing your job.
Debounce Job
By implementing the IDebounceJob
you tell the scheduler to only run the latest encounter of the job sharing the same key.
Queue Job
You can register your own queue with their defined concurrency and schedule on them IQueueJob
.
Usage
I advise you to use a Dependency Injection (DI) engine (like SimpleInjector) to register the JobRunnerBuilder
and JobScheduler
as singleton.
Example:
public class MyJob : IRecurringJob
{
//Set the retry rule in case of failure of the job, in this case we want
//to retry the job 3 times
//Works for any type of job
public IRetryAction FailRule { get; } = new RetryNTimes(3);
//Optional MaxRuntime for the job before its canncellationToke get cancelled
//Keep in mind, this only cancel the token, we have no clean way of stopping a running task
//then cancelling the token.
public TimeSpan? MaxRuntime { get; } = TimeSpan.FromSeconds(5);
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
//Your complex recurring code, here pretty simple
await Console.Out.WriteLineAsync("Hello World");
}
public Task<IRetryAction> OnFailure(JobException exception)
{
//Any exception that occured when executing your job will be wrapped in a JobException, check the InnerException
//for you to be able to handle a failure without breaking your application neither needed a try/catch in ExecuteAsync
return Task.CompletedTask;
}
//This job will run every 15 seconds
public TimeSpan Delay { get; } = TimeSpan.FromSeconds(15);
}
var builder = new JobRunnerBuilder();
var scheduler = new JobScheduler(builder);
//If you have already a cancellation token that you want to be used for stopping your job, you can pass it as second param
scheduler.Start(new MyJob());
//At the end of your application, you can ask the Scheduler to gracefully stop the running jobs and wait for them to stop.
//You can also pass a cancellationToken to force a non graceful cancellation of the jobs.
await scheduler.StopAsync();
Advanced
You can also use your own TaskScheduler. It's useful if you want to control in which thread your task is run.
var builder = new JobRunnerBuilder();
var scheduler = new JobScheduler(builder);
var taskScheduler = new MyTaskScheduler();
// This way, this specific instance of the job will be run in your defined task scheduler
scheduler.Start(new MyJob(), CancellationToken.None, taskScheduler);
Queue usage
public class OneTimeQueueJob : IQueueJob
{
public bool HasRun { get; set; }
public IRetryAction FailRule { get; } = new NoRetry();
public TimeSpan? MaxRuntime { get; }
public virtual async Task ExecuteAsync(CancellationToken cancellationToken)
{
HasRun = true;
}
public Task OnFailure(JobException exception)
{
return Task.CompletedTask;
}
//Unique key for this job. The queue won't accept twice the same job unless it has finished running.
public string Key { get; set; } = "test";
//Unique ID of the queue
public string QueueId { get; set; } = "test";
}
var builder = new JobRunnerBuilder();
var scheduler = new JobScheduler(builder);
//queue with a maximum of 1 job running at a time
var settings = new QueueSettings("test", 1);
scheduler.RegisterQueue(settings);
//Schedule the job as normal, it will be schedule in the queue
scheduler.Start(new OneTimeQueueJob());
Disposable
If your job implement IAsyncDisposable
the disposing will be called when the job has finished running.
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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. |
-
.NETStandard 2.1
- JetBrains.Annotations (>= 2023.3.0)
- Polly.Contrib.WaitAndRetry (>= 1.1.1)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Job.Scheduler:
Package | Downloads |
---|---|
Job.Scheduler.AspNetCore
A simple job scheduling library relying on the async/await pattern in C#. Supports Recurring Jobs, Delayed Jobs and One Time Jobs. Helper for ASP.NET Core. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Job.Scheduler:
Repository | Stars |
---|---|
Belphemur/SoundSwitch
C# application to switch default playing device. Download: https://soundswitch.aaflalo.me/
|
Version | Downloads | Last updated | |
---|---|---|---|
3.1.8 | 3,737 | 12/14/2023 | |
3.1.7 | 183 | 12/14/2023 | |
3.1.6 | 1,651 | 2/9/2023 | |
3.1.5 | 433 | 2/9/2023 | |
3.1.4 | 435 | 2/9/2023 | |
3.1.3 | 469 | 2/5/2023 | |
3.1.2 | 454 | 2/5/2023 | |
3.1.1 | 931 | 11/27/2022 | |
3.1.0 | 556 | 11/27/2022 | |
3.0.2 | 1,206 | 10/5/2022 | |
3.0.1 | 770 | 10/4/2022 | |
3.0.0 | 749 | 10/4/2022 | |
2.9.0 | 1,603 | 7/13/2022 | |
2.8.0 | 763 | 7/12/2022 | |
2.7.3 | 1,922 | 5/9/2022 | |
2.7.2 | 717 | 5/9/2022 | |
2.7.1 | 999 | 4/17/2022 | |
2.7.0 | 727 | 4/16/2022 | |
2.6.0 | 758 | 4/16/2022 | |
2.5.1 | 753 | 4/16/2022 | |
2.5.0 | 1,125 | 10/5/2021 | |
2.4.1 | 420 | 9/28/2021 | |
2.4.0 | 417 | 9/27/2021 | |
2.3.0 | 1,051 | 7/27/2021 | |
2.2.2 | 506 | 7/15/2021 | |
2.2.1 | 550 | 5/22/2021 | |
2.2.0 | 598 | 5/15/2021 | |
2.1.1 | 433 | 4/13/2021 | |
2.1.0 | 441 | 4/8/2021 | |
2.0.2 | 438 | 3/22/2021 | |
2.0.1 | 450 | 3/22/2021 | |
2.0.0 | 413 | 3/19/2021 | |
1.1.0 | 590 | 12/23/2020 | |
1.0.1 | 657 | 10/24/2020 | |
1.0.0 | 666 | 10/24/2020 |