0
Pallabi created
1 Answer(s)
-
0
Hi,
I found its registration like below:
await context.ServiceProvider .GetRequiredService<IBackgroundWorkerManager>() .AddAsync(context.ServiceProvider.GetRequiredService<ExpiredAuditLogDeleterWorker>());
It works as background worker.
It resolves
ExpiredAuditLogDeleterOptions
in the main ExpiredAuditLogDeleterWorker service. And that option class is like below:public class ExpiredAuditLogDeleterOptions { /// <summary> /// Default: Everyday once. /// </summary> public int Period { get; set; } = (int)TimeSpan.FromDays(1).TotalMilliseconds; }
Unfortunately, there is no a CRON-like condiguration that identifies exact time to work right now. Here a suggestion about how you can do it manually:
- Add Cronos libraryo to parse a CRON expressions:
dotnet add package Cronos
- Then create your own ExpiredAuditLogDeleterWorker service to replace the original one.
[Dependency(ReplaceServices = true)] [ExposeServices(typeof(ExpiredAuditLogDeleterWorker))] public class MyCustomExpiredAuditLogDeleterWorker : ExpiredAuditLogDeleterWorker { // Your expression here: public const string Expression = "0 23 * * *"; public MyCustomExpiredAuditLogDeleterWorker( AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory, IOptions<ExpiredAuditLogDeleterOptions> options) : base(timer, serviceScopeFactory, options) { // Cron expressions resolution is 1 minute, so we need to set the period to 1 minute // Each minute, the worker will check if there are any expired audit logs and delete them timer.Period = (int)TimeSpan.FromMinutes(1).TotalMilliseconds; } protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) { var cronExpression = CronExpression.Parse(Expression); var now = DateTimeOffset.UtcNow; var nextOccurrence = cronExpression.GetNextOccurrence(now.AddSeconds(-1), TimeZoneInfo.Utc); // If the next occurrence is within this minute, run the job if (nextOccurrence.HasValue && nextOccurrence.Value > now.AddSeconds(-60) && nextOccurrence.Value <= now) { await base.DoWorkAsync(workerContext); } // else: do nothing, wait for the next tick } }
It seems it's better to use background jobs instead background worker, but it's what it's right now. You can use this workaround and I'll inform the ABP team about it. They may want to make an enhancement