Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Hangfire with custom CRON job runner #252

Merged
merged 7 commits into from
Nov 23, 2021
Merged

Replace Hangfire with custom CRON job runner #252

merged 7 commits into from
Nov 23, 2021

Conversation

ostorc
Copy link
Contributor

@ostorc ostorc commented Nov 16, 2021

Closes #208

Todo:

  • migrate Hangire jobs
  • add some statistics for jobs (not sure yet)

@ostorc ostorc requested a review from tenhobi November 16, 2021 15:14
@ostorc ostorc requested a review from a team as a code owner November 16, 2021 15:14
@ostorc
Copy link
Contributor Author

ostorc commented Nov 16, 2021

Example implementations:

Scoped job:

[Cron("*/10 * * * * *")] // Not mandatory, you can register it with cron explicitly
public class WarningCountJob : IJob
{
    private readonly IWarningService _warningService;
    private readonly ILogger<WarningCountJob> _logger;
    public string Name { get; } = "Warning Count Job";

    public WarningCountJob(IWarningService warningService, ILogger<WarningCountJob> logger)
    {
        _warningService = warningService;
        _logger = logger;
    }

    public async Task ExecuteAsync(CancellationToken cancellationToken = default)
    {
        var warnings = await _warningService.GetAllWarningsAsync();

        _logger.LogError("Total warning count: {WarningCount}", warnings.Count);
    }
}

Singleton job:

public class PingJob : ICronJob
{
    private readonly ILogger<PingJob> _logger;
    public string Name { get; } = "Ping";
    public string CronExpression { get; } = "*/20 * * * * *";

    public PingJob(ILogger<PingJob> logger)
    {
        _logger = logger;
    }

    public Task ExecuteAsync(CancellationToken cancellationToken = default)
    {
        _logger.LogWarning("Ping at {Now}", DateTime.Now);
        return Task.CompletedTask;
    }
}

@ostorc
Copy link
Contributor Author

ostorc commented Nov 16, 2021

How to configure services:

services.AddScheduler(5000)
    .AddCronJob<PingJob>()
    .AddScopedCronJob<WarningCountJob>("*/20 * * * * *") // we can explicitly use cron expression
    .AddScopedCronJob<WarningCountJob>(); // or use CronAttribute

Copy link
Member

@tenhobi tenhobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are putting contracts to the same package now?

image

// #175

src/HonzaBotner.Scheduler/Scheduler.cs Outdated Show resolved Hide resolved
src/HonzaBotner.Scheduler/Scheduler.cs Outdated Show resolved Hide resolved
@ostorc
Copy link
Contributor Author

ostorc commented Nov 16, 2021

We are putting contracts to the same package now?

// #175

Mainly, because I didnt want to add another package only with three files... And since this is fairly lightweight library, I dont think it will be issue.

@ostorc ostorc requested a review from tenhobi November 16, 2021 18:35
@tenhobi tenhobi changed the title ✨ CRON Job runner Replace Hangfire with custom CRON job runner Nov 18, 2021
Copy link
Member

@tenhobi tenhobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ostorc ostorc merged commit 2e42ad4 into develop Nov 23, 2021
@ostorc ostorc deleted the feat/schedule branch November 23, 2021 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

CRON service
2 participants