Ahenk/opt/ahenk/base/scheduler/custom/custom_scheduler.py

46 lines
1.2 KiB
Python
Raw Normal View History

2016-04-05 14:30:12 +03:00
#!/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
2016-04-05 17:28:22 +03:00
from base.scheduler.custom.scheduledb import ScheduleTaskDB
from base.scheduler.custom.schedule_job import ScheduleTaskJob
2016-04-05 14:30:12 +03:00
from datetime import datetime, timedelta
import time
class CustomScheduler(BaseScheduler):
def __init__(self):
self.events = []
self.keep_run = True
self.logger = Scope.getInstance().getLogger()
2016-04-05 17:28:22 +03:00
self.scheduledb = ScheduleTaskDB()
2016-04-05 14:30:12 +03:00
def initialize(self):
2016-04-05 17:28:22 +03:00
self.scheduledb.initialize()
tasks = self.scheduledb.load()
for task in tasks:
self.add_job(ScheduleTaskJob(task))
2016-04-05 14:30:12 +03:00
2016-04-05 17:28:22 +03:00
def add_job(self, job):
2016-04-05 14:30:12 +03:00
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)