Philiprehberger.Scheduler
0.2.1
dotnet add package Philiprehberger.Scheduler --version 0.2.1
NuGet\Install-Package Philiprehberger.Scheduler -Version 0.2.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="Philiprehberger.Scheduler" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.Scheduler" Version="0.2.1" />
<PackageReference Include="Philiprehberger.Scheduler" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.Scheduler --version 0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Philiprehberger.Scheduler, 0.2.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.
#:package Philiprehberger.Scheduler@0.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.Scheduler&version=0.2.1
#tool nuget:?package=Philiprehberger.Scheduler&version=0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.Scheduler
Lightweight in-process job scheduler with cron expressions, timezone support, execution history, and lifecycle callbacks.
Installation
dotnet add package Philiprehberger.Scheduler
Usage
Define a Job
using Philiprehberger.Scheduler;
public class CleanupJob : IScheduledJob
{
public async Task ExecuteAsync(CancellationToken ct)
{
// Clean up old records
await Task.CompletedTask;
}
}
Register with DI
using Philiprehberger.Scheduler;
builder.Services.AddScheduler(options =>
{
options.AddJob<CleanupJob>("*/5 * * * *"); // every 5 minutes
});
Using Attributes
using Philiprehberger.Scheduler;
[ScheduledJob("cleanup", "*/5 * * * *", TimeZone = "America/New_York")]
public class CleanupJob : IScheduledJob
{
public async Task ExecuteAsync(CancellationToken ct)
{
// Runs every 5 minutes in Eastern time
await Task.CompletedTask;
}
}
Timezone-Aware Scheduling
using Philiprehberger.Scheduler;
builder.Services.AddScheduler(options =>
{
// Evaluate cron in a specific timezone instead of UTC
options.AddJob<ReportJob>("0 9 * * 1-5", timeZone: "Europe/London");
});
One-Time Scheduled Jobs
using Philiprehberger.Scheduler;
builder.Services.AddScheduler(options =>
{
options.ScheduleOnce("send-welcome", async ct =>
{
// Runs once at the specified time, then auto-removes
await SendWelcomeEmailAsync(ct);
}, DateTimeOffset.UtcNow.AddHours(1));
});
Job Execution History
using Philiprehberger.Scheduler;
// Inject IJobHistory to query past executions
app.MapGet("/jobs/history", (IJobHistory history) =>
{
var all = history.GetAll();
var cleanup = history.GetHistory("cleanup");
return Results.Ok(new { all, cleanup });
});
Job Event Callbacks
using Philiprehberger.Scheduler;
builder.Services.AddScheduler(options =>
{
options.AddJob<CleanupJob>("*/5 * * * *");
options.OnJobStarted = name =>
Console.WriteLine($"Job started: {name}");
options.OnJobCompleted = (name, duration) =>
Console.WriteLine($"Job completed: {name} in {duration.TotalMilliseconds}ms");
options.OnJobFailed = (name, ex) =>
Console.WriteLine($"Job failed: {name} - {ex.Message}");
});
Cron Expressions
using Philiprehberger.Scheduler;
var cron = CronExpression.Parse("*/5 * * * *");
var next = cron.GetNextOccurrence(DateTimeOffset.UtcNow);
var matches = cron.Matches(DateTimeOffset.UtcNow);
API
IScheduledJob
| Method | Description |
|---|---|
ExecuteAsync(CancellationToken) |
Execute the scheduled job |
CronExpression
| Method | Description |
|---|---|
Parse(string expression) |
Parse a 5-field cron expression |
GetNextOccurrence(DateTimeOffset) |
Get the next matching time |
Matches(DateTimeOffset) |
Check if a time matches |
ScheduledJobAttribute
| Property | Type | Default | Description |
|---|---|---|---|
Name |
string |
— | Job name |
CronExpression |
string |
— | Cron schedule |
PreventOverlap |
bool |
true |
Skip if previous run is still executing |
TimeZone |
string? |
null |
IANA timezone ID for cron evaluation |
SchedulerOptions
| Method | Description |
|---|---|
AddJob<TJob>(string cronExpression) |
Register a recurring job with a cron schedule |
AddJob<TJob>(string cronExpression, string? timeZone) |
Register a recurring job with timezone |
ScheduleOnce(string name, Func<CancellationToken, Task> action, DateTimeOffset runAt) |
Schedule a one-time job |
| Property | Type | Description |
|---|---|---|
OnJobStarted |
Action<string>? |
Callback when a job starts |
OnJobCompleted |
Action<string, TimeSpan>? |
Callback when a job completes |
OnJobFailed |
Action<string, Exception>? |
Callback when a job fails |
IJobHistory
| Method | Description |
|---|---|
GetHistory(string jobName) |
Get execution records for a specific job |
GetAll() |
Get all execution records across all jobs |
Record(JobExecutionRecord record) |
Store an execution record |
JobExecutionRecord
| Property | Type | Description |
|---|---|---|
JobName |
string |
Name of the executed job |
StartTime |
DateTimeOffset |
When the execution started |
Duration |
TimeSpan |
How long the execution took |
Success |
bool |
Whether the job succeeded |
ErrorMessage |
string? |
Error message if failed |
CronScheduler
| Method | Description |
|---|---|
StartAsync(CancellationToken) |
Start the scheduler |
StopAsync(CancellationToken) |
Stop gracefully |
Development
dotnet build src/Philiprehberger.Scheduler.csproj --configuration Release
Support
If you find this project useful:
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.