TheColonel26
TheColonel26

Reputation: 2728

apscheduler interval task is not running

I would like to have my main task fire at 6am everyday. but for testing purposes I set the interval to 5 seconds. The problem is it doesn't seem to ever fire. I have a break point in the maintask method that is never reached and nothing gets printed to the console. I am assuming it's not running.

ETA: my code gets to scheduler.start() where is stops because it is blocking. it should start my maintask in 5 seconds but it never does.

python version is 2.7 apscheduler version is 3.0

I have run it on windows and Debian same result.

Here is my code.

from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def maintask():
    print("blah")


def main():

    scheduler = BlockingScheduler()

    print("Scheduling Tasks")
    start_time = (datetime.datetime.now()).replace(hour=6, minute=0, second=0, microsecond=0)
    scheduler.scheduled_job(maintask, 'interval', id="MainTaskid", name="mainTask", start_date=start_time, seconds=5, misfire_grace_time=60)
    print("Tasks Scheduled")
    print("Running Tasks")
    scheduler.start()
    print("Good Bye")
    return 0


if __name__ == "__main__":
    main()

Upvotes: 4

Views: 4300

Answers (1)

abarnert
abarnert

Reputation: 365707

The problem is that you're using scheduled_job instead of add_job.

As the User Guide explains, scheduled_job is mainly intended to be used as a decorator, as "a convenience to declare jobs that don’t change during the application’s run time", while the latter is "the most common way" to add jobs.

In this particular case, I believe that the issue is that add_job forces the scheduler to wake up, and scheduled_job doesn't—which you wouldn't think would be relevant, except for the fact that you're relying on that wakeup to force the misfire check to run at startup instead of not until 6am tomorrow.

At any rate, just changing it to add_job solves the problem.

Upvotes: 3

Related Questions