refactor(worker): replace BackgroundService with Quartz IJob for scheduled execution
- Removed inheritance from BackgroundService - Implemented Quartz IJob interface for better scheduling control - Replaced ExecuteAsync with Execute(IJobExecutionContext) - Updated cancellation handling to use context.CancellationToken
This commit is contained in:
parent
4997f7d75c
commit
08ca116628
@ -36,7 +36,23 @@ try
|
|||||||
.ForEach(file => config.AddJsonFile(file, true, true));
|
.ForEach(file => config.AddJsonFile(file, true, true));
|
||||||
|
|
||||||
#region Worker
|
#region Worker
|
||||||
builder.Services.AddHostedService<Worker>();
|
builder.Services.AddQuartz(q =>
|
||||||
|
{
|
||||||
|
var name = $"{typeof(Worker).FullName}-{Guid.NewGuid():N}";
|
||||||
|
var jobKey = new JobKey(name);
|
||||||
|
q.AddJob<Worker>(opts => opts.WithIdentity(jobKey));
|
||||||
|
|
||||||
|
q.AddTrigger(opts => opts
|
||||||
|
.ForJob(jobKey)
|
||||||
|
.WithIdentity(name + "-trigger")
|
||||||
|
.WithCronSchedule("* * * * * ?"));
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddQuartzHostedService(opt =>
|
||||||
|
{
|
||||||
|
opt.WaitForJobsToComplete = true;
|
||||||
|
});
|
||||||
|
|
||||||
builder.Services.Configure<WorkerOptions>(config.GetSection("Worker"));
|
builder.Services.Configure<WorkerOptions>(config.GetSection("Worker"));
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -88,7 +104,7 @@ try
|
|||||||
|
|
||||||
Log.Information("The worker was stopped.");
|
Log.Information("The worker was stopped.");
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Fatal(ex, "Worker could not be started!");
|
Log.Fatal(ex, "Worker could not be started!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
using EnvelopeGenerator.Finalizer.Models;
|
using EnvelopeGenerator.Finalizer.Models;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Finalizer
|
namespace EnvelopeGenerator.Finalizer
|
||||||
{
|
{
|
||||||
public class Worker : BackgroundService
|
public class Worker : IJob
|
||||||
{
|
{
|
||||||
private readonly ILogger<Worker> _logger;
|
private readonly ILogger<Worker> _logger;
|
||||||
|
|
||||||
@ -15,16 +16,16 @@ namespace EnvelopeGenerator.Finalizer
|
|||||||
_options = workerOptions.Value;
|
_options = workerOptions.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
public async Task Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!context.CancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (_logger.IsEnabled(LogLevel.Information))
|
if (_logger.IsEnabled(LogLevel.Information))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
}
|
}
|
||||||
await Task.Delay(_options.IntervalInMillisecond, stoppingToken);
|
await Task.Delay(_options.IntervalInMillisecond, context.CancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
5
EnvelopeGenerator.Finalizer/appsettings.GdPicture.json
Normal file
5
EnvelopeGenerator.Finalizer/appsettings.GdPicture.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"GdPicture": {
|
||||||
|
"LicenseKey": "kG1Qf9PwmqgR8aDmIW2zI_ebj48RzqAJegRxcystEmkbTGQqfkNBdFOXIb6C_A00Ra8zZkrHdfjqzOPXK7kgkF2YDhvrqKfqh4WDug2vOt0qO31IommzkANSuLjZ4zmraoubyEVd25rE3veQ2h_j7tGIoH_LyIHmy24GaXsxdG0yCzIBMdiLbMMMDwcPY-809KeZ83Grv76OVhFvcbBWyYc251vou1N-kGg5_ZlHDgfWoY85gTLRxafjD3KS_i9ARW4BMiy36y8n7UP2jN8kGRnW_04ubpFtfjJqvtsrP_J9D0x7bqV8xtVtT5JI6dpKsVTiMgDCrIcoFSo5gCC1fw9oUopX4TDCkBQttO4-WHBlOeq9dG5Yb0otonVmJKaQA2tP6sMR-lZDs3ql_WI9t91yPWgpssrJUxSHDd27_LMTH_owJIqkF3NOJd9mYQuAv22oNKFYbH8e41pVKb8cT33Y9CgcQ_sy6YDA5PTuIRi67mjKge_nD9rd0IN213Ir9M_EFWqg9e4haWzIdHXQUo0md70kVhPX4UIH_BKJnxEEnFfoFRNMh77bB0N4jkcBEHPl-ghOERv8dOztf4vCnNpzzWvcLD2cqWIm6THy8XGGq9h4hp8aEreRleSMwv9QQAC7mjLwhQ1rBYkpUHlpTjhTLnMwHknl6HH0Z6zzmsgkRKVyfquv94Pd7QbQfZrRka0ss_48pf9p8hAywEn81Q=="
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user