Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 1 | # Copyright 2019-present Open Networking Foundation |
| 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 | from __future__ import absolute_import |
| 16 | import socketio |
| 17 | import psutil |
| 18 | import time |
| 19 | import datetime |
| 20 | |
| 21 | from threading import Timer |
| 22 | from gevent import pywsgi |
| 23 | from geventwebsocket.handler import WebSocketHandler |
| 24 | from multiprocessing import Process |
| 25 | from multistructlog import create_logger |
Illyoung Choi | 67e54e7 | 2019-07-25 10:44:59 -0700 | [diff] [blame] | 26 | from cord_workflow_controller_client.probe import GREETING, EVENT_EMIT |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 27 | from cord_workflow_controller_client.manager \ |
| 28 | import (WORKFLOW_KICKSTART, |
| 29 | WORKFLOW_REGISTER, WORKFLOW_REGISTER_ESSENCE, WORKFLOW_LIST, WORKFLOW_LIST_RUN, |
Illyoung Choi | 4df34b7 | 2019-07-18 13:55:18 -0700 | [diff] [blame] | 30 | WORKFLOW_CHECK, WORKFLOW_REMOVE, WORKFLOW_REMOVE_RUN, WORKFLOW_REPORT_NEW_RUN) |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 31 | from cord_workflow_controller_client.workflow_run \ |
Illyoung Choi | 70aea7d | 2019-07-29 15:56:19 -0700 | [diff] [blame] | 32 | import (WORKFLOW_RUN_NOTIFY_EVENT, WORKFLOW_RUN_COUNT_EVENTS, WORKFLOW_RUN_FETCH_EVENT) |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | """ |
| 36 | Run a dummy socket.io server as a separate process. |
| 37 | serve_forever() blocks until the process is killed, |
| 38 | so I had to use multi-process approach. |
| 39 | """ |
| 40 | |
| 41 | log = create_logger() |
| 42 | |
| 43 | # Socket IO |
| 44 | sio = None |
| 45 | |
| 46 | manager_clients = {} |
| 47 | workflows = {} |
| 48 | workflow_essences = {} |
| 49 | workflow_runs = {} |
| 50 | workflow_run_clients = {} |
| 51 | seq_no = 1 |
| 52 | |
| 53 | |
| 54 | class repeatableTimer(): |
| 55 | def __init__(self, time, handler, arg): |
| 56 | self.time = time |
| 57 | self.handler = handler |
| 58 | self.arg = arg |
| 59 | self.thread = Timer(self.time, self.on_tick) |
| 60 | |
| 61 | def on_tick(self): |
| 62 | self.handler(self.arg) |
| 63 | self.thread = Timer(self.time, self.on_tick) |
| 64 | self.thread.start() |
| 65 | |
| 66 | def start(self): |
| 67 | self.thread.start() |
| 68 | |
| 69 | def cancel(self): |
| 70 | self.thread.cancel() |
| 71 | |
| 72 | |
| 73 | def make_query_string_dict(query_string): |
| 74 | obj = {} |
| 75 | params = query_string.split('&') |
| 76 | for param in params: |
| 77 | kv = param.split('=') |
| 78 | key = kv[0] |
| 79 | val = kv[1] |
| 80 | obj[key] = val |
| 81 | |
| 82 | return obj |
| 83 | |
| 84 | |
| 85 | def _send_kickstart_event(sid): |
| 86 | global seq_no |
| 87 | |
| 88 | workflow_id = 'dummy_workflow_%d' % seq_no |
| 89 | workflow_run_id = 'dummy_workflow_run_%d' % seq_no |
| 90 | |
| 91 | seq_no += 1 |
| 92 | log.info('sending a kickstart event to sid %s' % sid) |
| 93 | sio.emit( |
| 94 | event=WORKFLOW_KICKSTART, |
| 95 | data={ |
| 96 | 'workflow_id': workflow_id, |
| 97 | 'workflow_run_id': workflow_run_id, |
| 98 | 'timestamp': str(datetime.datetime.now()) |
| 99 | }, |
| 100 | room=sid |
| 101 | ) |
| 102 | |
| 103 | |
| 104 | def _send_notify_event(sid): |
| 105 | global seq_no |
| 106 | |
| 107 | topic = 'topic_%s' % seq_no |
| 108 | message = { |
| 109 | 'sample_key': 'sample_value' |
| 110 | } |
| 111 | seq_no += 1 |
| 112 | |
| 113 | run_client = workflow_run_clients[sid] |
| 114 | if run_client: |
| 115 | workflow_run_id = run_client['workflow_run_id'] |
| 116 | workflow_run = workflow_runs[workflow_run_id] |
| 117 | if workflow_run: |
| 118 | workflow_run['queue'].append({ |
| 119 | 'topic': topic, |
| 120 | 'message': message |
| 121 | }) |
| 122 | |
| 123 | log.info('sending a notify event to sid %s' % sid) |
| 124 | sio.emit( |
| 125 | event=WORKFLOW_RUN_NOTIFY_EVENT, |
| 126 | data={ |
| 127 | 'topic': topic, |
| 128 | 'timestamp': str(datetime.datetime.now()) |
| 129 | }, |
| 130 | room=sid |
| 131 | ) |
| 132 | |
| 133 | |
| 134 | def _handle_event_connect(sid, query): |
| 135 | sio.emit(GREETING, {}) |
| 136 | |
| 137 | global last_client_action_time |
| 138 | last_client_action_time = datetime.datetime.now |
| 139 | |
| 140 | # if the client is a manager, send kickstart events every 3 sec |
| 141 | if query['type'] == 'workflow_manager': |
| 142 | log.info('manager (%s) is connected' % sid) |
| 143 | kickstart_timer = repeatableTimer(2, _send_kickstart_event, sid) |
| 144 | manager_clients[sid] = { |
| 145 | 'kickstart_timer': kickstart_timer |
| 146 | } |
| 147 | |
| 148 | kickstart_timer.start() |
| 149 | elif query['type'] == 'workflow_run': |
| 150 | log.info('workflow run (%s) is connected' % sid) |
| 151 | notify_event_timer = repeatableTimer(2, _send_notify_event, sid) |
| 152 | workflow_run_clients[sid] = { |
| 153 | 'workflow_id': query['workflow_id'], |
| 154 | 'workflow_run_id': query['workflow_run_id'], |
| 155 | 'notify_event_timer': notify_event_timer |
| 156 | } |
| 157 | |
| 158 | notify_event_timer.start() |
| 159 | |
| 160 | |
| 161 | def _handle_event_disconnect(sid): |
| 162 | if sid in manager_clients: |
| 163 | log.info('manager (%s) is disconnected' % sid) |
| 164 | if manager_clients[sid]['kickstart_timer']: |
| 165 | manager_clients[sid]['kickstart_timer'].cancel() |
| 166 | |
| 167 | del manager_clients[sid] |
| 168 | |
| 169 | if sid in workflow_run_clients: |
| 170 | log.info('workflow run (%s) is disconnected' % sid) |
| 171 | if workflow_run_clients[sid]['notify_event_timer']: |
| 172 | workflow_run_clients[sid]['notify_event_timer'].cancel() |
| 173 | |
| 174 | del workflow_run_clients[sid] |
| 175 | |
| 176 | global last_client_action_time |
| 177 | last_client_action_time = datetime.datetime.now |
| 178 | |
| 179 | |
| 180 | def _get_req_id(body): |
| 181 | req_id = 101010 |
| 182 | if 'req_id' in body: |
| 183 | req_id = int(body['req_id']) |
| 184 | return req_id |
| 185 | |
| 186 | |
| 187 | def _handle_event_workflow_reg(sid, body): |
| 188 | data = { |
| 189 | 'req_id': _get_req_id(body) |
| 190 | } |
| 191 | |
| 192 | if 'workflow' in body: |
| 193 | workflow = body['workflow'] |
| 194 | workflow_id = workflow['id'] |
| 195 | |
| 196 | if workflow_id in workflows: |
| 197 | # already exist |
| 198 | data['error'] = True |
| 199 | data['result'] = False |
| 200 | data['message'] = 'workflow is already registered' |
| 201 | else: |
| 202 | log.info('manager (%s) registers a workflow (%s)' % (sid, workflow_id)) |
| 203 | workflows[workflow_id] = workflow |
| 204 | |
| 205 | data['error'] = False |
| 206 | data['result'] = True |
| 207 | else: |
| 208 | data['error'] = True |
| 209 | data['result'] = False |
| 210 | data['message'] = 'workflow is not in the message body' |
| 211 | |
| 212 | log.info('returning a result for workflow register event to sid %s' % sid) |
| 213 | sio.emit( |
| 214 | event=WORKFLOW_REGISTER, |
| 215 | data=data, |
| 216 | room=sid |
| 217 | ) |
| 218 | |
| 219 | |
| 220 | def _handle_event_workflow_reg_essence(sid, body): |
| 221 | data = { |
| 222 | 'req_id': _get_req_id(body) |
| 223 | } |
| 224 | |
| 225 | if 'essence' in body: |
| 226 | essence = body['essence'] |
| 227 | for wid in essence: |
| 228 | workflow_essence = essence[wid] |
| 229 | if 'dag' in workflow_essence and 'dag_id' in workflow_essence['dag']: |
| 230 | dag = workflow_essence['dag'] |
| 231 | workflow_id = dag['dag_id'] |
| 232 | |
| 233 | if workflow_id in workflow_essences or workflow_id in workflows: |
| 234 | # already exist |
| 235 | data['error'] = True |
| 236 | data['result'] = False |
| 237 | data['message'] = 'workflow is already registered' |
| 238 | else: |
| 239 | log.info('manager (%s) registers a workflow (%s)' % (sid, workflow_id)) |
| 240 | workflow_essences[workflow_id] = workflow_essence |
| 241 | |
| 242 | data['error'] = False |
| 243 | data['result'] = True |
| 244 | else: |
| 245 | data['error'] = True |
| 246 | data['result'] = False |
| 247 | data['message'] = 'essence is not in the message body' |
| 248 | else: |
| 249 | data['error'] = True |
| 250 | data['result'] = False |
| 251 | data['message'] = 'essence is not in the message body' |
| 252 | |
| 253 | log.info('returning a result for workflow essence register event to sid %s' % sid) |
| 254 | sio.emit( |
| 255 | event=WORKFLOW_REGISTER_ESSENCE, |
| 256 | data=data, |
| 257 | room=sid |
| 258 | ) |
| 259 | |
| 260 | |
| 261 | def _handle_event_workflow_list(sid, body): |
| 262 | data = { |
| 263 | 'req_id': _get_req_id(body) |
| 264 | } |
| 265 | |
| 266 | workflow_ids = [] |
| 267 | |
| 268 | for workflow_id in workflows: |
| 269 | workflow_ids.append(workflow_id) |
| 270 | |
| 271 | for workflow_id in workflow_essences: |
| 272 | workflow_ids.append(workflow_id) |
| 273 | |
| 274 | data['error'] = False |
| 275 | data['result'] = workflow_ids |
| 276 | |
| 277 | log.info('returning a result for workflow list event to sid %s' % sid) |
| 278 | sio.emit( |
| 279 | event=WORKFLOW_LIST, |
| 280 | data=data, |
| 281 | room=sid |
| 282 | ) |
| 283 | |
| 284 | |
| 285 | def _handle_event_workflow_run_list(sid, body): |
| 286 | data = { |
| 287 | 'req_id': _get_req_id(body) |
| 288 | } |
| 289 | |
| 290 | workflow_run_ids = [] |
| 291 | |
| 292 | for workflow_run_id in workflow_runs: |
| 293 | workflow_run_ids.append(workflow_run_id) |
| 294 | |
| 295 | data['error'] = False |
| 296 | data['result'] = workflow_run_ids |
| 297 | |
| 298 | log.info('returning a result for workflow run list event to sid %s' % sid) |
| 299 | sio.emit( |
| 300 | event=WORKFLOW_LIST_RUN, |
| 301 | data=data, |
| 302 | room=sid |
| 303 | ) |
| 304 | |
| 305 | |
| 306 | def _handle_event_workflow_check(sid, body): |
| 307 | data = { |
| 308 | 'req_id': _get_req_id(body) |
| 309 | } |
| 310 | |
| 311 | if 'workflow_id' in body: |
| 312 | workflow_id = body['workflow_id'] |
| 313 | if workflow_id in workflows: |
| 314 | data['error'] = False |
| 315 | data['result'] = True |
| 316 | else: |
| 317 | data['error'] = False |
| 318 | data['result'] = False |
| 319 | else: |
| 320 | data['error'] = True |
| 321 | data['result'] = False |
| 322 | data['message'] = 'workflow_id is not in the message body' |
| 323 | |
| 324 | log.info('returning a result for workflow check event to sid %s' % sid) |
| 325 | sio.emit( |
| 326 | event=WORKFLOW_CHECK, |
| 327 | data=data, |
| 328 | room=sid |
| 329 | ) |
| 330 | |
| 331 | |
| 332 | def _handle_event_workflow_remove(sid, body): |
| 333 | data = { |
| 334 | 'req_id': _get_req_id(body) |
| 335 | } |
| 336 | |
| 337 | if 'workflow_id' in body: |
| 338 | workflow_id = body['workflow_id'] |
| 339 | if workflow_id in workflows: |
| 340 | |
| 341 | hasWorkflowRuns = False |
| 342 | for workflow_run_id in workflow_runs: |
| 343 | workflow_run = workflow_runs[workflow_run_id] |
| 344 | wid = workflow_run['workflow_id'] |
| 345 | if wid == workflow_id: |
| 346 | # there is a workflow run for the workflow id |
| 347 | hasWorkflowRuns = True |
| 348 | break |
| 349 | |
| 350 | if hasWorkflowRuns: |
| 351 | data['error'] = False |
| 352 | data['result'] = False |
| 353 | else: |
| 354 | del workflows[workflow_id] |
| 355 | |
| 356 | data['error'] = False |
| 357 | data['result'] = True |
| 358 | else: |
| 359 | data['error'] = False |
| 360 | data['result'] = False |
| 361 | else: |
| 362 | data['error'] = True |
| 363 | data['result'] = False |
| 364 | data['message'] = 'workflow_id is not in the message body' |
| 365 | |
| 366 | log.info('returning a result for workflow remove event to sid %s' % sid) |
| 367 | sio.emit( |
| 368 | event=WORKFLOW_REMOVE, |
| 369 | data=data, |
| 370 | room=sid |
| 371 | ) |
| 372 | |
| 373 | |
| 374 | def _handle_event_workflow_run_remove(sid, body): |
| 375 | data = { |
| 376 | 'req_id': _get_req_id(body) |
| 377 | } |
| 378 | |
| 379 | if 'workflow_id' in body and 'workflow_run_id' in body: |
| 380 | # workflow_id = body['workflow_id'] |
| 381 | workflow_run_id = body['workflow_run_id'] |
| 382 | |
| 383 | if workflow_run_id in workflow_runs: |
| 384 | del workflow_runs[workflow_run_id] |
| 385 | |
| 386 | data['error'] = False |
| 387 | data['result'] = True |
| 388 | else: |
| 389 | data['error'] = False |
| 390 | data['result'] = False |
| 391 | else: |
| 392 | data['error'] = True |
| 393 | data['result'] = False |
| 394 | data['message'] = 'workflow_id or workflow_run_id is not in the message body' |
| 395 | |
| 396 | log.info('returning a result for workflow run remove event to sid %s' % sid) |
| 397 | sio.emit( |
| 398 | event=WORKFLOW_REMOVE_RUN, |
| 399 | data=data, |
| 400 | room=sid |
| 401 | ) |
| 402 | |
| 403 | |
| 404 | def _handle_event_new_workflow_run(sid, body): |
| 405 | data = { |
| 406 | 'req_id': _get_req_id(body) |
| 407 | } |
| 408 | |
| 409 | if 'workflow_id' in body and 'workflow_run_id' in body: |
| 410 | workflow_id = body['workflow_id'] |
| 411 | workflow_run_id = body['workflow_run_id'] |
| 412 | |
| 413 | log.info('manager (%s) started a new workflow (%s), workflow_run (%s)' % (sid, workflow_id, workflow_run_id)) |
| 414 | workflow_runs[workflow_run_id] = { |
| 415 | 'workflow_id': workflow_id, |
| 416 | 'workflow_run_id': workflow_run_id, |
| 417 | 'queue': [] |
| 418 | } |
| 419 | |
| 420 | data['error'] = False |
| 421 | data['result'] = True |
| 422 | else: |
| 423 | data['error'] = True |
| 424 | data['result'] = False |
| 425 | data['message'] = 'workflow_id or workflow_run_id is not in the message body' |
| 426 | |
| 427 | log.info('returning a result for a new workflow run event to sid %s' % sid) |
| 428 | sio.emit( |
Illyoung Choi | 4df34b7 | 2019-07-18 13:55:18 -0700 | [diff] [blame] | 429 | event=WORKFLOW_REPORT_NEW_RUN, |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 430 | data=data, |
| 431 | room=sid |
| 432 | ) |
| 433 | |
| 434 | |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 435 | def _handle_event_workflow_run_count_events(sid, body): |
| 436 | data = { |
| 437 | 'req_id': _get_req_id(body) |
| 438 | } |
| 439 | |
| 440 | if 'workflow_id' in body and 'workflow_run_id' in body: |
| 441 | # workflow_id = body['workflow_id'] |
| 442 | workflow_run_id = body['workflow_run_id'] |
| 443 | |
| 444 | if workflow_run_id in workflow_runs: |
| 445 | workflow_run = workflow_runs[workflow_run_id] |
| 446 | queue = workflow_run['queue'] |
| 447 | count = len(queue) |
| 448 | |
| 449 | data['error'] = False |
| 450 | data['result'] = count |
| 451 | else: |
| 452 | data['error'] = True |
| 453 | data['result'] = 0 |
| 454 | data['message'] = 'cannot find workflow run' |
| 455 | else: |
| 456 | data['error'] = True |
| 457 | data['result'] = 0 |
| 458 | data['message'] = 'workflow_id, workflow_run_id, task_id or status is not in the message body' |
| 459 | |
| 460 | log.info('returning a result for workflow run count events to sid %s' % sid) |
| 461 | sio.emit( |
| 462 | event=WORKFLOW_RUN_COUNT_EVENTS, |
| 463 | data=data, |
| 464 | room=sid |
| 465 | ) |
| 466 | |
| 467 | |
| 468 | def _handle_event_workflow_run_fetch_event(sid, body): |
| 469 | data = { |
| 470 | 'req_id': _get_req_id(body) |
| 471 | } |
| 472 | |
| 473 | if 'workflow_id' in body and 'workflow_run_id' in body and 'task_id' in body and 'topic' in body: |
| 474 | # workflow_id = body['workflow_id'] |
| 475 | workflow_run_id = body['workflow_run_id'] |
| 476 | # task_id = body['task_id'] |
| 477 | topic = body['topic'] |
| 478 | |
| 479 | if workflow_run_id in workflow_runs: |
| 480 | workflow_run = workflow_runs[workflow_run_id] |
| 481 | queue = workflow_run['queue'] |
| 482 | |
| 483 | event = None |
| 484 | for idx in range(len(queue)): |
| 485 | if queue[idx]['topic'] == topic: |
| 486 | # found |
| 487 | event = queue.pop(idx) |
| 488 | break |
| 489 | |
| 490 | if event: |
| 491 | data['error'] = False |
| 492 | data['result'] = event |
| 493 | else: |
| 494 | data['error'] = False |
| 495 | data['result'] = {} |
| 496 | else: |
| 497 | data['error'] = False |
| 498 | data['result'] = False |
| 499 | data['message'] = 'cannot find workflow run' |
| 500 | else: |
| 501 | data['error'] = True |
| 502 | data['result'] = False |
| 503 | data['message'] = 'workflow_id, workflow_run_id, task_id or topic is not in the message body' |
| 504 | |
| 505 | log.info('returning a result for workflow run fetch event to sid %s' % sid) |
| 506 | sio.emit( |
| 507 | event=WORKFLOW_RUN_FETCH_EVENT, |
| 508 | data=data, |
| 509 | room=sid |
| 510 | ) |
| 511 | |
| 512 | |
Illyoung Choi | 67e54e7 | 2019-07-25 10:44:59 -0700 | [diff] [blame] | 513 | def _handle_event_emit(sid, body): |
| 514 | data = { |
| 515 | 'req_id': _get_req_id(body) |
| 516 | } |
| 517 | |
| 518 | if 'topic' in body and 'message' in body: |
| 519 | # workflow_id = body['workflow_id'] |
| 520 | topic = body['topic'] |
| 521 | message = body['message'] |
| 522 | |
| 523 | log.info('probe topic %s - message %s' % (topic, message)) |
| 524 | |
| 525 | data['error'] = False |
| 526 | data['result'] = True |
| 527 | else: |
| 528 | data['error'] = True |
| 529 | data['result'] = False |
| 530 | data['message'] = 'topic or message is not in the message body' |
| 531 | |
| 532 | log.info('returning a result for event emit to sid %s' % sid) |
| 533 | sio.emit( |
| 534 | event=EVENT_EMIT, |
| 535 | data=data, |
| 536 | room=sid |
| 537 | ) |
| 538 | |
| 539 | |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 540 | def _handle_event(event, sid, body): |
| 541 | log.info('event %s - body %s (%s)' % (event, body, type(body))) |
| 542 | |
| 543 | |
| 544 | class ServerEventHandler(socketio.namespace.Namespace): |
| 545 | def trigger_event(self, event, *args): |
| 546 | sid = args[0] |
| 547 | if event == 'connect': |
| 548 | querystr = args[1]['QUERY_STRING'] |
| 549 | query = make_query_string_dict(querystr) |
| 550 | _handle_event_connect(sid, query) |
| 551 | elif event == 'disconnect': |
| 552 | _handle_event_disconnect(sid) |
| 553 | |
| 554 | # manager |
Illyoung Choi | 4df34b7 | 2019-07-18 13:55:18 -0700 | [diff] [blame] | 555 | elif event == WORKFLOW_REPORT_NEW_RUN: |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 556 | _handle_event_new_workflow_run(sid, args[1]) |
| 557 | elif event == WORKFLOW_REGISTER_ESSENCE: |
| 558 | _handle_event_workflow_reg_essence(sid, args[1]) |
| 559 | elif event == WORKFLOW_REGISTER: |
| 560 | _handle_event_workflow_reg(sid, args[1]) |
| 561 | elif event == WORKFLOW_LIST: |
| 562 | _handle_event_workflow_list(sid, args[1]) |
| 563 | elif event == WORKFLOW_LIST_RUN: |
| 564 | _handle_event_workflow_run_list(sid, args[1]) |
| 565 | elif event == WORKFLOW_CHECK: |
| 566 | _handle_event_workflow_check(sid, args[1]) |
| 567 | elif event == WORKFLOW_REMOVE: |
| 568 | _handle_event_workflow_remove(sid, args[1]) |
| 569 | elif event == WORKFLOW_REMOVE_RUN: |
| 570 | _handle_event_workflow_run_remove(sid, args[1]) |
| 571 | |
| 572 | # workflow run |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 573 | elif event == WORKFLOW_RUN_COUNT_EVENTS: |
| 574 | _handle_event_workflow_run_count_events(sid, args[1]) |
| 575 | elif event == WORKFLOW_RUN_FETCH_EVENT: |
| 576 | _handle_event_workflow_run_fetch_event(sid, args[1]) |
Illyoung Choi | 67e54e7 | 2019-07-25 10:44:59 -0700 | [diff] [blame] | 577 | elif event == EVENT_EMIT: |
| 578 | _handle_event_emit(sid, args[1]) |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 579 | else: |
| 580 | _handle_event(event, args[0], args[1]) |
| 581 | |
| 582 | |
| 583 | def _run(port): |
| 584 | global sio |
| 585 | sio = socketio.Server(ping_timeout=5, ping_interval=1) |
| 586 | app = socketio.WSGIApp(sio) |
| 587 | sio.register_namespace(ServerEventHandler('/')) |
| 588 | |
| 589 | server = pywsgi.WSGIServer( |
| 590 | ('', port), |
| 591 | app, |
| 592 | handler_class=WebSocketHandler |
| 593 | ) |
| 594 | |
| 595 | server.serve_forever() |
| 596 | |
| 597 | |
| 598 | def start(port): |
| 599 | p = Process(target=_run, args=(port, )) |
| 600 | p.start() |
| 601 | time.sleep(3) |
| 602 | |
| 603 | log.info('Dummy server is started!') |
| 604 | return p |
| 605 | |
| 606 | |
| 607 | def stop(p): |
| 608 | log.info('Stopping dummy server!') |
| 609 | |
| 610 | try: |
| 611 | process = psutil.Process(p.pid) |
| 612 | for proc in process.children(recursive=True): |
| 613 | proc.kill() |
| 614 | process.kill() |
| 615 | p.join() |
Illyoung Choi | 4df34b7 | 2019-07-18 13:55:18 -0700 | [diff] [blame] | 616 | except BaseException: |
| 617 | pass |
Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame] | 618 | except psutil.NoSuchProcess: |
| 619 | pass |
| 620 | |
| 621 | # clean-up |
| 622 | global sio, manager_clients, workflow_runs, seq_no |
| 623 | sio = None |
| 624 | manager_clients = {} |
| 625 | workflow_runs = {} |
| 626 | seq_no = 1 |
| 627 | |
| 628 | time.sleep(3) |
| 629 | |
| 630 | log.info('Dummy server is stopped!') |