-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
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;
}
} |
How to configure services: services.AddScheduler(5000)
.AddCronJob<PingJob>()
.AddScopedCronJob<WarningCountJob>("*/20 * * * * *") // we can explicitly use cron expression
.AddScopedCronJob<WarningCountJob>(); // or use CronAttribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
e966cb1
to
080a060
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes #208
Todo: