agronholm / apscheduler

Task scheduling library for Python
MIT License
6.29k stars 710 forks source link

When a job is forcibly cancelled by calling `stop()`, the added schedule to the scheduler will no longer create new job. #903

Open Trung0246 opened 6 months ago

Trung0246 commented 6 months ago

Things to check first

Version

4.0.0a4

What happened?

How the schedule was created:


TRACK_ENGINE = sqlalchemy.ext.asyncio.create_async_engine(f"sqlite+aiosqlite:///{schedule_path}")
pathlib.Path(schedule_path).parent.mkdir(parents=True, exist_ok=True)
TRACK_DB = apscheduler.datastores.sqlalchemy.SQLAlchemyDataStore(
    engine=TRACK_ENGINE,
)

async with apscheduler.AsyncScheduler(data_store=TRACK_DB) as scheduler:
    TRACK_SCHEDULE = scheduler

await TRACK_SCHEDULE.add_schedule(
    track_handle,
    apscheduler.triggers.interval.IntervalTrigger(
        weeks=data.get("weeks", 0),
        days=data.get("days", 0),
        hours=data.get("hours", 0),
        minutes=data.get("minutes", 0),
        seconds=data.get("seconds", 0),
        microseconds=data.get("microseconds", 0),
        start_time=data.get("start_time", datetime.datetime.now()),
        end_time=data.get("end_time", None),
    ),
    max_running_jobs = 1,
    conflict_policy = apscheduler.ConflictPolicy.do_nothing
)

The schedule termination mechanism:

await asyncio.gather(TRACK_SCHEDULE.stop(), TRACK_SCHEDULE.wait_until_stopped())

How can we reproduce the bug?

This only happens when the job is RUNNING, not when it's finished and therefore we can safely cancel during that time span.

Use the above code to plug in appropriate order. When TRACK_SCHEDULE restarted with new python runtime instance, the job no longer getting created.

The problem is probably this:

image

Looks like running_jobs is still at 1 despite the job being cancelled.

agronholm commented 3 months ago

Can you reproduce this on master? I've made TONS of changes around this area, and the problem may not exist anymore. If it does, would you mind creating a minimal working example for me?