2012-04-11 44 views
3

我正在使用sqlalchemy job store APS。使用函數add_cron_job添加作業也是cron。但是在數據庫表中沒有輸入。請幫我出:以下是我從來沒有使用APScheduler代碼Advance Python Scheduler和SQLAlchemyJobStore

import time 
import logging 
from threading import Timer 
from threading import Lock 
from gadgetplatform.classes.utils.utilities import import_module 
from apscheduler.scheduler import Scheduler 

from apscheduler.jobstores.sqlalchemy_store import SQLAlchemyJobStore 

class SchedulerManager(object): 
    _instance = None 
    _initialised = False 
    lock = Lock() 
    log = logging.getLogger(__name__) 
    jobDict=[] 

    def __new__(cls): 

     if not cls._instance or not cls._initialised: 
      cls.lock.acquire() 
      if not cls._instance: 
       cls.log.info("creating instance") 
       cls._instance = super(SchedulerManager, cls).__new__(cls) 
       cls._initialised = True 
       cls._instance.init() 
       cls.log.info("instance created") 
      cls.lock.release() 
      cls.log.info("lock released") 
      cls.log.info("returning instance") 
     return cls._instance  

    def init(self): 
     self.sched=Scheduler() 
     self.sched.add_jobstore(SQLAlchemyJobStore('mysql://[email protected]/mygola?charset=utf8&use_unicode=0'), 'apschedulerJobs')   

    def addToCron(self,source,time): 
     self.log.info("called to add schedular") 

     time = time.split(' ') 

     m=str(time[0])   
     h=str(time[1])   
     d=str(time[2])   
     mnth=str(time[3])     
     yr=str(time[4]) 

     func=self.convertStringToFunction(source) 
     self.sched.add_cron_job(func, year=yr, month=mnth, day=d, hour=h, minute=m)   
     self.jobDict.append(source) 

     self.log.info("added with the time")    

    def removeFromCron(self,source):   
     func=self.convertStringToFunction(source) 
     self.sched.unschedule_func(func) 

    def start(self): 
     self.sched.start() 
     self.log.info("Schedular Started") 

    def stop(self): 
     self.sched.shutdown() 

    def getRunningJobs(self): 
     return self.jobDict 

    def convertStringToFunction(self,source): 
     strArr = source.rsplit('.',1) 
     mod = import_module(strArr[0]) 
     func = getattr(mod, strArr[1]) 
     return func 

回答

2

,但是從文檔,它看起來好像你必須指定什麼樣的工作存儲到作業添加到。

您可以確保在SQL鍊金術作業店給它特殊的名字default是默認的:

self.sched.add_jobstore(SQLAlchemyJobStore('...'), 'default') 

或者您也可以添加作業時指定作業店名:

self.sched.add_cron_job(func, 
         jobstore="apschedulerJobs", 
         year=yr, month=mnth, day=d, hour=h, minute=m)