-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 [BUG] Unhandled exception. System.ArgumentException: Invalid value '2671030968.9793' for parameter 'interval'. #22
Comments
My bad, it's this line Seems to be something wrong with the monthly cron. |
Is the issue MonthlyRemoverService.cs or InactiveRemoverService.cs? @binn |
@furkandeveloper sorry forgot to respond, it was MonthlyRemoverService.cs Scheduling it every month causes this bug to happen for some reason |
You're right, this error doesn't happen all the time. I tried several times on the sample project, but it worked successfully every time. I got the same error as you when I used the code below. services.ApplyResulation<ConsoleCronJob>(options =>
{
options.CronExpression = "* * * 12 *";
options.TimeZoneInfo = TimeZoneInfo.Local;
}); I think the interval value should not be more than 25 days. I tried the following value as I tried it on 18.01. I got the same error as it was over 25 days. services.ApplyResulation<ConsoleCronJob>(options =>
{
options.CronExpression = "* * 13 2 *";
options.TimeZoneInfo = TimeZoneInfo.Local;
}); I didn't get the error when the max was 25 days and the project was successful. services.ApplyResulation<ConsoleCronJob>(options =>
{
options.CronExpression = "* * 12 2 *";
options.TimeZoneInfo = TimeZoneInfo.Local;
}); Let's summarize; It uses Timer in EasyCronJob. Timer cron is used to schedule jobs. In schedule operations, the current time is subtracted from the time the job will run and is assigned as a delay. Delay value must not be greater than int.MaxValue. 2671030968.9793 you are getting this error because this value is greater than int.MaxValue. I guess there is nothing to do. You must use values not greater than int.MaxValue. |
That is pretty weird, pretty faulty limitation. Is there a possible workaround?
Timer, being limited to an integer, may not be suitable then. Any other suggestions or patterns that may work out for the library? |
I prepared a solution for you. This solution contains Overengineering methods. Startup.csservices.ApplyResulation<YourCronJob>(options =>
{
options.CronExpression = "0 0 25 * *";
options.TimeZoneInfo = TimeZoneInfo.Local;
}); YourCronJob.cspublic class YourCronJob : CronJobService
{
private System.Timers.Timer timer;
private readonly ILogger<YourCronJob> logger;
public YourCronJob(ICronConfiguration<YourCronJob> cronConfiguration, ILogger<YourCronJob> logger)
: base(cronConfiguration.CronExpression, cronConfiguration.TimeZoneInfo)
{
this.logger = logger;
}
private TimeSpan CalculateTimerDelay()
{
DateTime next = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
var delay = next - DateTimeOffset.Now;
return delay;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
var delay = CalculateTimerDelay();
timer = new System.Timers.Timer(delay.TotalMilliseconds);
logger.LogInformation("Start Your Job" + " Start Time : " + DateTime.UtcNow);
return base.StartAsync(cancellationToken);
}
protected override Task ScheduleJob(CancellationToken cancellationToken)
{
logger.LogInformation("Schedule Your Job" + " Start Time : " + DateTime.UtcNow);
return base.ScheduleJob(cancellationToken);
}
public override Task DoWork(CancellationToken cancellationToken)
{
logger.LogInformation("Timer Starting" + " Start Time : " + DateTime.UtcNow);
timer.Elapsed += async (sender, args) =>
{
timer.Dispose(); // reset and dispose timer
timer = null;
Test();
};
timer.Start();
return base.DoWork(cancellationToken);
}
public void Test()
{
var delay = CalculateTimerDelay();
timer = new System.Timers.Timer(delay.TotalMilliseconds);
logger.LogInformation("Working Your Job" + " Start Time : " + DateTime.UtcNow);
}
} ResultIt will run on the 25th day of each month and create a timer until the first day of the next month. So the int.MaxValue limit will not block you. fyi @binn |
It's kinda workaround and seems like a good approach to get over the problem. Thanks @furkandeveloper |
Thank you, I'll try it out in the coming days. |
Hello, I got the same problem. Maybe to fix this you could use something else than System.Timers.Timer here ? Maybe System.Threading.Timer or https://stackoverflow.com/questions/41284103/assign-a-double-value-to-timer-interval |
I'm trying to use your workaround, but I think it doesn't work like it is, I mean, if you call Or maybe I misunderstood something ? Besides, I think that if your server is restarting for any reason, between the 25th and the 1st, you will have the same problem. Finally, having the setting of the timer in StartAsync will give you problems if you start your server at the beginning of the month, for the same reasons we are having these problems. I moved the setting in the DoWork. I think to complete this, the cron expression should be I will post the solution I'm trying, not sure it will definitely work, I'll wait this month to see |
Not sure this will work, let me know about it. public class MyJob: CronJobService
{
private readonly ILogger<MyJob> _logger;
private System.Timers.Timer timer;
public MyJob(ICronConfiguration<MyJob> cronConfiguration,
ILogger<MyJob> logger,
IServiceProvider serviceProvider) :
base(cronConfiguration, logger, serviceProvider)
{
_logger = logger;
}
public void DoJob()
{
_logger.LogInformation("DoJob - MyJob");
//Code of what my job is supposed to do
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Delayed Start [MyJob]");
return base.StartAsync(cancellationToken);
}
protected override Task ScheduleJob(CancellationToken cancellationToken)
{
_logger.LogInformation("Custom Schedule [MyJob]");
return base.ScheduleJob(cancellationToken);
}
public override Task DoWork(CancellationToken cancellationToken)
{
_logger.LogInformation("Delayed Timer Starting [MyJob]");
var delay = CalculateTimerDelay();
if (timer == null)
{
timer = new System.Timers.Timer(delay.TotalMilliseconds);
timer.Elapsed += (sender, args) =>
{
//Reset and dispose timer
timer.Dispose();
timer = null;
DoJob();
};
timer.Start();
}
return base.DoWork(cancellationToken);
}
private TimeSpan CalculateTimerDelay()
{
DateTime next = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
var delay = next - DateTimeOffset.Now;
return delay;
}
} with EDITED services.ApplyResulation<MyJob>(options =>
{
options.CronExpression = "0 23 15,28,29,30,31 * *";
options.TimeZoneInfo = TimeZoneInfo.Local;
}); |
Hello, |
Hello @furkandeveloper, You're welcome :) I understand. I'll try to remember to let you know :) |
Hello, It worked well for me :) |
Actually it did not work completely.
by
Indeed, when the server was rebooting between the 25th and the end of the month, it could not compute the time when it should be launched. |
I have a similar/related issue, but with negative value, passed to the Timer constructor.
My cron is "*/5 * * * *". I can see that the error happens periodically (~ once per day) but always when time has 0 seconds. I.e at 11:45:00, 21:40:00, 11:55:00. As I can see from the source code
|
Describe the bug
Not sure what's causing this, but it sometimes happens and sometimes doesn't happen.
To Reproduce
Steps to reproduce the behavior:
Here is the code I used.
The line in question I believe is the InactiveRemoverService.
Code of AddCronJob
Expected behavior
Job scheduled to run every two hours.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: