Browse Source

fix: Simplify cron expression generation and improve error handling in background worker managers

pull/25066/head
maliming 5 months ago
parent
commit
7e12823cd5
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 28
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireDynamicBackgroundWorkerManager.cs
  2. 7
      framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQDynamicBackgroundWorkerManager.cs
  3. 4
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/DefaultDynamicBackgroundWorkerManager.cs

28
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireDynamicBackgroundWorkerManager.cs

@ -140,29 +140,31 @@ public class HangfireDynamicBackgroundWorkerManager : IDynamicBackgroundWorkerMa
protected virtual string GetCron(int period)
{
var time = TimeSpan.FromMilliseconds(period);
string cron;
if (time.TotalSeconds <= 59)
{
cron = $"*/{time.TotalSeconds} * * * * *";
var seconds = (int)Math.Round(time.TotalSeconds);
return $"*/{seconds} * * * * *";
}
else if (time.TotalMinutes <= 59)
{
cron = $"*/{time.TotalMinutes} * * * *";
}
else if (time.TotalHours <= 23)
if (time.TotalMinutes <= 59)
{
cron = $"0 */{time.TotalHours} * * *";
var minutes = (int)Math.Round(time.TotalMinutes);
return $"*/{minutes} * * * *";
}
else if (time.TotalDays <= 31)
if (time.TotalHours <= 23)
{
cron = $"0 0 0 1/{time.TotalDays} * *";
var hours = (int)Math.Round(time.TotalHours);
return $"0 */{hours} * * *";
}
else
if (time.TotalDays <= 31)
{
throw new AbpException($"Cannot convert period: {period} to cron expression.");
var days = (int)Math.Round(time.TotalDays);
return $"0 0 */{days} * *";
}
return cron;
throw new AbpException($"Cannot convert period: {period} to cron expression.");
}
}

7
framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQDynamicBackgroundWorkerManager.cs

@ -92,6 +92,10 @@ public class TickerQDynamicBackgroundWorkerManager : IDynamicBackgroundWorkerMan
AbpTickerQBackgroundWorkersProvider.BackgroundWorkers.Remove(functionName);
HandlerRegistry.Unregister(workerName);
// Note: ICronTickerManager<T> does not provide a remove API.
// The handler is unregistered above, so any persisted cron entry will
// find a null handler and skip execution silently.
return Task.FromResult(true);
}
@ -138,6 +142,9 @@ public class TickerQDynamicBackgroundWorkerManager : IDynamicBackgroundWorkerMan
var time = TimeSpan.FromMilliseconds(period);
if (time.TotalMinutes < 1)
{
Logger.LogWarning(
"TickerQ does not support sub-minute intervals. Period {Period}ms will be rounded up to every minute.",
period);
return "* * * * *";
}

4
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/DefaultDynamicBackgroundWorkerManager.cs

@ -37,7 +37,7 @@ public class DefaultDynamicBackgroundWorkerManager : IDynamicBackgroundWorkerMan
schedule.Validate();
if (schedule.Period == null && !string.IsNullOrWhiteSpace(schedule.CronExpression))
if (schedule.Period == null)
{
throw new AbpException(
$"The default in-memory background worker manager does not support CronExpression without Period for dynamic worker '{workerName}'. " +
@ -79,7 +79,7 @@ public class DefaultDynamicBackgroundWorkerManager : IDynamicBackgroundWorkerMan
schedule.Validate();
if (schedule.Period == null && !string.IsNullOrWhiteSpace(schedule.CronExpression))
if (schedule.Period == null)
{
throw new AbpException(
$"The default in-memory background worker manager does not support CronExpression without Period for dynamic worker '{workerName}'. " +

Loading…
Cancel
Save