jda5
jda5

Reputation: 1456

How to access and modify the original next_run_time of a paused Job in APScheduler?

I am using Python 3.12 and APScheduler version 3.11.0.

I have scheduled a job to run at a specific time. If the job needs to be paused before execution, I would like to adjust its next_run_time before resuming it. However, I am unsure how to access the originally set trigger time (the time specified when scheduling the job).

Here's an example to illustrate the issue:

from datetime import datetime, timedelta
from apscheduler.schedulers.background import BackgroundScheduler
from time import sleep

scheduler = BackgroundScheduler()
scheduler.start()

def do_stuff():
    pass

job = scheduler.add_job(
    func=do_stuff,
    trigger="date",
    next_run_time=datetime.now() + timedelta(seconds=10),
    misfire_grace_time=None,
    max_instances=10,
)
print("[SET] Next run time:", job.next_run_time)
print("[SET] Trigger:", job.trigger)

# Pause the job before execution
sleep(5)
job.pause()
print("[PAUSE] Next run time:", job.next_run_time)

# Wait for the original run time to pass
sleep(30)

# Resume the job
job.resume()
print("[RESUMED] Next run time:", job.next_run_time)

scheduler.shutdown()

Running the above outputs:

[SET] Next run time: 2025-01-22 14:25:44.252052+00:00
[SET] Trigger: date[2025-01-22 14:25:34 GMT]
[PAUSE] Next run time: None
[RESUMED] Next run time: 2025-01-22 14:25:34.252282+00:00

From this output, it seems that APScheduler resets the job's trigger as the new next_run_time when the job is paused and resumed. However, I want to access the originally set next_run_time so that I can modify it before resuming the job.

Is it possible to retrieve the job's originally scheduled next_run_time after the job has been paused?

If not, is there a recommended way to store and modify this information externally before resuming the job?

Upvotes: 1

Views: 11

Answers (0)

Related Questions