You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB
C#

using DS.Module.Core;
using Hangfire;
using Hangfire.MySql;
namespace DS.WMS.JobService
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private BackgroundJobServer _server;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
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();
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;
}
}
}