Hangfire
Install-Package Hangfire.AspNetCore Install-Package Hangfire.LiteDBusing Hangfire; using Hangfire.LiteDB; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddHangfire(configuration => { configuration.UseLiteDbStorage("litedb.db"); }); builder.Services.AddHangfireServer(); var app = builder.Build(); // Configure the HTTP request pipeline. app.UseHangfireDashboard("/cron"); app.MapControllers(); app.Run();RecurringJob.AddOrUpdate("my-recurring-job", () => Console.WriteLine("Recurring job!"), Cron.Daily);RecurringJob.AddOrUpdate("my-recurring-job", () => Console.WriteLine("Updated recurring job!"), Cron.Hourly);RecurringJob.RemoveIfExists("my-recurring-job");public void RemoveAllRecurringJobs() { var recurringJobs = JobStorage.Current.GetConnection().GetRecurringJobs(); foreach (var job in recurringJobs) { RecurringJob.RemoveIfExists(job.Id); } }public class HangfireServiceManager { private readonly IBackgroundProcessingServer _backgroundProcessingServer; public HangfireServiceManager(IBackgroundProcessingServer backgroundProcessingServer) { _backgroundProcessingServer = backgroundProcessingServer; } public void RestartService() { _backgroundProcessingServer.Dispose(); // Stop the server // Optionally, wait a bit before starting it again GlobalConfiguration.Configuration.UseLiteDbStorage("litedb.db"); JobStorage.Current = new LiteDbStorage("litedb.db"); var backgroundJobServer = new BackgroundJobServer(); // Start the server again } }
Example Controller
Last updated