Skip to content
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

BackgroundServiceTask: timeInterval computation is wrong #425

Closed
serlooks opened this issue Sep 6, 2023 · 1 comment
Closed

BackgroundServiceTask: timeInterval computation is wrong #425

serlooks opened this issue Sep 6, 2023 · 1 comment

Comments

@serlooks
Copy link

serlooks commented Sep 6, 2023

The line: https://github.com/grandnode/grandnode2/blob/main/src/Web/Grand.Web.Common/Infrastructure/BackgroundServiceTask.cs#L57 is wrong.

If a task is executed before it should, this check makes sure to delay it further until it's proper execution time, however this line does not compute it properly. The computed value later goes to do Task.Delay(timeInterval).

Few debug exports:

UTC Now LastStart Base Interval LastStart + Interval Computed new Interval What it should be
06.09.2023 09:27:03 06.09.2023 08:44:57 60 06.09.2023 09:44:57 42 17
06.09.2023 09:27:06 05.09.2023 11:51:52 1440 06.09.2023 11:51:52 1295 144

The line should be replaced either with:
timeInterval = (int)(task.LastStartUtc.Value.AddMinutes(task.TimeInterval) - datetimenow).TotalMinutes
or
timeInterval = task.TimeInterval - (int)(datetimenow - task.LastStartUtc.Value).TotalMinutes;

which will put the task to sleep for the remaining time.


This was discovered when we noticed our tasks running at an interval of 119 minutes instead of 60. The task would attempt to execute maybe a few seconds before it should, and current implementation reschedules it in another 59 minutes.

Please verify this and correct me if I'm wrong.

Best Regards,
Luka

@KrzysztofPajak
Copy link
Member

@serlooks Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants