MHanafy.Scheduling 1.0.0

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

// Install MHanafy.Scheduling as a Cake Tool
#tool nuget:?package=MHanafy.Scheduling&version=1.0.0                

Scheduler

A .Net scheduler that handles parallaization and adaptive sleeping on inactivity, can be used to develop long running workers and is specially suited for pulling scenarios

Scheduler takes care of two things:

  1. Running a task in parallel, resolving the instance for each thread
  2. Sleeping in a progressive fashion when no processing is needed; i.e. when a worker process finds no items to process

This helps building worker roles that need to pull periodically, the sleeping pattern would minimize the impact of the server allowing for more efficient execution.

The level of concurrency and the sleeping behaviour are configurable, To get started, just create a scheduler and either call Start() or StartAndBlock()

Scheduler is designed to be used via DI, and using the Options pattern

var scheduler = serviceProvider.GetService<IScheduler>();
//Since we're using Start, the code won't block, and scheduler will keep executing as long as it's in scope
scheduler.Start(async () =>
{
    Console.WriteLine($"{DateTime.Now:hh:mm:ss.ff} Settings-based scheduler");
    //Simulate some work
    await Task.Delay(5000);
    return true;
});

We can still create a scheduler without using the service provider, a service provider is still needed to resolve types when calling Start<T>

var options = new OptionsWrapper<SchedulerSettings>(new SchedulerSettings
{
    ImmediateResume = true, //causes the scheduler to immediately call the function again when it returns true
    Concurrency = 1, //runs the function on a single thread
    InitialSleepTime = 100, //when the function returns false, scheduler will wait for 100ms before calling it again; this number is then multiplied until MaxSleepTime is reached
    MaxSleepTime = 450 //time between calls can't exceed this value, in this case if the function keeps returning false wait times would be 100, 200, 400, then 450 instead of 800
});

var blockingScheduler = new Scheduler(options, serviceProvider);
//This is a blocking call, so the program will keep executing and any code beyond this call won't be reached until the program is terminated
blockingScheduler.StartAndBlock<Test>(async (Test t) =>
{
    //here we're using the instance resolved by the scheduler for this thread.
    Console.WriteLine($"{DateTime.Now:hh:mm:ss.ff} Blocking scheduler Test instance: {t.Guid}");
    //wait for a bit
    await Task.Delay(2000);
    //returning false will cause the scheduler to call the function immediately if ImmediateResume is set to true, otherwise calls after waiting for InitialSleepTime
    return true;
});

Have a look at the samples project for more information

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.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

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.0 665 2/25/2020