SirTony / Rota

A simple yet robust job scheduling library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rota

A simple yet robust job scheduling library that aims to be simpler and easier to use than Quartz.NET, but also more robust and configurable than other simple schedulers like Coravel while providing sensible default configuration options such that for light use little to no configuration is actually required beyond setting up job schedules.

Rota was heavily inspired by Coravel.

Features

  • Job scheduling
    • Simple schedule that triggers on an interval.
    • Schedule that uses cron expressions.
    • Schedule that uses a rate limiter to further restrict execution.
    • Complex schedule that can build highly specific schedules without needing to know cron expressions.
  • Multiple execution modes.
    • Concurrent/parallel execution.
    • Consecutive execution.
  • Granular control of configuration.
  • Microsoft.Extensions.Hosting support.
  • Caching & persistence for schedules, and jobs.
    • Built-in cache providers.
      • JSON
      • XML
      • MongoDB
      • PgSQL
      • MySQL/MariaDB
      • SQLite
      • SurrealDB

Quick Start

Rota is available on NuGet.

Manual

Below is a minimal example of a job that prints Hello, World! to the console every 10 seconds on a manually managed job scheduler.

using Rota;
using Rota.Jobs;
using Rota.Scheduling;

var scheduler = new JobScheduler();
var everyTenSeconds = Schedule.FromInterval( TimeSpan.FromSeconds( 10 ) ).RunOnceAtStartup();

scheduler.ScheduleJob<HelloWorldJob>( everyTenSeconds ); // HelloWorldJob will execute every 10 seconds

while( !scheduler.IsCancellationRequested )
{
    await scheduler.RunJobsAsync(); // run all jobs that are due to be executed
    await Task.Delay(
        scheduler.Configuration.PollingRate // the interval at which RunJobsAsync() should be called.
    );
}

public sealed class HelloWorldJob : IJob
{
    public async ValueTask ExecuteAsync( CancellationToken cancellationToken )
        => await Console.Out.WriteLineAsync( "Hello, World!" );
}

Hosted

Below is a minimal example of a job that prints Hello, World! to the console every 10 seconds using a scheduler registered to a hosting context. It also demonstrates dependency injection support by using an ILogger instance to print to the console.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Rota.Hosting;
using Rota.Jobs;
using Rota.Scheduling;

var host = Host.CreateDefaultBuilder()
               .ConfigureDefaults( args )
               .UseConsoleLifetime()
               .AddScheduler()
               .Build();

host.UseScheduler(
    scheduler => {
        var everyTenSeconds = Schedule.FromInterval( TimeSpan.FromSeconds( 10 ) ).RunOnceAtStartup();

        scheduler.ScheduleJob<HelloWorldJob>( everyTenSeconds ); // HelloWorldJob will execute every 10 seconds
    }
);

await host.RunAsync();

public sealed class HelloWorldJob : IJob
{
    private readonly ILogger<HelloWorldJob> _logger;

    public HelloWorldJob( ILogger<HelloWorldJob> logger ) => this._logger = logger;

    public ValueTask ExecuteAsync( CancellationToken cancellationToken )
    {
        this._logger.LogInformation( "Hello, World!" );
        return ValueTask.CompletedTask;
    }
}

About

A simple yet robust job scheduling library.

License:MIT License


Languages

Language:C# 100.0%