4

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()
TheColonel26
  • 2,618
  • 7
  • 25
  • 50
  • Surely most of this code isn't at all relevant to the problem. Can you provide us a [minimal example](http://stackoverflow.com/help/mcve)? – abarnert Oct 21 '14 at 22:43
  • Also, what version of APScheduler are you using? – abarnert Oct 21 '14 at 22:46
  • Finally, you've got all these logging statements in your code… so what gets logged? Does it get to `"Running Tasks"`? What about `"Good Bye"`? – abarnert Oct 21 '14 at 22:47
  • @abarnet: In my opinion yes but I've been down voted several time before for not including code that I thought was completely irrelevant. APSch Version is 3.0 Python version is 2.7 it gets to scheduler.start() where is stops because I am using the blocking scheduler. no exceptions are throw. my task just never runs. – TheColonel26 Oct 21 '14 at 23:07
  • If the example you post doesn't actually demonstrate the problem, then of course your question will be downvoted or closed, because it's impossible to answer such a question. An MCVE has to be complete and verifiable. But it _also_ has to be minimal. – abarnert Oct 21 '14 at 23:16
  • And note that this code _isn't_ a complete example. You're calling a `log` function that you never import or define. And you're also missing at least one other `import`. And you never call `main`. People may want to help you by running your code to see what happens, but if your code doesn't run, they will usually just give up. – abarnert Oct 21 '14 at 23:18

1 Answers1

3

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • ooooh. I read that is what a decorator in the user guide but didn't know what that meant. from googling it seems it just like a class wrapper but for a function. – TheColonel26 Oct 21 '14 at 23:55
  • @TheColonel26: A decorator… is a way of applying either a class wrapper or a function wrapper to a class or function at definition time. I don't think that explains it very well, but it's hard to do in a comment, and the reference docs don't help much. [PEP 318](http://legacy.python.org/dev/peps/pep-0318/) is more readable, or just google "python decorator tutorial" and you'll find lots of blog posts that try to clarify things. – abarnert Oct 22 '14 at 00:59
  • @TheColonel26: The short version is that `@spam` followed by `def eggs(): pass` is basically the same as `def eggs(): pass` followed by `eggs = spam(eggs)`, if that helps. So, `spam` is a function that takes a function, and (usually) returns a wrapper function. – abarnert Oct 22 '14 at 01:00