From c003ba53051d1e845bc9e56cb8e58bdb128454b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Mon, 21 Mar 2016 12:02:15 +0200 Subject: [PATCH] receiving task and save to DB --- opt/ahenk/base/database/AhenkDbService.py | 4 +- opt/ahenk/base/execution/ExecutionManager.py | 3 +- opt/ahenk/base/model/Task.py | 43 +++++++++++++++++--- opt/ahenk/base/task/TaskManager.py | 28 +++++++------ 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py index d0e351d..de134d2 100644 --- a/opt/ahenk/base/database/AhenkDbService.py +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -17,10 +17,8 @@ class AhenkDbService(object): self.connection=None self.cursor = None - - def initialize_table(self): - self.check_and_create_table('task',['task TEXT','timestamp TEXT']) + self.check_and_create_table('task',['id INTEGER','create_date TEXT','modify_date TEXT','command_cls_id TEXT','parameter_map BLOB','deleted INTEGER','plugin TEXT']) self.check_and_create_table('policy',['id INTEGER PRIMARY KEY AUTOINCREMENT','type TEXT','version TEXT','name TEXT']) self.check_and_create_table('profile',['id INTEGER','create_date TEXT','label TEXT','description TEXT','overridable INTEGER','active INTEGER','deleted INTEGER','profile_data BLOB','modify_date TEXT']) self.check_and_create_table('plugin',['version TEXT','name TEXT','description TEXT']) diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index 4f76df6..6f47e1b 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -83,8 +83,9 @@ class ExecutionManager(object): def execute_task(self,arg): self.logger.debug('[ExecutionManager] Adding new task...') - task = Task(arg) + task = Task(json.loads(arg)) self.task_manager.addTask(task) + self.logger.debug('[ExecutionManager] Task added') def move_file(self,arg): default_file_path=self.config_manager.get('CONNECTION', 'receiveFileParam') diff --git a/opt/ahenk/base/model/Task.py b/opt/ahenk/base/model/Task.py index e356456..c88c122 100644 --- a/opt/ahenk/base/model/Task.py +++ b/opt/ahenk/base/model/Task.py @@ -1,18 +1,49 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN +# Author: Volkan Şahin +from base.model.Plugin import Plugin import json class Task(object): """docstring for Task""" def __init__(self,message): - self.payload = json.loads(message) - self.request = self.payload['request'] + self.task = message['task'] @property - def task(self): - self.request['task'] + def id(self): + return self.task['id'] @property - def timestamp(self): - self.request['timestamp'] + def create_date(self): + return self.task['createdate'] + + @property + def modify_date(self): + return self.task['modifydate'] + + @property + def command_cls_id(self): + return self.task['commandclsid'] + + @property + def parameter_map(self): + return self.task['parametermap'] + + @property + def deleted(self): + return self.task['deleted'] + + @property + def plugin(self): + return Plugin(self.task['plugin']) + + def to_string(self): + return str(self.task) + + def to_json(self): + return json.load(self.task) + + + + diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 5612147..a63a4c9 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -5,18 +5,20 @@ from base.Scope import Scope from base.model.MessageFactory import MessageFactory from base.model.MessageType import MessageType + class TaskManager(object): """docstring for TaskManager""" + def __init__(self): super(TaskManager, self).__init__() scope = Scope.getInstance() self.pluginManager = scope.getPluginManager() - self.logger= scope.getLogger() - self.dbService = scope.getDbService() + self.logger = scope.getLogger() + self.db_service = scope.getDbService() - def addTask(self,task): + def addTask(self, task): try: - self.logger.debug('Adding task ... ' + str(task.plugin_ame) + ' - ' + str(task.command_id)) + self.logger.debug('Adding task ... ') self.saveTask(task) # TODO send task received message self.pluginManager.processTask(task) @@ -24,26 +26,26 @@ class TaskManager(object): # TODO error log here pass - def saveTask(self,task): - # TODO not implemented yet - # task reveiced to ahenk save to db firstly. - # if user close before processing task you can load from db for process - pass + def saveTask(self, task): + cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin'] + values = [str(task.id), str(task.create_date), str(task.modify_date), str(task.command_cls_id), str(task.parameter_map), str(task.deleted), task.plugin.to_string()] + self.db_service.update('task', cols, values, None) + self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.id + ')') - def updateTask(self,task): + def updateTask(self, task): # TODO not implemented yet # This is updates task status processing - processed ... pass - def deleteTask(self,task): + def deleteTask(self, task): # TODO not implemented yet # remove task if it is processed pass - def sendMessage(self,type,message): + def sendMessage(self, type, message): # TODO not implemented yet pass if __name__ == '__main__': - print(MessageFactory.createMessage(MessageType.TASK_PROCESSING,"my message")) + print(MessageFactory.createMessage(MessageType.TASK_PROCESSING, "my message"))