def doCall(self): session = db.get_session() item = session.query(db.PluginStatus).filter(db.PluginStatus.plugin_name==self.job_name).one() if item.is_running: session.commit() self.log.error("Plugin %s is already running! Not doing re-entrant call!", self.job_name) return item.is_running = True item.last_run = datetime.datetime.now() session.commit() try: self._doCall() except Exception: item.last_error = datetime.datetime.now() item.last_error_msg = traceback.format_exc() raise finally: item.is_running = False item.last_run_end = datetime.datetime.now() session.commit() db.delete_session()
def scheduleJobs(sched, timeToStart): jobs = [] offset = 0 for key, value in activePlugins.scrapePlugins.items(): baseModule, interval = value jobs.append((key, baseModule, interval, timeToStart+datetime.timedelta(seconds=60*offset))) offset += 1 activeJobs = [] print("JobCaller: ", JobCaller) print("JobCaller.callMod: ", JobCaller.callMod) for jobId, callee, interval, startWhen in jobs: jId = callee.__name__ print("JobID = ", jId) activeJobs.append(jId) if not sched.get_job(jId): sched.add_job(do_call, args = (callee.__name__, ), trigger = 'interval', seconds = interval, start_date = startWhen, id = jId, max_instances = 1, replace_existing = True, jobstore = "main_jobstore", misfire_grace_time = 2**30) for job in sched.get_jobs('main_jobstore'): if not job.id in activeJobs: sched.remove_job(job.id, 'main_jobstore') print("JobSetup call resetting run-states!") session = db.get_session() session.query(db.PluginStatus).update({db.PluginStatus.is_running : False}) session.commit() db.delete_session() print("Run-states reset.")
def __init__(self, job_name): if not job_name in CALLABLE_LUT: raise JobNameException("Callable '%s' is not in the class lookup table: '%s'!" % (job_name, CALLABLE_LUT)) self.runModule = CALLABLE_LUT[job_name] self.job_name = job_name session = db.get_session() try: query = session.query(db.PluginStatus).filter(db.PluginStatus.plugin_name==job_name) have = query.scalar() if not have: new = db.PluginStatus(plugin_name=job_name) session.add(new) session.commit() except sqlalchemy.exc.OperationalError: session.rollback() except sqlalchemy.exc.InvalidRequestError: session.rollback() finally: db.delete_session()