custom scheduler impl

This commit is contained in:
İsmail Başaran 2016-04-05 14:30:12 +03:00
parent f07868f46c
commit 75ce3d84fa
9 changed files with 147 additions and 30 deletions

View file

@ -1,28 +0,0 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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

View file

View file

@ -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

View file

@ -0,0 +1,40 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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)

View file

@ -0,0 +1,80 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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)

View file

@ -0,0 +1,18 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
class ScheduleDB(object):
def save(self):
pass
def delete(self):
pass
def load(self):
pass
def check_table_exists(self):
pass

View file

@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
from base.scheduler.APSchedulerImpl import APSchedulerImpl
from base.scheduler.custom_scheduler_impl import CustomScheduler
class SchedulerFactory():
def get_intstance(self):
return APSchedulerImpl()
return CustomScheduler()