dotnet-labs / ServiceWorkerCronJob

Schedule Cron Jobs using HostedService in ASP.NET Core
MIT License
264 stars 72 forks source link

This solution is not working for CRON Expression - (0 0 1 * *) #7

Closed akashpavate58 closed 3 years ago

akashpavate58 commented 3 years ago

Root Cause: _timer = new System.Timers.Timer(delay.TotalMilliseconds);

Here, the delay is the time between now and the next occurrence. If this number delay.TotalMilliseconds is greater than System.Int32.MaxValue, the System.Timers.Timer throws an ArgumentException.

In nutshell, the current solution does not work for any CRON Expression that results in delay.TotalMilliseconds to be larger than System.Int32.MaxValue.

Can someone help me in fixing this?

changhuixu commented 3 years ago

Someone has raised this question before. The simplest solution is to run the job daily, and check the date in the actual jobService. In this way, you at least have some logs to track this job's status daily.

If you want to put the workload to the CronJobService, then @dusan-tkac has provided an improvement: https://gist.github.com/dusan-tkac/2233fddc136b13ea656fe3d5bd9f1061

Thank you.

blakepell commented 2 years ago

I'm not sure her answer works right.. basically what her code is doing is saying anything greater than Int32.MaxValue becomes Int32.MaxValue. So if you had a 30 day cron it would run every 24 days (roughly). The real issue is the timer class. The source code for it requests a double, has a field that it stores that value in that is a double but does an intermediary cast to an int which is the root of the problem.

I don't know what the real solution, it's either using a different Timer or chaining together multiple calls until it gets to the final day (a month for example). Thought I would share for posterity.