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

Fix Issue #388: Celery Beat scheduled tasks may be executed repeatedly #660

Merged
merged 3 commits into from
Jul 25, 2023

Conversation

BaiJiangJie
Copy link
Contributor

@BaiJiangJie BaiJiangJie commented Jul 10, 2023

Describe

Fix the problem that Celery Beat scheduled tasks may be executed repeatedly

Problem

Describe

If self.schedule is called, it may cause the data in the database to be retrieved before the self.save() method, instead of the temporarily set last_run_at data

If self.schedule is called here
Then it may cause the last_run_at of self.schedule[name] calling save to be the old data retrieved from the database
Instead of last_run_at temporarily set after task execution (set in __next__() method)
After the max_interval interval, the next task detection cycle will still execute the task again

Demo

Task information:

beat config: max_interval = 60s

Task name: cap
Task execution cycle: Execute every 3 minutes
Task last execution time: 18:00

The first execution of the task: 18:03 (set last_run_at = 18:03 when executing, and it is in memory at this time)

After the task execution is completed,
It is detected that sync is required, and self.schedule is called in sync,
If schedule_changed() is found to be True in self.schedule, all_as_schedule() needs to be called
At this point, the last_run_at of self.schedule[name] called in sync is 18:00
At this time, self.save() is performed in self.sync()

beat: Waking up 60s...

The second execution of the task: 18:04 (because the last_run_at obtained is 18:00, entry.is_due() == True)

Test

There are currently no test cases.

Effect

After modifying the code, the problem of repeated execution no longer appeared in our project JumpServer.

@BaiJiangJie
Copy link
Contributor Author

BaiJiangJie commented Jul 10, 2023

@BaiJiangJie BaiJiangJie changed the title Fix: The problem that Celery Beat scheduled tasks may be executed rep… Fix Issue #388: The problem that Celery Beat scheduled tasks may be executed rep… Jul 10, 2023
@BaiJiangJie BaiJiangJie changed the title Fix Issue #388: The problem that Celery Beat scheduled tasks may be executed rep… Fix Issue #388: Celery Beat scheduled tasks may be executed repeatedly Jul 11, 2023
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try to add new tests for this. also if yyou can fix the failing test in this or another PR, it will be really helpful

@BaiJiangJie
Copy link
Contributor Author

你从代码逻辑上来看这样修改是对的吗?
如果没问题,我再考虑增加它的测试用例。

Is it right to modify it in this way from the logical point of view of code?

If there is no problem, I will consider adding its test cases.

@auvipy
Copy link
Member

auvipy commented Jul 11, 2023

I think you can proceed with tests

@BaiJiangJie
Copy link
Contributor Author

Okay, it might take a while.


# Check
assert e1_pre_sync_last_run_at == e1_post_sync_last_run_at

Copy link
Contributor Author

@BaiJiangJie BaiJiangJie Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result of the test. Normally, None should be the last_run_at value updated last time, but after querying it, it is found that it is not.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is normal if self.schedule is changed to self._schedule.

image

@BaiJiangJie
Copy link
Contributor Author

The test case has been added, you can review it, thank you.

@auvipy

@auvipy auvipy self-requested a review July 25, 2023 08:36
@auvipy auvipy merged commit 700c804 into celery:main Jul 25, 2023
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

Successfully merging this pull request may close these issues.

2 participants