Exemplo n.º 1
0
class ScheInterface(object):
    def __init__(self, job_type, store_executor_alias, process_count):
        self.sche = TornadoScheduler()
        self.host = MONGO_CONFIG.get('host')
        self.mongo_client = MongoClient(self.host)
        self.job_type = job_type
        self.mongo_job_store = MongoDBJobStore(collection='job',
                                               database=DBNAME,
                                               client=self.mongo_client)
        self.store_executor_alise = store_executor_alias
        self.process_poll = ProcessPoolExecutor(process_count)

    def add_date_job(self, func, args, run_date, max_instances, listener_fun):
        self.sche.add_jobstore(self.mongo_job_store,
                               alias=self.store_executor_alise)
        self.sche.add_executor(self.process_poll,
                               alias=self.store_executor_alise)
        self.sche.add_listener(listener_fun,
                               EVENT_JOB_ERROR | EVENT_JOB_MISSED)
        try:
            self.sche.add_job(func,
                              self.job_type,
                              args=args,
                              run_date=run_date,
                              max_instances=max_instances,
                              jobstore=self.store_executor_alise)
            return True
        except Exception:
            return False
Exemplo n.º 2
0
class Application(tornado.web.Application):
    def __init__(self):
        jobstores = {
            'mongo': CustomStore(host='localhost', port=27017),
        }
        executors = {
            'default': ThreadPoolExecutor(20),
            'processpool': ProcessPoolExecutor(5)
        }
        job_defaults = {'coalesce': False, 'max_instances': 1}
        self.scheduler = TornadoScheduler(
            jobstores=jobstores,
            executors=executors,
            job_defaults=job_defaults,
            timezone=pytz.timezone('Asia/Shanghai'))
        self.scheduler.add_listener(
            self.schedulerListener,
            EVENT_JOB_EXECUTED | EVENT_JOB_ERROR | EVENT_JOB_MISSED)

        settings = dict(template_path=os.path.join(os.path.dirname(__file__),
                                                   "view"),
                        static_path=os.path.join(os.path.dirname(__file__),
                                                 "static"),
                        debug=False)

        conn = MongoClient("localhost", 27017)
        self.db = conn['orderMonitor']
        self.prepareJobResultStore()
        tornado.web.Application.__init__(self, handlers, **settings)

    def prepareJobResultStore(self):
        self.db.jobResultStore.create_index("jobId")
        self.db.jobResultStore.create_index("status")
        self.db.jobResultStore.create_index("lastRunTime")

    def schedulerListener(self, ev):
        jobResult = {
            "jobId": ev.job_id,
            "lastRunTime": ev.scheduled_run_time,
            "status": "",
            "desc": ""
        }
        if ev.code & EVENT_JOB_EXECUTED:
            jobResult['status'] = "success"
            jobResult['desc'] = str(ev.retval)
        elif ev.code & EVENT_JOB_ERROR:
            jobResult['status'] = "failure"
            jobResult['desc'] = repr(ev.exception) + '\n' + str(ev.traceback)

        self.db.jobResultStore.insert(jobResult)