This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sodar_taskflow.py
322 lines (273 loc) · 9.33 KB
/
sodar_taskflow.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from flask import Flask, request, Response
import logging
from logging.handlers import RotatingFileHandler
from multiprocessing import Process
import os
import sys
from apis import irods_utils, lock_api, sodar_api
from config import settings
import flows
app = Flask('sodar_taskflow')
app.config.from_envvar('SODAR_TASKFLOW_SETTINGS')
# NOTE: FLASK_ENV from settings does not work automatically?
app.config['ENV'] = settings.FLASK_ENV
# Set up logging
app.logger.handlers = []
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
# Stdout handler
out_handler = logging.StreamHandler(stream=sys.stdout)
out_handler.setFormatter(formatter)
out_handler.setLevel(logging.getLevelName('DEBUG'))
app.logger.addHandler(out_handler)
# File handler
if settings.TASKFLOW_LOG_TO_FILE:
file_handler = RotatingFileHandler(settings.TASKFLOW_LOG_PATH)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.getLevelName('INFO'))
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.getLevelName(settings.TASKFLOW_LOG_LEVEL))
def run_flow(
flow,
project_uuid,
timeline_uuid,
sodar_api,
irods,
force_fail,
async_mode=True,
):
"""
Run a task flow, either synchronously or asynchronously.
:param flow: Flow object
:param project_uuid: Project UUID as string
:param timeline_uuid: Timeline event UUID as string
:param sodar_api: SODARAPI object
:param irods: iRODS session object
:param force_fail: Force failure (boolean, for testing)
:param async_mode: Submit in async mode (boolean, default=True)
:return: Response object
"""
coordinator = None
lock = None
# Acquire lock if needed
if flow.require_lock:
# Acquire lock
coordinator = lock_api.get_coordinator()
if not coordinator:
ex_str = 'Error retrieving lock coordinator'
else:
lock_id = project_uuid
lock = coordinator.get_lock(lock_id)
try:
lock_api.acquire(lock)
except Exception as ex:
msg = 'Unable to acquire project lock'
app.logger.info(msg + ': ' + str(ex))
irods_utils.close_irods(irods)
return Response(msg, status=503)
else:
app.logger.info('Lock not required (flow.require_lock=False)')
flow_result = None
ex_str = None
response = None
# Build flow
app.logger.info('--- Building flow "{}" ---'.format(flow.flow_name))
try:
flow.build(force_fail)
except Exception as ex:
msg = 'Error building flow'
# TODO: HACK! generalize to report building problems in ODM!
if async_mode and 'zone_uuid' in flow.flow_data:
# Set zone status in the Django site
set_data = {
'zone_uuid': flow.flow_data['zone_uuid'],
'status': 'NOT CREATED'
if flow.flow_name == 'landing_zone_create'
else 'FAILED',
'status_info': '{}: {}'.format(msg, ex),
}
sodar_api.send_request('landingzones/taskflow/status/set', set_data)
# Set timeline status
sodar_api.set_timeline_status(
event_uuid=timeline_uuid, status_type='FAILED', status_desc=msg
)
app.logger.error('{}: {}'.format(msg, ex))
else:
response = Response('{}: {}'.format(msg, ex), status=500)
app.logger.info('--- Building flow OK ---')
# Run flow
if not ex_str:
try:
flow_result = flow.run()
except Exception as ex:
ex_str = str(ex)
# Flow completion
if flow_result:
if async_mode:
sodar_api.set_timeline_status(
event_uuid=timeline_uuid,
status_type='OK',
status_desc='Async submit OK',
)
else:
response = Response(str(flow_result), status=200)
# Exception/failure
else:
if async_mode:
sodar_api.set_timeline_status(
event_uuid=timeline_uuid,
status_type='FAILED',
status_desc='Error running async flow: '
+ (ex_str if ex_str else 'unknown error'),
)
else:
msg = 'Error running flow: ' + (
ex_str if ex_str != '' else 'unknown error'
)
app.logger.error(msg)
response = Response(msg, status=500)
# Release lock if acquired
if flow.require_lock and lock:
lock_api.release(lock)
coordinator.stop()
irods_utils.close_irods(irods)
return response
@app.route('/submit', methods=['POST'])
def submit():
"""
Handle POST request from Flask.
"""
###################
# Validate request
###################
form_data = request.json
app.logger.debug('Submit data: {}'.format(form_data))
force_fail = form_data['force_fail'] if 'force_fail' in form_data else False
test_mode = form_data['test_mode'] if 'test_mode' in form_data else False
required_keys = [
'project_uuid',
'request_mode',
'flow_name',
'targets',
'sodar_secret',
]
for k in required_keys:
if k not in form_data or form_data[k] == '':
msg = 'Missing or invalid argument: "{}"'.format(k)
app.logger.error(msg)
return Response(msg, status=400) # Bad request
# Ensure sodar_secret is correct
if form_data['sodar_secret'] != settings.TASKFLOW_SODAR_SECRET:
msg = 'Not authorized'
app.logger.error(msg)
return Response(msg, status=403)
# Make sure we can support the named flow
flow_cls = flows.get_flow(form_data['flow_name'])
if not flow_cls:
msg = 'Flow "{}" not supported'.format(form_data['flow_name'])
app.logger.error(msg)
return Response(msg, status=501) # Not implemented
#############
# Init iRODS
#############
try:
irods = irods_utils.init_irods(test_mode=test_mode)
except Exception as ex:
msg = 'Error initializing iRODS: {} ({})'.format(
ex.__class__.__name__, ex
)
app.logger.error(msg)
return Response(msg, status=500)
################
# Init SODAR API
################
if 'sodar_url' in form_data:
sodar_url = form_data['sodar_url']
else:
sodar_url = settings.TASKFLOW_SODAR_URL
sodar_tf = sodar_api.SODARAPI(sodar_url)
##############
# Create flow
##############
flow = flow_cls(
irods=irods,
sodar_api=sodar_tf,
project_uuid=form_data['project_uuid'],
flow_name=form_data['flow_name'],
flow_data=form_data['flow_data'],
targets=form_data['targets'],
request_mode=form_data['request_mode'],
timeline_uuid=form_data['timeline_uuid'],
)
try:
flow.validate()
except TypeError as ex:
msg = 'Error validating flow: {}'.format(ex)
app.logger.error(msg)
irods_utils.close_irods(irods)
return Response(msg, status=400)
project_uuid = form_data['project_uuid']
#####################
# Build and run flow
#####################
# Run asynchronously
if form_data['request_mode'] == 'async':
p = Process(
target=run_flow,
args=(
flow,
project_uuid,
form_data['timeline_uuid'],
sodar_tf,
irods,
force_fail,
True,
),
)
p.start()
return Response(str(True), status=200)
# Run synchronously
else:
return run_flow(
flow,
project_uuid,
form_data['timeline_uuid'],
sodar_tf,
irods,
force_fail,
False,
)
@app.route('/cleanup', methods=['POST'])
def cleanup():
form_data = request.json
test_mode = form_data['test_mode'] if 'test_mode' in form_data else False
if test_mode or settings.TASKFLOW_ALLOW_IRODS_CLEANUP:
try:
app.logger.info('--- Cleanup started ---')
irods = irods_utils.init_irods(test_mode=test_mode)
irods_utils.cleanup_irods_data(irods)
app.logger.info('--- Cleanup done ---')
irods_utils.close_irods(irods)
except Exception as ex:
return Response(
'Error during cleanup: {} ({})'.format(
ex.__class__.__name__, ex
),
status=500,
)
return Response('OK', status=200)
return Response('iRODS cleanup not allowed', status=403)
# DEBUG
@app.route('/hello', methods=['GET'])
def hello():
app.logger.debug('Hello world request received')
return Response('Hello world from sodar_taskflow!', status=200)
if __name__ == '__main__':
app.logger.info('settings={}'.format(os.getenv('SODAR_TASKFLOW_SETTINGS')))
app_kwargs = {'processes': 4} if settings.DEBUG else {}
app.run('0.0.0.0', 5005, threaded=False, **app_kwargs)
def validate_kwargs(kwargs_dict, required_keys):
"""Base validation function for kwargs"""
# TODO: Could also do e.g. proper json schema validation
for k in required_keys:
if k not in kwargs_dict or kwargs_dict[k] == '':
raise TypeError('Missing or invalid argument: "{}"'.format(k))