How to debug APScheduler when callback functions don't run

933 views
Skip to first unread message

Braun Brelin

unread,
May 10, 2013, 12:33:21 PM5/10/13
to apsch...@googlegroups.com
Hi all,
 
 
I've got a couple of mysterious issues with APScheduler 2.1.0
I've created an application running as a daemon that reads jobs from a text file called 'schedtab'  and loads them into APScheduler
 
The APScheduler no longer calls the callback function that I've been using for the last few weeks.
It fails silently, that is, it simply doesn't execute the callback_method. I know this because I've put in debugging statements
as the first line of code in the callback method. Since there aren't any errors logged , I have no idea how to debug this problem.
i've tried putting in debugging statements into the threadpool.py code where I'm guessing it runs the scheduled jobs as threads.
but nothing outputs.  running the print_jobs method shows that the job(s) are scheduled correctly, but when the time comes to run...
nothing.
 
 
I also have a second issue.
 
The first time that I load the file in and add jobs (via the default jobstore and the cron trigger)
I get a job schedule that looks normal, i.e. it puts the jobs into the schedule correctly.  However, if I reload the
jobs file, the next_run attribute in all of the  jobs changes to None and no jobs will run. Before reloading the file,
I clear all of the jobs out via a method that looks like this:
 
jobs = self.sched.get_jobs()   # sched is the Schedule object
for job in jobs:
    self.sched.unschedule_job(job)
 
I then reload the file with the jobs again via the add_cron_job method.   The reason for this is that it is possible that jobs
may have been deleted out of the  text file so I figured it's simply easiest to wipe out everything and reload the jobs
from scratch. 
 
Any ideas on how I can debug these problems?
 
Thanks,
 
Braun Brelin
 
 
.
 
 
 

Braun Brelin

unread,
May 10, 2013, 3:04:20 PM5/10/13
to apsch...@googlegroups.com
So, i've narrowed down the problem to the fact that running APScheduler inside a daemon doesn't seem to work correctly.
 
Here's some code examples:
 
This code works...
 
#!/usr/bin/env python
import sys
import time
import daemon
from apscheduler.scheduler import Scheduler
 
def main():
    while (True):
         pass
 
class testclass:
    def test_func(self,*args):
         for arg in args:
             print arg
   
    def __init__(self):
        self.s = Scheduler()
        self.s.add_cron_job(self.test_func,year=2013,month=05,day=10,hour=19,minute=53,second=00,args=("Hello World!\n","foo))
          self.s.start()
 
 
t = testclass()
main()
 
 
 
 
this code fails:
 
#!/usr/bin/env python
import sys
import time
import daemon
from apscheduler.scheduler import Scheduler
def main():
     while (True):
          pass
class testclass:
def test_func(self,*args):
for arg in args:
print arg
def __init__(self):
self.s = Scheduler()
self.s.add_cron_job(self.test_func,year=2013,month=05,day=10,hour=19,minute=53,second=00,args=("Hello World!\n","foo))
self.s.start()
t = testclass()
context = Daemon.DaemonContext(
            stdout = sys.stdout,
            stderr = sys.stderr
)
 
with context:
    main()
 
It's not an issue with the daemon closing stdout/stderr, because I can print something from __init__().
Any ideas?
 
Thanks,
 
Braun Brelin

Alex Grönholm

unread,
May 10, 2013, 4:11:00 PM5/10/13
to apsch...@googlegroups.com
Have you tried starting the scheduler thread *after* you've daemonized the process?
 
Thanks,
 
Braun Brelin
 
 
.
 
 
 
--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Braun Brelin

unread,
May 16, 2013, 8:37:23 AM5/16/13
to apsch...@googlegroups.com
Alex,
 
While putting the apscheduler instantiation inside the main function works, this leads me to all sorts of other issues.
For example, if I have code that looks like this:
 
import daemon
class bar():
     s = Scheduler()
     s.start()
    def print_sched(self):
         s.print_jobs()
def sigusr1_handler(signum,frame):
 
    foo.print_sched()
def main():
     foo = bar()
context = daemon.DaemonContext()
context.signal_map = {
     signal.SIGUSR1: sigusr1_handler
}
with context:
     if (__name__="__main__"):
         main()
 
I'll get a NameError when I send a SIGUSR1 to the program as it can't find foo which is defined in the main function.
That's why I was trying to instantiate the class before I created a daemon.  Do you know why the APScheduler
won't work when instantiated before the code becomes a daemon?
 
Thanks,
 
Braun

Alex Grönholm

unread,
May 16, 2013, 8:57:49 AM5/16/13
to apsch...@googlegroups.com
16.05.2013 15:37, Braun Brelin kirjoitti:
Alex,
 
While putting the apscheduler instantiation inside the main function works, this leads me to all sorts of other issues.
For example, if I have code that looks like this:
 
import daemon
class bar():
     s = Scheduler()
     s.start()
    def print_sched(self):
         s.print_jobs()
def sigusr1_handler(signum,frame):
 
    foo.print_sched()
def main():
     foo = bar()
context = daemon.DaemonContext()
context.signal_map = {
     signal.SIGUSR1: sigusr1_handler
}
with context:
     if (__name__="__main__"):
         main()
 
I'll get a NameError when I send a SIGUSR1 to the program as it can't find foo which is defined in the main function.
That's why I was trying to instantiate the class before I created a daemon.  Do you know why the APScheduler
won't work when instantiated before the code becomes a daemon?
You're still starting the scheduler before daemonization. Nothing has changed.

Braun Brelin

unread,
May 16, 2013, 9:47:24 AM5/16/13
to apsch...@googlegroups.com
Perhaps I'm mis-reading the code, but the instantiation of the scheduler happens in the main function, does it not?
I.e. main doesn't get called until the "with context:...main()"?  Does that not mean that the process has already daemonized itself?
 
Braun

Alex Grönholm

unread,
May 16, 2013, 3:31:01 PM5/16/13
to apsch...@googlegroups.com
16.05.2013 16:47, Braun Brelin kirjoitti:
Perhaps I'm mis-reading the code, but the instantiation of the scheduler happens in the main function, does it not?
No, it happens while the class "bar" is being defined, in: s.start()
Reply all
Reply to author
Forward
0 new messages