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

job successfully added but never removed #708

Open
3 tasks done
shuffleyxf opened this issue Feb 19, 2023 · 6 comments
Open
3 tasks done

job successfully added but never removed #708

shuffleyxf opened this issue Feb 19, 2023 · 6 comments
Labels

Comments

@shuffleyxf
Copy link

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

3.10.0

What happened?

I am using apscheduler to schedule some task, and do something when the task stop through the EVENT_JOB_REMOVED. But i found that job will never deleted when the time between start and end is shorter than interval, and never trigger EVENT_JOB_REMOVED.

How can we reproduce the bug?

import datetime
import time


from apscheduler.events import EVENT_JOB_REMOVED
from apscheduler.schedulers.background import BackgroundScheduler


def my_listener(event): 
    print('remove job!')


def func(): 
    print('hello, apcheduler')
    
    
if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_listener(my_listener, EVENT_JOB_REMOVED)
    scheduler.start()
    end_time = datetime.datetime.now() + datetime.timedelta(seconds=3)
    scheduler.add_job(func, 'interval', seconds=5, end_date=end_time)

    while True:
        print(scheduler._jobstores['default'].get_all_jobs())
        time.sleep(5)
@shuffleyxf shuffleyxf added the bug label Feb 19, 2023
@riosw
Copy link

riosw commented Mar 18, 2023

I think this happens because the job is considered as "will never run" (i.e. next_run_time is None). In fact, in that code snippet, func() never got called.

For example, calling add_job with slightly later start_date argument works. It properly executes func() once and removes the job:

scheduler.add_job(
        func,
        "interval",
        seconds=5,
        start_date=datetime.datetime.now() + datetime.timedelta(seconds=1),
        end_date=end_time,
    )

@riosw
Copy link

riosw commented Mar 18, 2023

My comment earlier might be more of an inspection of the issue. If you need anything, feel free to comment!

@shuffleyxf
Copy link
Author

shuffleyxf commented Sep 9, 2023

I understand what you said, but I still think the job should be removed and trigger EVENT_JOB_REMOVED rather than kept forever.

@agronholm
Copy link
Owner

Could you try this with APScheduler 4.0.0a2 (or master)? I feel this isn't a problem on 4.x anymore, but I'll fix it if it happens there too.

@shuffleyxf
Copy link
Author

I believe this issue still exists on master. The following code reproduces the issue.

import datetime

from apscheduler import TaskRemoved, TaskAdded

from apscheduler.schedulers.sync import Scheduler
from apscheduler.triggers.interval import IntervalTrigger


def remove_listener(event):
    print('remove task!')


def add_listener(event):
    print('add task!')


def func():
    print('hello, apcheduler')


if __name__ == '__main__':
    scheduler = Scheduler()
    scheduler.event_broker.subscribe(remove_listener, {TaskRemoved})
    scheduler.event_broker.subscribe(add_listener, {TaskAdded})
    end_time = datetime.datetime.now() + datetime.timedelta(seconds=3)
    trigger = IntervalTrigger(seconds=5, end_time=end_time)
    scheduler.add_schedule(func, trigger)
    scheduler.run_until_stopped()

@agronholm
Copy link
Owner

I think there's a misunderstanding regarding v4.0 here: the task isn't supposed to go away, only the schedule and any jobs spawned from it.

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

No branches or pull requests

3 participants