diff --git a/opt/ahenk/base/Scope.py b/opt/ahenk/base/Scope.py index f02e1f4..d12fd02 100644 --- a/opt/ahenk/base/Scope.py +++ b/opt/ahenk/base/Scope.py @@ -22,6 +22,7 @@ class Scope(object): self.executionManager = None self.dbService = None self.messager = None + self.scheduler = None @staticmethod def getInstance(): @@ -106,3 +107,9 @@ class Scope(object): def setMessager(self, messager): self.messager = messager + + def set_scheduler(self, scheduler): + self.scheduler = scheduler + + def get_scheduler(self): + return self.scheduler \ No newline at end of file diff --git a/opt/ahenk/base/scheduler/custom/custom_scheduler.py b/opt/ahenk/base/scheduler/custom/custom_scheduler.py index 05b8dda..c14ce18 100644 --- a/opt/ahenk/base/scheduler/custom/custom_scheduler.py +++ b/opt/ahenk/base/scheduler/custom/custom_scheduler.py @@ -27,6 +27,16 @@ class CustomScheduler(BaseScheduler): def add_job(self, job): self.events.append(job) + def save_and_add_job(self, task): + self.scheduledb.save(task) + self.events.append(ScheduleTaskJob(task)) + + def remove_job(self,task): + self.scheduledb.delete(task) + for event in self.events: + if event.task.id == task.id: + self.events.remove(event) + def stop(self): self.keep_run = False diff --git a/opt/ahenk/base/scheduler/custom/schedule_job.py b/opt/ahenk/base/scheduler/custom/schedule_job.py index b99d9c4..92a2881 100644 --- a/opt/ahenk/base/scheduler/custom/schedule_job.py +++ b/opt/ahenk/base/scheduler/custom/schedule_job.py @@ -23,10 +23,12 @@ class ScheduleTaskJob(object): def processTask(self): try: self.task_manager.addTask(self.task) + if self.is_single_shot(): + Scope.getInstance().get_scheduler().remove_job(self.task) except Exception as e: self.logger.error(e) - def parse_cron_str(self,cron_str): + def parse_cron_str(self, cron_str): try: cron_exp_arr = cron_str.split(" ") cron_sj = [] @@ -66,6 +68,11 @@ class ScheduleTaskJob(object): obj = set(obj) return obj + def is_single_shot(self): + if '*' in self.task.cron_str: + return True + else: + return False def matchtime(self, t): """Return True if this event should trigger at the specified datetime""" diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 2c5bcc0..fd3e103 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -15,14 +15,18 @@ class TaskManager(object): self.pluginManager = scope.getPluginManager() self.logger = scope.getLogger() self.db_service = scope.getDbService() + self.scheduler = scope.get_scheduler() def addTask(self, task): try: - self.logger.debug('Adding task ... ') - self.saveTask(task) - self.logger.info('Task saved ') - # TODO send task received message - self.pluginManager.processTask(task) + if not task.cron_str == None or task.cron_str == '': + self.logger.debug('Adding task ... ') + self.saveTask(task) + self.logger.info('Task saved ') + # TODO send task received message + self.pluginManager.processTask(task) + else: + self.scheduler.save_and_add_job(task) except Exception as e: # TODO error log here