Debian Codegraph @

taskflow/engines/action_engine/actions/task.py

raw
1 # Copyright (C) 2012-2013 Yahoo! Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import functools
16
17 from taskflow.engines.action_engine.actions import base
18 from taskflow import logging
19 from taskflow import states
20 from taskflow import task as task_atom
21 from taskflow.types import failure
22
23 LOG = logging.getLogger(__name__)
24
25
26 class TaskAction(base.Action):
27 """An action that handles scheduling, state changes, ... of task atoms."""
28
29 def __init__(self, storage, notifier, task_executor):
30 super().__init__(storage, notifier)
31 self._task_executor = task_executor
32
33 def _is_identity_transition(self, old_state, state, task, progress=None):
34 if state in self.SAVE_RESULT_STATES:
35 # saving result is never identity transition
36 return False
37 if state != old_state:
38 # changing state is not identity transition by definition
39 return False
40 # NOTE(imelnikov): last thing to check is that the progress has
41 # changed, which means progress is not None and is different from
42 # what is stored in the database.
43 if progress is None:
44 return False
45 old_progress = self._storage.get_task_progress(task.name)
46 if old_progress != progress:
47 return False
48 return True
49
50 def change_state(self, task, state,
51 progress=None, result=base.Action.NO_RESULT):
52 old_state = self._storage.get_atom_state(task.name)
53 if self._is_identity_transition(old_state, state, task,
54 progress=progress):
55 # NOTE(imelnikov): ignore identity transitions in order
56 # to avoid extra write to storage backend and, what's
57 # more important, extra notifications.
58 return
59 if state in self.SAVE_RESULT_STATES:
60 save_result = None
61 if result is not self.NO_RESULT:
62 save_result = result
63 self._storage.save(task.name, save_result, state)
64 else:
65 self._storage.set_atom_state(task.name, state)
66 if progress is not None:
67 self._storage.set_task_progress(task.name, progress)
68 task_uuid = self._storage.get_atom_uuid(task.name)
69 details = {
70 'task_name': task.name,
71 'task_uuid': task_uuid,
72 'old_state': old_state,
73 }
74 if result is not self.NO_RESULT:
75 details['result'] = result
76 self._notifier.notify(state, details)
77 if progress is not None:
78 task.update_progress(progress)
79
80 def _on_update_progress(self, task, event_type, details):
81 """Should be called when task updates its progress."""
82 try:
83 progress = details.pop('progress')
84 except KeyError:
85 pass
86 else:
87 try:
88 self._storage.set_task_progress(task.name, progress,
89 details=details)
90 except Exception:
91 # Update progress callbacks should never fail, so capture and
92 # log the emitted exception instead of raising it.
93 LOG.exception("Failed setting task progress for %s to %0.3f",
94 task, progress)
95
96 def schedule_execution(self, task):
97 self.change_state(task, states.RUNNING, progress=0.0)
98 arguments = self._storage.fetch_mapped_args(
99 task.rebind,
100 atom_name=task.name,
101 optional_args=task.optional
102 )
103 if task.notifier.can_be_registered(task_atom.EVENT_UPDATE_PROGRESS):
104 progress_callback = functools.partial(self._on_update_progress,
105 task)
106 else:
107 progress_callback = None
108 task_uuid = self._storage.get_atom_uuid(task.name)
109 return self._task_executor.execute_task(
110 task, task_uuid, arguments,
111 progress_callback=progress_callback)
112
113 def complete_execution(self, task, result):
114 if isinstance(result, failure.Failure):
115 self.change_state(task, states.FAILURE, result=result)
116 else:
117 self.change_state(task, states.SUCCESS,
118 result=result, progress=1.0)
119
120 def schedule_reversion(self, task):
121 self.change_state(task, states.REVERTING, progress=0.0)
122 arguments = self._storage.fetch_mapped_args(
123 task.revert_rebind,
124 atom_name=task.name,
125 optional_args=task.revert_optional
126 )
127 task_uuid = self._storage.get_atom_uuid(task.name)
128 task_result = self._storage.get(task.name)
129 failures = self._storage.get_failures()
130 if task.notifier.can_be_registered(task_atom.EVENT_UPDATE_PROGRESS):
131 progress_callback = functools.partial(self._on_update_progress,
132 task)
133 else:
134 progress_callback = None
135 return self._task_executor.revert_task(
136 task, task_uuid, arguments, task_result, failures,
137 progress_callback=progress_callback)
138
139 def complete_reversion(self, task, result):
140 if isinstance(result, failure.Failure):
141 self.change_state(task, states.REVERT_FAILURE, result=result)
142 else:
143 self.change_state(task, states.REVERTED, progress=1.0,
144 result=result)