Ahenk/opt/ahenk/base/scheduler/custom/scheduledb.py
İsmail Başaran 44891af172 scheduler db
2016-04-05 17:28:22 +03:00

50 lines
2.2 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
from base.Scope import Scope
from base.model.Task import Task
class ScheduleTaskDB(object):
def __init__(self):
scope = Scope.getInstance()
self.logger = scope.getLogger()
self.db_service = scope.getDbService()
def initialize(self):
self.logger.debug('[ScheduleTaskDB] Initializing scheduler database...')
self.db_service.check_and_create_table('schedule_task', ['id INTEGER', 'task_json TEXT'])
self.logger.debug('[ScheduleTaskDB] Scheduler database is ok.')
def save(self, task):
self.logger.debug('[ScheduleTaskDB] Preparing schedule task for save operation... creating columns and values...')
cols = ['id', 'task_json']
values = [str(task.id), str(task.to_json())]
self.logger.debug('[ScheduleTaskDB] Saving scheduler task to db... ')
self.db_service.update('schedule_task', cols, values, None)
self.logger.debug('[ScheduleTaskDB] Scheduler task saved.')
def delete(self, task):
try:
self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task.id))
self.db_service.delete('schedule_task', 'id=' + str(task.id))
self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task.id))
except Exception as e:
self.logger.error('[ScheduleTaskDB] Exception occur when deleting schedule task ' + str(e))
def load(self):
try:
self.logger.debug('[ScheduleTaskDB] Loading schedule tasks...')
rows = self.db_service.select('schedule_task')
tasks = []
for row in rows:
task_json = row['task_json']
task = Task(None)
task.from_json(task_json)
tasks.append(task)
self.logger.debug(
'[ScheduleTaskDB] Schedule tasks loaded successfully. Schedule Tasks size=' + str(len(tasks)))
return tasks
except Exception as e:
self.logger.error('[ScheduleTaskDB] Exception occur when loading schedule tasks! ' + str(e))