-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.py
241 lines (208 loc) · 8.79 KB
/
execute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import contextlib
import copy_reg
import functools
import logging
import threading
import types
import coloredlogs
import six
from futurist import ProcessPoolExecutor, ThreadPoolExecutor
from oslo_utils import excutils
from taskflow.conductors import backends as conductors
from taskflow.jobs.backends.impl_zookeeper import ZookeeperJob
from taskflow.persistence import backends as persistence_backends
from taskflow.listeners import logging as logging_listener
from taskflow.utils import async_utils
from taskflow.conductors import base
from taskflow.types import timing as tt
try:
from contextlib import ExitStack # noqa
except ImportError:
from contextlib2 import ExitStack # noqa
from taskflow import exceptions as excp
from board import HypernodeJobBoard
from taskflow import engines
LOG = logging.getLogger(__name__)
# coloredlogs.install(level='DEBUG')
WAIT_TIMEOUT = 0.5
NO_CONSUME_EXCEPTIONS = tuple([
excp.ExecutionFailure,
excp.StorageFailure,
])
logging.basicConfig(level=logging.WARN)
pool = ProcessPoolExecutor(100)
def _run_job(flow_detail, store, engine, persistence, engine_options):
engine = engines.load_from_detail(flow_detail, store=store,
engine=engine,
backend=persistence,
**engine_options)
with logging_listener.LoggingListener(engine, log=LOG):
LOG.debug("Dispatching engine for job")
consume = True
try:
for stage_func, event_name in [(engine.compile, 'compilation'),
(engine.prepare, 'preparation'),
(engine.validate, 'validation'),
(engine.run, 'running')]:
# self._notifier.notify("%s_start" % event_name, {
# 'job': job,
# 'engine': engine,
# 'conductor': self,
# })
stage_func()
# self._notifier.notify("%s_end" % event_name, {
# 'job': job,
# 'engine': engine,
# 'conductor': self,
# })
except excp.WrappedFailure as e:
if all((f.check(*NO_CONSUME_EXCEPTIONS) for f in e)):
consume = False
if LOG.isEnabledFor(logging.WARNING):
# if consume:
# LOG.warn("Job execution failed (consumption being"
# " skipped): %s [%s failures]", job, len(e))
# else:
# LOG.warn("Job execution failed (consumption"
# " proceeding): %s [%s failures]", job, len(e))
# Show the failure/s + traceback (if possible)...
for i, f in enumerate(e):
LOG.warn("%s. %s", i + 1, f.pformat(traceback=True))
except NO_CONSUME_EXCEPTIONS:
# LOG.warn("Job execution failed (consumption being"
# " skipped): %s", job, exc_info=True)
consume = False
except Exception:
# LOG.warn("Job execution failed (consumption proceeding): %s",
# job, exc_info=True)
pass
else:
pass
# LOG.info("Job completed successfully: %s", job)
return consume
class AsyncConductor(base.Conductor):
"""A conductor that dispatches jobs to separate processes
This conductor iterates over jobs in the provided jobboard, start a process
to work on the job, and then consume those work units after completion. This
process will repeat until the conductor has been stopped or other critical
error occurs.
"""
START_FINISH_EVENTS_EMITTED = tuple([
'compilation', 'preparation',
'validation', 'running',
])
"""Events will be emitted for the start and finish of each engine
activity defined above, the actual event name that can be registered
to subscribe to will be ``${event}_start`` and ``${event}_end`` where
the ``${event}`` in this pseudo-variable will be one of these events.
"""
def __init__(self, name, jobboard,
persistence=None, engine=None,
engine_options=None, wait_timeout=None):
super(AsyncConductor, self).__init__(
name, jobboard, persistence=persistence,
engine=engine, engine_options=engine_options)
if wait_timeout is None:
wait_timeout = WAIT_TIMEOUT
if isinstance(wait_timeout, (int, float) + six.string_types):
self._wait_timeout = tt.Timeout(float(wait_timeout))
elif isinstance(wait_timeout, tt.Timeout):
self._wait_timeout = wait_timeout
else:
raise ValueError("Invalid timeout literal: %s" % (wait_timeout))
self._dead = threading.Event()
def stop(self):
"""Requests the conductor to stop dispatching.
This method can be used to request that a conductor stop its
consumption & dispatching loop.
The method returns immediately regardless of whether the conductor has
been stopped.
"""
self._wait_timeout.interrupt()
@property
def dispatching(self):
return not self._dead.is_set()
def _listeners_from_job(self, job, engine):
listeners = super(AsyncConductor, self)._listeners_from_job(job, engine)
listeners.append(logging_listener.LoggingListener(engine, log=LOG))
return listeners
def _dispatch_job(self, job):
flow_detail = self._flow_detail_from_job(job)
if job.details and 'store' in job.details:
store = dict(job.details["store"])
else:
store = {}
return pool.submit(_run_job, flow_detail, store, self._engine, self._persistence, self._engine_options)
def _job_done(self, job, f):
consume = f.result()
try:
if consume:
self._jobboard.consume(job, self._name)
else:
self._jobboard.abandon(job, self._name)
except (excp.JobFailure, excp.NotFound):
if consume:
LOG.warn("Failed job consumption: %s", job,
exc_info=True)
else:
LOG.warn("Failed job abandonment: %s", job,
exc_info=True)
def run(self):
self._dead.clear()
try:
while True:
if self._wait_timeout.is_stopped():
break
for job in self._jobboard.iterjobs():
if self._wait_timeout.is_stopped():
break
LOG.debug("Trying to claim job: %s", job)
try:
self._jobboard.claim(job, self._name)
except (excp.UnclaimableJob, excp.NotFound):
LOG.debug("Job already claimed or consumed: %s", job)
continue
try:
f = self._dispatch_job(job)
f.add_done_callback(functools.partial(self._job_done, job))
except KeyboardInterrupt:
with excutils.save_and_reraise_exception():
LOG.warn("Job dispatching interrupted: %s", job)
except Exception:
LOG.warn("Job dispatching failed: %s", job,
exc_info=True)
if not self._wait_timeout.is_stopped():
self._wait_timeout.wait()
except StopIteration:
pass
finally:
self._dead.set()
def wait(self, timeout=None):
"""Waits for the conductor to gracefully exit.
This method waits for the conductor to gracefully exit. An optional
timeout can be provided, which will cause the method to return
within the specified timeout. If the timeout is reached, the returned
value will be False.
:param timeout: Maximum number of seconds that the :meth:`wait` method
should block for.
"""
return self._dead.wait(timeout)
def main():
persistence = persistence_backends.fetch({
'connection': 'sqlite:////tmp/taskflow.db'
})
board = HypernodeJobBoard('my-board', {
"hosts": "localhost",
}, persistence=persistence)
# board = job_backends.fetch("my-board", {
# "board": "zookeeper",
# "hosts": "localhost",
# "path": "/jobboard",
# }, persistence=persistence)
board.connect()
# conductor = conductors.fetch("blocking", "executor 1", board, engine="parallel", wait_timeout=.1)
conductor = AsyncConductor("async", board, engine="parallel")
with contextlib.closing(board):
conductor.run()
if __name__ == '__main__':
main()