From 75ce3d84fabd645d80efdaf378799063c8614d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Ba=C5=9Faran?= Date: Tue, 5 Apr 2016 14:30:12 +0300 Subject: [PATCH] custom scheduler impl --- opt/ahenk/base/scheduler/APSchedulerImpl.py | 28 ------- opt/ahenk/base/scheduler/__init__.py | 0 .../{BaseScheduler.py => base_scheduler.py} | 0 opt/ahenk/base/scheduler/custom/__init__.py | 0 opt/ahenk/base/scheduler/custom/all_match.py | 7 ++ .../base/scheduler/custom/custom_scheduler.py | 40 ++++++++++ .../base/scheduler/custom/schedule_job.py | 80 +++++++++++++++++++ opt/ahenk/base/scheduler/custom/scheduledb.py | 18 +++++ ...hedulerFactory.py => scheduler_factory.py} | 4 +- 9 files changed, 147 insertions(+), 30 deletions(-) delete mode 100644 opt/ahenk/base/scheduler/APSchedulerImpl.py create mode 100644 opt/ahenk/base/scheduler/__init__.py rename opt/ahenk/base/scheduler/{BaseScheduler.py => base_scheduler.py} (100%) create mode 100644 opt/ahenk/base/scheduler/custom/__init__.py create mode 100644 opt/ahenk/base/scheduler/custom/all_match.py create mode 100644 opt/ahenk/base/scheduler/custom/custom_scheduler.py create mode 100644 opt/ahenk/base/scheduler/custom/schedule_job.py create mode 100644 opt/ahenk/base/scheduler/custom/scheduledb.py rename opt/ahenk/base/scheduler/{SchedulerFactory.py => scheduler_factory.py} (65%) diff --git a/opt/ahenk/base/scheduler/APSchedulerImpl.py b/opt/ahenk/base/scheduler/APSchedulerImpl.py deleted file mode 100644 index d75eed9..0000000 --- a/opt/ahenk/base/scheduler/APSchedulerImpl.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -# Author: İsmail BAŞARAN - -from base.scheduler.BaseScheduler import BaseScheduler - -class APSchedulerImpl(BaseScheduler): - - def initialize(self): - # Not implemented yet - pass - - def add_job(self): - # Not implemented yet - pass - - def add_job_by_hour(self): - # Not implemented yet - pass - - def add_job_by_mount(self): - # Not implemented yet - pass - - def add_job_by_minute(self): - # Not implemented yet - pass - diff --git a/opt/ahenk/base/scheduler/__init__.py b/opt/ahenk/base/scheduler/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/opt/ahenk/base/scheduler/BaseScheduler.py b/opt/ahenk/base/scheduler/base_scheduler.py similarity index 100% rename from opt/ahenk/base/scheduler/BaseScheduler.py rename to opt/ahenk/base/scheduler/base_scheduler.py diff --git a/opt/ahenk/base/scheduler/custom/__init__.py b/opt/ahenk/base/scheduler/custom/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/opt/ahenk/base/scheduler/custom/all_match.py b/opt/ahenk/base/scheduler/custom/all_match.py new file mode 100644 index 0000000..f4c549d --- /dev/null +++ b/opt/ahenk/base/scheduler/custom/all_match.py @@ -0,0 +1,7 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +# Some utility classes / functions first +class AllMatch(set): + """Universal set - match everything""" + def __contains__(self, item): return True \ 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 new file mode 100644 index 0000000..de8603a --- /dev/null +++ b/opt/ahenk/base/scheduler/custom/custom_scheduler.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + +from base.scheduler.base_scheduler import BaseScheduler +from base.Scope import Scope +from datetime import datetime, timedelta +import time + + +class CustomScheduler(BaseScheduler): + + def __init__(self): + self.events = [] + self.keep_run = True + self.logger = Scope.getInstance().getLogger() + + def initialize(self): + # Implement this from your implementation class + pass + + def add_job(self,job): + self.events.append(job) + + def stop(self): + self.keep_run = False + + def run(self): + t = datetime(*datetime.now().timetuple()[:5]) + while 1 and self.keep_run: + for e in self.events: + e.check(t) + + t += timedelta(minutes=1) + while datetime.now() < t: + time.sleep((t - datetime.now()).seconds) + + + + diff --git a/opt/ahenk/base/scheduler/custom/schedule_job.py b/opt/ahenk/base/scheduler/custom/schedule_job.py new file mode 100644 index 0000000..b99d9c4 --- /dev/null +++ b/opt/ahenk/base/scheduler/custom/schedule_job.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + +from base.scheduler.custom.all_match import AllMatch +from base.Scope import Scope + +class ScheduleTaskJob(object): + def __init__(self, task): + self.task = task + cron_sj = self.parse_cron_str(task.cron_str) + if cron_sj: + self.mins = self.conv_to_set(cron_sj[0]) + self.hours= self.conv_to_set(cron_sj[1]) + self.days = self.conv_to_set(cron_sj[2]) + self.months = self.conv_to_set(cron_sj[3]) + self.dow = self.conv_to_set(cron_sj[4]) + self.action = self.processTask + scope = Scope.getInstance() + self.logger = scope.getLogger() + self.task_manager = scope.getTaskManager() + + def processTask(self): + try: + self.task_manager.addTask(self.task) + except Exception as e: + self.logger.error(e) + + def parse_cron_str(self,cron_str): + try: + cron_exp_arr = cron_str.split(" ") + cron_sj = [] + count = 0 + for exp in cron_exp_arr: + if exp.isdigit(): + cron_sj.append(exp) + elif '*' == exp: + cron_sj.append(AllMatch()) + elif '/' in exp: + range_val = int(exp.split("/")[1]) + if count == 0: + cron_sj.append(range(0, 60, range_val)) + elif count == 1: + cron_sj.append(range(0, 24, range_val)) + elif count == 2: + cron_sj.append(range(0, 7, range_val)) + elif count == 3: + cron_sj.append(range(0, 12, range_val)) + elif count == 3: + cron_sj.append(range(0, 7, range_val)) + else: + print("it is not supported") + elif ',' in exp: + cron_sj.append("(" + str(exp) + ")") + else: + print("it is not supported") + count = count + 1 + return cron_sj + except Exception as e: + self.logger.error(str(e)) + + def conv_to_set(obj): + if isinstance(obj, (int)): + return set([obj]) + if not isinstance(obj, set): + obj = set(obj) + return obj + + + def matchtime(self, t): + """Return True if this event should trigger at the specified datetime""" + return ((t.minute in self.mins) and + (t.hour in self.hours) and + (t.day in self.days) and + (t.month in self.months) and + (t.weekday() in self.dow)) + + def check(self, t): + if self.matchtime(t): + self.action(*self.args, **self.kwargs) \ No newline at end of file diff --git a/opt/ahenk/base/scheduler/custom/scheduledb.py b/opt/ahenk/base/scheduler/custom/scheduledb.py new file mode 100644 index 0000000..e47e6f8 --- /dev/null +++ b/opt/ahenk/base/scheduler/custom/scheduledb.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + + +class ScheduleDB(object): + + def save(self): + pass + + def delete(self): + pass + + def load(self): + pass + + def check_table_exists(self): + pass diff --git a/opt/ahenk/base/scheduler/SchedulerFactory.py b/opt/ahenk/base/scheduler/scheduler_factory.py similarity index 65% rename from opt/ahenk/base/scheduler/SchedulerFactory.py rename to opt/ahenk/base/scheduler/scheduler_factory.py index c6fb0b6..799decc 100644 --- a/opt/ahenk/base/scheduler/SchedulerFactory.py +++ b/opt/ahenk/base/scheduler/scheduler_factory.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN -from base.scheduler.APSchedulerImpl import APSchedulerImpl +from base.scheduler.custom_scheduler_impl import CustomScheduler class SchedulerFactory(): def get_intstance(self): - return APSchedulerImpl() \ No newline at end of file + return CustomScheduler() \ No newline at end of file