add sync task in workflow
[vfc/nfvo/lcm.git] / lcm / workflows / graphflow / task / task.py
1 # Copyright 2018 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import datetime
16 from lcm.workflows.graphflow import STARTED, PROCESSING, FINISHED, ERROR, TIMEOUT_DEFAULT
17 import logging
18
19 logger = logging.getLogger(__name__)
20
21
22 class Task(object):
23     TASK_STATUS = (STARTED, PROCESSING, FINISHED, ERROR) = (STARTED, PROCESSING, FINISHED, ERROR)
24     TASK_ATTRIBUTES = (KEY, MANAGER, INPUT, TIMEOUT, ENDTIME, OUTPUT, STATUS) = ("key", "manager", "input", "timeout", "endtime", "output", "status")
25     INPUT_REST = (URL, METHOD, CONTENT) = ("url", "method", "content")
26     TIMEOUT_DEFAULT = TIMEOUT_DEFAULT
27     TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
28
29     def __init__(self, *args):
30         task = args[0]
31         self.key = task[self.KEY]
32         self.taskManager = task[self.MANAGER]
33         self.input = task[self.INPUT]
34         self.timeout = task[self.TIMEOUT] if self.TIMEOUT in task else self.TIMEOUT_DEFAULT
35         self.endtime = (datetime.datetime.now() + datetime.timedelta(seconds=self.timeout)).strftime(self.TIME_FORMAT)
36         self.status = STARTED
37         self.output = None
38
39     def execute(self):
40         pass
41
42     def update_task(self, status, output=None):
43         task = self.taskManager.get_task(self.key)
44         task.status = status
45         if output:
46             task.output = output
47         logger.debug("Update task %s status %s" % (task.key, task.status))
48         self.taskManager.update_task(self.key, task)