using DS.Module.Core; using DS.WMS.Core.Code.Entity; using DS.WMS.Core.HangfireJob.Interface; using Hangfire; using Hangfire.MySql; namespace DS.WMS.JobService { public class Worker : BackgroundService { private readonly ILogger _logger; private BackgroundJobServer _OpServer; public Worker(ILogger logger) { _logger = logger; } public override async Task StartAsync(CancellationToken stoppingToken) { try { while (!stoppingToken.IsCancellationRequested) { _OpServer = new BackgroundJobServer(new BackgroundJobServerOptions { SchedulePollingInterval = TimeSpan.FromMinutes(1), ServerName = "OpWorkService", Queues = new[] { "op" } }); await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); } } catch (OperationCanceledException ex) { Console.WriteLine(ex.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); //Environment.Exit(1); } } //protected override async Task StopAsync(CancellationToken stoppingToken) //{ //} protected override async Task ExecuteAsync(CancellationToken stoppingToken) { //var options = new BackgroundJobServerOptions //{ // SchedulePollingInterval = TimeSpan.FromMinutes(1), // ServerName = "WorkService", // Queues = new[] { "op", "task" } //}; while (!stoppingToken.IsCancellationRequested) { try { //_server = new BackgroundJobServer(options); //_server = new BackgroundJobServer(new BackgroundJobServerOptions //{ // SchedulePollingInterval = TimeSpan.FromMinutes(1), // ServerName = "WorkService", // Queues = new[] { "op", "task" } //}); //_server = new BackgroundJobServer(); await DoWork(stoppingToken); Console.WriteLine("Hangfire Server started. Press any key to exit..."); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex); } await Task.Delay(1000, stoppingToken); } } private async Task DoWork(CancellationToken cancellationToken) { // 这里放你的业务逻辑代码 await Task.CompletedTask; } } }