-
Notifications
You must be signed in to change notification settings - Fork 720
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 when stopping scheduler with jobs running - APScheduler v4 #946
Comments
I see two problems in the code:
|
However, the expected behavior when the job is completed normally is:
However, in my case, I used Scheduler.stop before the job was completed. Then the Scheduler showed me the log Job ...id... completed successfully, so the job was completed. However, the job was not deleted from the datastore and the task did not have its running_jobs decrement. My question is, what is causing the scheduler not to delete the job at the end of its execution and also why did it not decrement the task's running_jobs field?
In pymongo you can use Transactions within a session. But I don't think it's necessary in this case, because the task was not canceled or got an error. The job was completed successfully. If I'm leaving anything unnoticed please let me know. |
Transactions don't work on a single node Mongo server. |
Because |
You are right. So that would be the difficulty because not everyone uses MongoDB with more than one node :/ I use MongoDB with more than one node. So I think I would have to try to adapt something like MongoDBDataStore(allow_transaction=True) |
I understood |
How would that help users with just one node? |
Unfortunately this would still be a problem. Since transactions would only work for those who used allow_transaction=True |
I found a temporary solution to the problem I'm facing. Just save the jobs that were running when the scheduler stopped and use the .release_job method manually, like this: asyncio.run(
scheduler.data_store.release_job(
...
)
) This worked correctly even after the scheduler stopped. |
This is a dangerous looking "fix". You should be aware that I'm currently in the process of refactoring the |
It's not really the best solution. But it would help temporarily. Your idea about CancelScope sounds good. I hope it works well :) [Another idea] Example: class Scheduler():
...
scheduler = Scheduler()
scheduler.send_signal('stop running new jobs')
"""
I wait until no jobs are running in the scheduler and then use the scheduler.stop() method.
With this, the scheduler would be able to process the job deletion operations after they are executed,
and also decrement the task running_jobs field.
"""
assert len(scheduler._async_scheduler._running_jobs) == 0
scheduler.stop() This seems like a good solution to the current problem. |
The |
I just checked and, indeed, the AsyncScheduler._process_jobs method has a condition to only run with RunState.started. So, I think the best bet would be your idea about CancelScope. Man, I'd like to take this opportunity to thank you for your great work. This new version of APScheduler is looking amazing. I really like it. 😉 |
I'm not sure we're on the same page here. I brought up
Thanks! Always nice to see one's work appreciated! |
I noticed that with postgresql datastore if scheduler is terminated abruptly due to a program crashing or terminating then the running_jobs is left as is. There should be a reconciliation logic when the scheduler starts to align jobs (in I am using async job_executor and having max_running_jobs greater than 1 to allow many jobs per task. |
Yeah, that was the intention. Things like this is the reason it's still in alpha. |
Things to check first
I have checked that my issue does not already have a solution in the FAQ
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
Version
4.0.0a5
What happened?
Hello,
I would like to report a bug that occurs after stopping the Scheduler. From what I have noticed, the bug occurs when I stop the scheduler while there is a job running.
Summary: While a job is still running, I use the scheduler's .stop method. I wait until the scheduler.state is in the stopped state. I see that when it reaches the scheduler.state.stopped state, the job that was running before I stopped the scheduler is completed successfully. However, some operations remain pending in the DataStore, such as decreasing the running_jobs of the task document, and the job is not deleted from the job collection.
Tested only with MongoDBDataStore
How can we reproduce the bug?
Code to replicate the bug:
My logs running the sample code:
The text was updated successfully, but these errors were encountered: