https://github.com/michalmoudry/fs-cron
A .NET library for periodically running tasks
https://github.com/michalmoudry/fs-cron
cronjob-scheduler csharp dotnet dotnet-9 fsharp
Last synced: about 1 month ago
JSON representation
A .NET library for periodically running tasks
- Host: GitHub
- URL: https://github.com/michalmoudry/fs-cron
- Owner: MichalMoudry
- License: mit
- Created: 2024-08-11T12:15:20.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-25T17:41:41.000Z (about 1 year ago)
- Last Synced: 2025-06-08T22:01:45.916Z (about 1 year ago)
- Topics: cronjob-scheduler, csharp, dotnet, dotnet-9, fsharp
- Language: F#
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FsCron
A .NET library for periodically running tasks. This library was built on top of [Cronos](https://github.com/HangfireIO/Cronos).
## Basic usage
```csharp
await using var scheduler = new Scheduler(TimeZoneInfo.Local);
scheduler.NewAsyncJob(
"* * * * *",
async token =>
{
Console.WriteLine($"[{DateTimeOffset.Now}] Test print");
await Task.Delay(1000, token);
}
);
scheduler.Start();
```
### Async usage
Asynchronous usage is about running the scheduler in a separate thread.
```csharp
await using var scheduler = new Scheduler(TimeZoneInfo.Local);
scheduler.NewAsyncJob(
"* * * * *",
async token =>
{
Console.WriteLine($"[{DateTimeOffset.Now}] Test print");
await Task.Delay(1000, token);
}
);
scheduler.StartAsync();
```
Internally, `Scheduler` class has two methods `Start()` and `StartAsync()`. `Start()` executes startInternal() method directly and `StartAsync()` treats startInternal() as a ThreadStart delegate for a background [Thread](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-9.0 "Link to Thread class documentation").
## Types of jobs
- Async job - A [Func](https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=net-9.0) delegate that has one parameter (cancellation token) and returns a Task. The returned task is not awaited, but rather started on a thread pool.
- Sync job: An [Action](https://learn.microsoft.com/en-us/dotnet/api/system.action?view=net-9.0 "Link to Action delegate documentation") delegate that is periodically queued on the thread pool, so that it is not blocking scheduler's own execution.
## Job cancellation
`Scheduler` class realizes IDisposable (and IAsyncDisposable) interface. Also, the class internally stores a CancellationTokenSource which is cancelled and disposed along side the scheduler itself.
## AOT compatibility