jay_t
jay_t

Reputation: 3793

Python Advanced scheduler job not found in jobstore (apscheduler)

Consider this small piece of code

from apscheduler.scheduler import Scheduler
import time

class First():
    def __init__(self):
        self.remove_job=None
    def go(self):
        self.remove_job('test')
class Sched():
    def __init__(self):
        self.sched = Scheduler()
        self.sched.add_interval_job(    self.execute,
                        seconds=1,
                        name = 'test'
                        )
    def execute(self):
        print "i'm alive"
    def remove_job(self,job):
        self.sched.print_jobs()
        self.sched.unschedule_job(job)

def main():
    first = First()
    sched = Sched()
    first.remove_job=sched.remove_job
    sched.sched.start()
    time.sleep(5)
    first.go()
    return 0

if __name__ == '__main__':
    main()

python sched_test.py 
i'm alive
i'm alive
i'm alive
i'm alive
i'm alive
Jobstore default:
    test (trigger: interval[0:00:01], next run at: 2011-12-22 01:25:36.577572)
Traceback (most recent call last):
  File "sched_test.py", line 55, in <module>
    main()
  File "sched_test.py", line 51, in main
    first.go()
  File "sched_test.py", line 31, in go
    self.remove_job('test')
  File "sched_test.py", line 43, in remove_job
    self.sched.unschedule_job(job)
  File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/scheduler.py", line 401, in unschedule_job
    raise KeyError('Job "%s" is not scheduled in any job store' % job)
KeyError: 'Job "test" is not scheduled in any job store'

Why am I getting this error while printing out the jobs works? print_jobs() gives me the right overview though.

Can someone shed some light on this problem?

Upvotes: 1

Views: 2036

Answers (3)

Power Serge
Power Serge

Reputation: 51

This might be obvious for most of the people, but it took me a while to get it. Wanted just to share what made my code work:

myJobName= "Homework"

for job in self.sched.get_jobs():
    if job.name == "Homework":
        self.sched.unschedule_job(job)
        print "No more homework!"

Upvotes: 3

jay_t
jay_t

Reputation: 3793

you must pass the job instance (returned by add_interval_job) to unschedule_job instead of a string. That fixes the problem.

Upvotes: 1

sarnold
sarnold

Reputation: 104020

Note that your First class doesn't actually have a sched instance; it certainly doesn't have access to the sched.sched instance of the Scheduler that you probably want to manipulate.

class First():
    def __init__(self):
        self.remove_job=None
    def go(self):
        self.remove_job('test')

Perhaps you should construct the Sched object first, so you can pass it to the First() constructor, so you can make calls to it. I'll sketch out an untested mechanism I think would solve this:

class First():
    def __init__(self, sched):
        self.sched = sched
    def go(self):
        self.sched.remove_job('test')
def main():
    sched = Sched()
    first = First(sched)
    sched.sched.start()
    time.sleep(5)
    first.go()
    return 0

This keeps the Sched class alone -- perhaps a cleaner design could be found by merging First and Sched -- the fact that First knows the name of a job that Sched controls is a sign that something isn't quite right.

Maybe take a step back and explain what problem you're trying to solve? This doesn't feel like the cleanest solution, so I have to wonder if the problem you've got could be solved through a better mechanism.

Upvotes: 0

Related Questions