def __init__(self, port, error_handler=null_error_handler): """ :param port port to be used for web server :param error_handler error handler to be called in case of job failure function(ScheduledJob, Exception) """ self.port = port self.jobs = {} self.error_handler = error_handler self.scheduler = Scheduler() self.scheduler_thread = None
class Cronen(object): def __init__(self, port, error_handler=null_error_handler): """ :param port port to be used for web server :param error_handler error handler to be called in case of job failure function(ScheduledJob, Exception) """ self.port = port self.jobs = {} self.error_handler = error_handler self.scheduler = Scheduler() self.scheduler_thread = None def add_job(self, name, func, trigger): job = ScheduledJob(name, func, trigger, self.error_handler) self.jobs[name] = job self.scheduler.add_job(job) def start(self): """ Start scheduled task and web server. Call is blocking. """ self._start_scheduler() self._start_web_server() def _start_scheduler(self): self.scheduler_thread = threading.Thread(target=self._scheduler_loop, name='Cronen Scheduler') self.scheduler_thread.daemon = True self.scheduler_thread.start() def _scheduler_loop(self): while True: self.scheduler.run_pending() sleep(1) def _start_web_server(self): @bottle.route('/run/<name>') def run(name): logging.info('Running job {} manually'.format(name)) self.scheduler.schedule_now(self.jobs[name]) @bottle.route('/status') def status(): bottle.response.content_type = 'application/json' return json.dumps({ job.name: self.run_result_to_dict(job.last_run) for job in self.jobs.values() }) @bottle.route('/') def web_status(): return bottle.template(WEB_STATUS_TEMPLATE, jobs=self.jobs.values()) bottle.run(host='localhost', port=self.port) @staticmethod def run_result_to_dict(run_result): return run_result._asdict()
class Cronen(object): def __init__(self, port, error_handler=null_error_handler): """ :param port port to be used for web server :param error_handler error handler to be called in case of job failure function(ScheduledJob, Exception) """ self.port = port self.jobs = {} self.error_handler = error_handler self.scheduler = Scheduler() self.scheduler_thread = None def add_job(self, name, func, trigger): job = ScheduledJob(name, func, trigger, self.error_handler) self.jobs[name] = job self.scheduler.add_job(job) def start(self): """ Start scheduled task and web server. Call is blocking. """ self._start_scheduler() self._start_web_server() def _start_scheduler(self): self.scheduler_thread = threading.Thread( target=self._scheduler_loop, name='Cronen Scheduler' ) self.scheduler_thread.daemon = True self.scheduler_thread.start() def _scheduler_loop(self): while True: self.scheduler.run_pending() sleep(1) def _start_web_server(self): @bottle.route('/run/<name>') def run(name): logging.info('Running job {} manually'.format(name)) self.scheduler.schedule_now(self.jobs[name]) @bottle.route('/status') def status(): bottle.response.content_type = 'application/json' return json.dumps( {job.name: self.state_to_dict(job.state) for job in self.jobs.values()} ) @bottle.route('/') def web_status(): return bottle.template(WEB_STATUS_TEMPLATE, jobs=self.jobs.values()) bottle.run(host='0.0.0.0', port=self.port) @staticmethod def state_to_dict(job_state): return job_state._asdict()