2016-02-29 17:13:39 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-06-22 18:12:06 +03:00
|
|
|
|
2016-08-24 18:05:54 +03:00
|
|
|
from base.scope import Scope
|
2016-08-24 18:00:01 +03:00
|
|
|
from base.model.message_factory import MessageFactory
|
|
|
|
from base.model.enum.message_type import MessageType
|
2016-02-29 17:13:39 +02:00
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
|
2016-02-29 17:13:39 +02:00
|
|
|
class TaskManager(object):
|
|
|
|
"""docstring for TaskManager"""
|
2016-03-21 12:02:15 +02:00
|
|
|
|
2016-02-29 17:13:39 +02:00
|
|
|
def __init__(self):
|
2016-03-23 17:15:27 +02:00
|
|
|
# super(TaskManager, self).__init__()
|
2016-03-02 11:59:19 +02:00
|
|
|
scope = Scope.getInstance()
|
|
|
|
self.pluginManager = scope.getPluginManager()
|
2016-03-21 12:02:15 +02:00
|
|
|
self.logger = scope.getLogger()
|
|
|
|
self.db_service = scope.getDbService()
|
2016-04-05 18:47:05 +03:00
|
|
|
self.scheduler = scope.get_scheduler()
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
def addTask(self, task):
|
2016-03-02 11:59:19 +02:00
|
|
|
try:
|
2016-06-22 18:12:06 +03:00
|
|
|
self.saveTask(task)
|
2016-07-01 17:04:53 +03:00
|
|
|
if task.get_cron_str() is None or task.get_cron_str() == '':
|
2016-06-22 18:12:06 +03:00
|
|
|
self.logger.debug('[TaskManager] Adding task ... ')
|
2016-07-18 18:16:37 +03:00
|
|
|
self.pluginManager.process_task(task)
|
2016-04-05 18:47:05 +03:00
|
|
|
else:
|
|
|
|
self.scheduler.save_and_add_job(task)
|
2016-03-30 17:34:10 +03:00
|
|
|
|
2016-03-02 11:59:19 +02:00
|
|
|
except Exception as e:
|
2016-08-10 12:33:21 +03:00
|
|
|
self.logger.debug('[TaskManager] Exception occurred when adding task. Error Message: {0}'.format(str(e)))
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-03-30 17:34:10 +03:00
|
|
|
def addPolicy(self, policy):
|
2016-03-29 11:52:18 +03:00
|
|
|
try:
|
2016-07-18 18:16:37 +03:00
|
|
|
self.pluginManager.process_policy(policy)
|
2016-03-29 11:52:18 +03:00
|
|
|
except Exception as e:
|
2016-08-10 12:33:21 +03:00
|
|
|
self.logger.error("[TaskManager] Exception occurred when adding policy. Error Message: {0}".format(str(e)))
|
2016-03-29 11:52:18 +03:00
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
def saveTask(self, task):
|
2016-06-22 18:12:06 +03:00
|
|
|
try:
|
2016-08-10 12:33:21 +03:00
|
|
|
task_cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin',
|
|
|
|
'cron_expr', 'file_server']
|
|
|
|
plu_cols = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name',
|
|
|
|
'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based']
|
|
|
|
plugin_args = [str(task.get_plugin().get_active()), str(task.get_plugin().get_create_date()),
|
|
|
|
str(task.get_plugin().get_deleted()), str(task.get_plugin().get_description()),
|
|
|
|
str(task.get_plugin().get_machine_oriented()), str(task.get_plugin().get_modify_date()),
|
|
|
|
str(task.get_plugin().get_name()), str(task.get_plugin().get_policy_plugin()),
|
|
|
|
str(task.get_plugin().get_user_oriented()), str(task.get_plugin().get_version()),
|
|
|
|
str(task.get_plugin().get_task_plugin()), str(task.get_plugin().get_x_based())]
|
2016-06-22 18:12:06 +03:00
|
|
|
plugin_id = self.db_service.update('plugin', plu_cols, plugin_args)
|
2016-08-10 12:33:21 +03:00
|
|
|
values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()),
|
|
|
|
str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()),
|
|
|
|
str(plugin_id), str(task.get_cron_str()), str(task.get_file_server())]
|
2016-06-27 17:21:10 +03:00
|
|
|
self.db_service.update('task', task_cols, values)
|
2016-06-22 18:12:06 +03:00
|
|
|
except Exception as e:
|
2016-08-10 12:33:21 +03:00
|
|
|
self.logger.error("[TaskManager] Exception occurred while saving task. Error Message: {0}".format(str(e)))
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
def updateTask(self, task):
|
2016-03-02 11:59:19 +02:00
|
|
|
# TODO not implemented yet
|
|
|
|
# This is updates task status processing - processed ...
|
|
|
|
pass
|
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
def deleteTask(self, task):
|
2016-03-02 11:59:19 +02:00
|
|
|
# TODO not implemented yet
|
|
|
|
# remove task if it is processed
|
|
|
|
pass
|
2016-03-14 10:55:28 +02:00
|
|
|
|
2016-03-21 12:02:15 +02:00
|
|
|
def sendMessage(self, type, message):
|
2016-03-14 10:55:28 +02:00
|
|
|
# TODO not implemented yet
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-03-21 12:02:15 +02:00
|
|
|
print(MessageFactory.createMessage(MessageType.TASK_PROCESSING, "my message"))
|