IegTools.FluentPollster 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package IegTools.FluentPollster --version 2.1.0                
NuGet\Install-Package IegTools.FluentPollster -Version 2.1.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="IegTools.FluentPollster" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IegTools.FluentPollster --version 2.1.0                
#r "nuget: IegTools.FluentPollster, 2.1.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 IegTools.FluentPollster as a Cake Addin
#addin nuget:?package=IegTools.FluentPollster&version=2.1.0

// Install IegTools.FluentPollster as a Cake Tool
#tool nuget:?package=IegTools.FluentPollster&version=2.1.0                

IegTools.FluentPollster

FluentPollster provides a user-friendly fluent interface for creating easy-to-read polling tasks.
The library is written in C# 11.0 and targets .NET Standard 2.0 (.NET Core and .NET Framework).

Topics

Why another polling library?
Usage
Additional settings
ExtensionMethods
Breaking Changes

Why another polling library?

I need polling for two scenarios

  • get data from an third party Api
  • get Temperatures from 1-Wire Bus DS18B20 sensors

I found that FluentScheduler didn't meet my requirements.
I had some ideas and well it's less than 500 loc at the moment, so that's nothing.
Hey and it's fun.

Usage

To get into the details, explore the Integration-Tests.

Simple polling job

You can add any Action as job to the PollsterBuilder,
specify the poll-interval and the condition,
build the Pollster and run it.

private IPollster _pollster;

public void SimplePolling()
{
    var counter = 0;
    var uut = PollsterBuilder.Create()
        .AddJob(() => counter++, TimeSpan.FromMilliseconds(100), () => HasWhatSoEverCondition());

    _pollster = uut.Build();
    
    // executes only once, so when and how you call it belongs to you
    _pollster.Execute();
}

Execute the pollster automatically every x minutes, seconds,...

You can add Actions also without any condition,
build the Pollster and run it in automatic-mode.

private IPollster _pollster;

public void AutomaticPolling()
{
    var counter = 0;
    var uut = PollsterBuilder.Create()
        .AddJob(() => counter++, TimeSpan.FromSeconds(5));

    _pollster = uut.Build();
    
    // run automatic (like FluentScheduler does it...)
    _pollster.RunAutomaticEvery(TimeSpan.FromSeconds(10));
}

Multiple interval polling job

You can add any Action with multiple interval/condition combinations as job to the PollsterBuilder,
build the Pollster and run it.

private IPollster _pollster;

public void MultipleIntervalPolling()
{
    var counter = 0;

    var intervals = new List<(TimeSpan, Func<bool>)>
    {
        (TimeSpan.FromSeconds(60),  () => Condition1()),
        (TimeSpan.FromSeconds(120), () => Condition2()),
    };

    var uut = PollsterBuilder.Create()
        .AddJob(() => counter++, intervals);

    var pollster = uut.Build();

    _pollster.Execute();
}

Additional settings

Logger

The FluentPollster does not require dependency injection, however, it provides the option to inject an ILogger so that you can receive notifications about any exceptions that may occur during your poll actions.
Additionally, all other significant events are currently being logged as 'Trace'.

Maximum tasks per poll cycle

And you can set the maximum number of poll tasks that should be executed during one poll cycle.

    _pollster = PollsterBuilder.Create()
        .SetLogger(logger)
        .SetMaxJobsPerPoll(10)
        .AddJob(() => Job1(), TimeSpan.FromSeconds(5));
        .AddJob(() => Job2(), TimeSpan.FromSeconds(10));
        .Build();

    _pollster.RunAutomaticEvery(TimeSpan.FromSeconds(1));

ExtensionMethods

IsMinuteDivisibleBy

    // ensures the polling task executes solely at specific minutes within an hour.
    IsMinuteDivisibleBy(this DateTime time, int everyMinutes, int offsetMinute = 0)

Examples:

  • Use DateTime.Now.IsMinuteDivisibleBy(5) as condition and your action will be called only in minute 0, 5, 10, 15...

  • Use DateTime.Now.IsMinuteDivisibleBy(15) as condition and your action will be called only in minute 0, 15, 30 and 45

  • Use DateTime.Now.IsMinuteDivisibleBy(15, 1) as condition and your action will be called only in minute 1, 16, 31 and 46

For additional details, please refer to IntegrationTests.

Breaking Changes

v1 to v2

  • switched from net7.0 to netstandard2.0 (there is no PeriodicTimer in netstandard2.0...)
  • rename method poller.Run...() to poller.Execute...()
  • rename method poller.StopAsync() to poller.Stop()
  • rename ExtensionMethod IsCurrentMinuteDivisibleBy(...) to IsMinuteDivisibleBy(...)
  • change behaviour of ExtensionMethod IsMinuteDivisibleBy(...) offsetMinute
    from (time.Minute + offsetMinute) to (time.Minute - offsetMinute)
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
2.2.0 277 10/17/2023
2.1.1 180 5/3/2023
2.1.0 161 5/2/2023
2.0.0 199 4/17/2023
1.1.0-rc.1 97 4/15/2023
1.0.1 189 4/5/2023
1.0.0 211 4/5/2023
1.0.0-beta.3 112 4/4/2023
1.0.0-beta.2 98 4/4/2023
1.0.0-beta.1 98 4/3/2023

switched to netstandard2.0, some breaking changes