Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | (function () { |
| 19 | 'use strict'; |
| 20 | |
| 21 | const _ = require('lodash'); |
| 22 | const Workflow = require('../types/workflow.js'); |
| 23 | const logger = require('../config/logger.js'); |
| 24 | |
| 25 | let serviceEvents = { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 26 | // manager -> controller -> manager |
| 27 | WORKFLOW_REGISTER: 'cord.workflow.ctlsvc.workflow.register', |
| 28 | WORKFLOW_REGISTER_ESSENCE: 'cord.workflow.ctlsvc.workflow.register_essence', |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 29 | WORKFLOW_LIST: 'cord.workflow.ctlsvc.workflow.list', |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 30 | WORKFLOW_LIST_RUN: 'cord.workflow.ctlsvc.workflow.run.list', |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 31 | WORKFLOW_CHECK: 'cord.workflow.ctlsvc.workflow.check', |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 32 | WORKFLOW_REMOVE: 'cord.workflow.ctlsvc.workflow.remove', |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 33 | WORKFLOW_REMOVE_RUN: 'cord.workflow.ctlsvc.workflow.run.remove', |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 34 | WORKFLOW_REPORT_NEW_RUN: 'cord.workflow.ctlsvc.workflow.report_new_run', |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 35 | WORKFLOW_REPORT_RUN_STATUS: 'cord.workflow.ctlsvc.workflow.report_run_status', |
| 36 | WORKFLOW_REPORT_RUN_STATUS_BULK: 'cord.workflow.ctlsvc.workflow.report_run_status_bulk', |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 37 | // controller -> manager |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 38 | WORKFLOW_KICKSTART: 'cord.workflow.ctlsvc.workflow.kickstart', |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 39 | WORKFLOW_CHECK_STATUS: 'cord.workflow.ctlsvc.workflow.check.status', |
| 40 | WORKFLOW_CHECK_STATUS_BULK: 'cord.workflow.ctlsvc.workflow.check.status_bulk', |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | // WebSocket interface for workflow registration |
| 44 | // Message format: |
| 45 | // { |
| 46 | // topic: 'cord.workflow.ctlsvc.workflow.reg', |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 47 | // message: { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 48 | // req_id: <req_id>, // optional |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 49 | // workflow: <workflow> |
| 50 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 51 | // } |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 52 | const registerWorkflow = (topic, message, cb) => { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 53 | const eventrouter = require('./eventrouter.js'); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 54 | |
| 55 | let errorMessage; |
| 56 | if(!message) { |
| 57 | // error |
| 58 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 59 | logger.log('warn', `Return error - ${errorMessage}`); |
| 60 | cb(errorMessage, false); |
| 61 | return; |
| 62 | } |
| 63 | |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 64 | if(!('workflow' in message)) { |
| 65 | // error |
| 66 | errorMessage = `field 'workflow' does not exist in message body - ${JSON.stringify(message)}`; |
| 67 | logger.log('warn', `Return error - ${errorMessage}`); |
| 68 | cb(errorMessage, false); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | let workflow = message.workflow; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 73 | |
| 74 | logger.log('debug', `workflow - ${JSON.stringify(workflow)}`); |
| 75 | |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 76 | let result = eventrouter.addWorkflow(workflow); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 77 | if(!result) { |
| 78 | errorMessage = `failed to register a workflow ${workflow.getId()}`; |
| 79 | cb(errorMessage, false); |
| 80 | } |
| 81 | else { |
| 82 | cb(null, true); |
| 83 | } |
| 84 | return; |
| 85 | }; |
| 86 | |
| 87 | // WebSocket interface for workflow registration (via essence) |
| 88 | // Message format: |
| 89 | // { |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 90 | // topic: 'cord.workflow.ctlsvc.workflow.reg_essence', |
| 91 | // message: { |
| 92 | // req_id: <req_id> // optional |
| 93 | // essence: <workflow essence> |
| 94 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 95 | // } |
| 96 | const registerWorkflowEssence = (topic, message, cb) => { |
| 97 | const eventrouter = require('./eventrouter.js'); |
| 98 | let errorMessage; |
| 99 | if(!message) { |
| 100 | // error |
| 101 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 102 | logger.log('warn', `Return error - ${errorMessage}`); |
| 103 | cb(errorMessage, false); |
| 104 | return; |
| 105 | } |
| 106 | |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 107 | if(!('essence' in message)) { |
| 108 | // error |
| 109 | errorMessage = `field 'essence' does not exist in message body - ${JSON.stringify(message)}`; |
| 110 | logger.log('warn', `Return error - ${errorMessage}`); |
| 111 | cb(errorMessage, false); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | let essence = message.essence; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 116 | let result = true; |
| 117 | let errorResults = []; |
| 118 | |
| 119 | logger.log('debug', `workflow essence - ${JSON.stringify(essence)}`); |
| 120 | |
| 121 | let workflows = Workflow.loadWorkflowsFromEssence(essence); |
| 122 | workflows.forEach((workflow) => { |
| 123 | if(workflow) { |
| 124 | let localResult = eventrouter.addWorkflow(workflow); |
| 125 | errorResults.push(localResult); |
| 126 | result = result && localResult; // false if any of registrations fails |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | if(!result) { |
| 131 | errorMessage = `failed to register workflows ${errorResults}`; |
| 132 | cb(errorMessage, false); |
| 133 | } |
| 134 | else { |
| 135 | cb(null, true); |
| 136 | } |
| 137 | return; |
| 138 | }; |
| 139 | |
| 140 | // WebSocket interface for workflow listing |
| 141 | // Message format: |
| 142 | // { |
| 143 | // topic: 'cord.workflow.ctlsvc.workflow.list', |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 144 | // message: { |
| 145 | // req_id: <req_id> // optional |
| 146 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 147 | // } |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 148 | const listWorkflows = (_topic, _message, cb) => { |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 149 | const eventrouter = require('./eventrouter.js'); |
| 150 | |
| 151 | let result = eventrouter.listWorkflows(); |
| 152 | cb(null, result); |
| 153 | return; |
| 154 | }; |
| 155 | |
| 156 | // WebSocket interface for workflow run listing |
| 157 | // Message format: |
| 158 | // { |
| 159 | // topic: 'cord.workflow.ctlsvc.workflow.list', |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 160 | // message: { |
| 161 | // req_id: <req_id> // optional |
| 162 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 163 | // } |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 164 | const listWorkflowRuns = (_topic, _message, cb) => { |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 165 | const eventrouter = require('./eventrouter.js'); |
| 166 | |
| 167 | let result = eventrouter.listWorkflowRuns(); |
| 168 | cb(null, result); |
| 169 | return; |
| 170 | }; |
| 171 | |
| 172 | // WebSocket interface for workflow check |
| 173 | // Message format: |
| 174 | // { |
| 175 | // topic: 'cord.workflow.ctlsvc.workflow.check', |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 176 | // message: { |
| 177 | // req_id: <req_id> // optional |
| 178 | // workflow_id: <workflow_id> |
| 179 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 180 | // } |
| 181 | const checkWorkflow = (topic, message, cb) => { |
| 182 | const eventrouter = require('./eventrouter.js'); |
| 183 | |
| 184 | let errorMessage; |
| 185 | if(!message) { |
| 186 | // error |
| 187 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 188 | logger.log('warn', `Return error - ${errorMessage}`); |
| 189 | cb(errorMessage, false); |
| 190 | return; |
| 191 | } |
| 192 | |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 193 | if(!('workflow_id' in message)) { |
| 194 | // error |
| 195 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 196 | logger.log('warn', `Return error - ${errorMessage}`); |
| 197 | cb(errorMessage, false); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | let workflowId = message.workflow_id; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 202 | let result = eventrouter.checkWorkflow(workflowId); |
| 203 | cb(null, result); |
| 204 | return; |
| 205 | }; |
| 206 | |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 207 | // WebSocket interface for workflow removal |
| 208 | // Message format: |
| 209 | // { |
| 210 | // topic: 'cord.workflow.ctlsvc.workflow.remove', |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 211 | // message: { |
| 212 | // req_id: <req_id> // optional |
| 213 | // workflow_id: <workflow_id> |
| 214 | // } |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 215 | // } |
| 216 | const removeWorkflow = (topic, message, cb) => { |
| 217 | const eventrouter = require('./eventrouter.js'); |
| 218 | |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 219 | let errorMessage; |
| 220 | if(!message) { |
| 221 | // error |
| 222 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 223 | logger.log('warn', `Return error - ${errorMessage}`); |
| 224 | cb(errorMessage, false); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if(!('workflow_id' in message)) { |
| 229 | // error |
| 230 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 231 | logger.log('warn', `Return error - ${errorMessage}`); |
| 232 | cb(errorMessage, false); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | let workflowId = message.workflow_id; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 237 | let result = eventrouter.removeWorkflow(workflowId); |
| 238 | cb(null, result); |
| 239 | return; |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 240 | }; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 241 | |
| 242 | // WebSocket interface for workflow run removal |
| 243 | // Message format: |
| 244 | // { |
| 245 | // topic: 'cord.workflow.ctlsvc.workflow.run.remove', |
| 246 | // message: { |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 247 | // req_id: <req_id> // optional |
| 248 | // workflow_id: <workflow_id>, |
| 249 | // workflow_run_id: <workflow_run_id> |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 250 | // } |
| 251 | // } |
| 252 | const removeWorkflowRun = (topic, message, cb) => { |
| 253 | const eventrouter = require('./eventrouter.js'); |
| 254 | |
| 255 | let errorMessage; |
| 256 | if(!message) { |
| 257 | // error |
| 258 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 259 | logger.log('warn', `Return error - ${errorMessage}`); |
| 260 | cb(errorMessage, false); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if(!('workflow_id' in message)) { |
| 265 | // error |
| 266 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 267 | logger.log('warn', `Return error - ${errorMessage}`); |
| 268 | cb(errorMessage, false); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if(!('workflow_run_id' in message)) { |
| 273 | // error |
| 274 | errorMessage = `field 'workflow_run_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 275 | logger.log('warn', `Return error - ${errorMessage}`); |
| 276 | cb(errorMessage, false); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | let workflowRunId = message.workflow_run_id; |
| 281 | |
| 282 | let result = eventrouter.removeWorkflowRun(workflowRunId); |
| 283 | cb(null, result); |
| 284 | return; |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 285 | }; |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 286 | |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 287 | // WebSocket interface for reporting a new workflow run |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 288 | // Message format: |
| 289 | // { |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 290 | // topic: 'cord.workflow.ctlsvc.workflow.report_new_run', |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 291 | // message: { |
| 292 | // req_id: <req_id> // optional |
| 293 | // workflow_id: <workflow_id>, |
| 294 | // workflow_run_id: <workflow_run_id> |
| 295 | // } |
| 296 | // } |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 297 | const reportNewWorkflowRun = (topic, message, cb) => { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 298 | const eventrouter = require('./eventrouter.js'); |
| 299 | |
| 300 | let errorMessage; |
| 301 | if(!message) { |
| 302 | // error |
| 303 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 304 | logger.log('warn', `Return error - ${errorMessage}`); |
| 305 | cb(errorMessage, false); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if(!('workflow_id' in message)) { |
| 310 | // error |
| 311 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 312 | logger.log('warn', `Return error - ${errorMessage}`); |
| 313 | cb(errorMessage, false); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if(!('workflow_run_id' in message)) { |
| 318 | // error |
| 319 | errorMessage = `field 'workflow_run_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 320 | logger.log('warn', `Return error - ${errorMessage}`); |
| 321 | cb(errorMessage, false); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | let workflowRunId = message.workflow_run_id; |
| 326 | |
| 327 | // there must be a workflow matching |
| 328 | // set the workflow kickstarted |
| 329 | let result = eventrouter.setWorkflowRunKickstarted(workflowRunId); |
| 330 | cb(null, result); |
| 331 | return; |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 334 | // WebSocket interface for reporting workflow run status |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 335 | // Message format: |
| 336 | // { |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 337 | // topic: 'cord.workflow.ctlsvc.workflow.report_run_status', |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 338 | // message: { |
| 339 | // req_id: <req_id> // optional |
| 340 | // workflow_id: <workflow_id>, |
| 341 | // workflow_run_id: <workflow_run_id>, |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 342 | // status: one of ['success', 'running', 'failed', 'unknown'] |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 343 | // } |
| 344 | // } |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 345 | const reportWorkflowRunStatus = (topic, message, cb) => { |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 346 | const eventrouter = require('./eventrouter.js'); |
| 347 | |
| 348 | let errorMessage; |
| 349 | if(!message) { |
| 350 | // error |
| 351 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 352 | logger.log('warn', `Return error - ${errorMessage}`); |
| 353 | cb(errorMessage, false); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if(!('workflow_id' in message)) { |
| 358 | // error |
| 359 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 360 | logger.log('warn', `Return error - ${errorMessage}`); |
| 361 | cb(errorMessage, false); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if(!('workflow_run_id' in message)) { |
| 366 | // error |
| 367 | errorMessage = `field 'workflow_run_id' does not exist in message body - ${JSON.stringify(message)}`; |
| 368 | logger.log('warn', `Return error - ${errorMessage}`); |
| 369 | cb(errorMessage, false); |
| 370 | return; |
| 371 | } |
| 372 | |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 373 | if(!('status' in message)) { |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 374 | // error |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 375 | errorMessage = `field 'status' does not exist in message body - ${JSON.stringify(message)}`; |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 376 | logger.log('warn', `Return error - ${errorMessage}`); |
| 377 | cb(errorMessage, false); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | let workflowRunId = message.workflow_run_id; |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 382 | let status = message.status; |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 383 | |
| 384 | // there must be a workflow matching |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 385 | // set workflow status |
| 386 | let result = eventrouter.setWorkflowRunStatus(workflowRunId, status); |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 387 | cb(null, result); |
| 388 | return; |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 391 | // WebSocket interface for reporting workflow run status bulk |
| 392 | // Message format: |
| 393 | // { |
| 394 | // topic: 'cord.workflow.ctlsvc.workflow.report_run_status_bulk', |
| 395 | // message: { |
| 396 | // req_id: <req_id> // optional |
| 397 | // data: [{ |
| 398 | // workflow_id: <workflow_id>, |
| 399 | // workflow_run_id: <workflow_run_id>, |
| 400 | // status: one of ['success', 'running', 'failed', 'unknown'] |
| 401 | // }, ...] |
| 402 | // } |
| 403 | // } |
| 404 | const reportWorkflowRunStatusBulk = (topic, message, cb) => { |
| 405 | const eventrouter = require('./eventrouter.js'); |
| 406 | |
| 407 | let errorMessage; |
| 408 | if(!message) { |
| 409 | // error |
| 410 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 411 | logger.log('warn', `Return error - ${errorMessage}`); |
| 412 | cb(errorMessage, false); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | if(!('data' in message)) { |
| 417 | // error |
| 418 | errorMessage = `field 'data' does not exist in message body - ${JSON.stringify(message)}`; |
| 419 | logger.log('warn', `Return error - ${errorMessage}`); |
| 420 | cb(errorMessage, false); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | let results = []; |
Illyoung Choi | 8f3ea3d | 2019-07-30 17:49:40 -0700 | [diff] [blame^] | 425 | message.data.forEach((d) => { |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 426 | if(!('workflow_id' in d)) { |
| 427 | // error |
| 428 | errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(d)}`; |
| 429 | logger.log('warn', `Return error - ${errorMessage}`); |
| 430 | cb(errorMessage, false); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | if(!('workflow_run_id' in d)) { |
| 435 | // error |
| 436 | errorMessage = `field 'workflow_run_id' does not exist in message body - ${JSON.stringify(d)}`; |
| 437 | logger.log('warn', `Return error - ${errorMessage}`); |
| 438 | cb(errorMessage, false); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | if(!('status' in d)) { |
| 443 | // error |
| 444 | errorMessage = `field 'status' does not exist in message body - ${JSON.stringify(d)}`; |
| 445 | logger.log('warn', `Return error - ${errorMessage}`); |
| 446 | cb(errorMessage, false); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | let workflowRunId = d.workflow_run_id; |
| 451 | let status = d.status; |
| 452 | |
| 453 | // there must be a workflow matching |
| 454 | // set workflow status |
| 455 | let result = eventrouter.setWorkflowRunStatus(workflowRunId, status); |
| 456 | results.append(result); |
Illyoung Choi | 8f3ea3d | 2019-07-30 17:49:40 -0700 | [diff] [blame^] | 457 | }); |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 458 | |
| 459 | cb(null, results); |
| 460 | return; |
| 461 | } |
| 462 | |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 463 | const getRouter = () => { |
| 464 | return { |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 465 | registerWorkflow: { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 466 | topic: serviceEvents.WORKFLOW_REGISTER, |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 467 | handler: registerWorkflow |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 468 | }, |
| 469 | registerWorkflowEssence: { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 470 | topic: serviceEvents.WORKFLOW_REGISTER_ESSENCE, |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 471 | handler: registerWorkflowEssence |
| 472 | }, |
| 473 | listWorkflows: { |
| 474 | topic: serviceEvents.WORKFLOW_LIST, |
| 475 | handler: listWorkflows |
| 476 | }, |
| 477 | listWorkflowRuns: { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 478 | topic: serviceEvents.WORKFLOW_LIST_RUN, |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 479 | handler: listWorkflowRuns |
| 480 | }, |
| 481 | checkWorkflow: { |
| 482 | topic: serviceEvents.WORKFLOW_CHECK, |
| 483 | handler: checkWorkflow |
| 484 | }, |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 485 | removeWorkflow: { |
| 486 | topic: serviceEvents.WORKFLOW_REMOVE, |
| 487 | handler: removeWorkflow |
| 488 | }, |
| 489 | removeWorkflowRun: { |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 490 | topic: serviceEvents.WORKFLOW_REMOVE_RUN, |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 491 | handler: removeWorkflowRun |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 492 | }, |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 493 | reportNewWorkflowRun: { |
| 494 | topic: serviceEvents.WORKFLOW_REPORT_NEW_RUN, |
| 495 | handler: reportNewWorkflowRun |
| 496 | }, |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 497 | reportWorkflowRunStatus: { |
| 498 | topic: serviceEvents.WORKFLOW_REPORT_RUN_STATUS, |
| 499 | handler: reportWorkflowRunStatus |
| 500 | }, |
| 501 | reportWorkflowRunStatusBulk: { |
| 502 | topic: serviceEvents.WORKFLOW_REPORT_RUN_STATUS_BULK, |
| 503 | handler: reportWorkflowRunStatusBulk |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 504 | } |
| 505 | }; |
| 506 | }; |
| 507 | |
| 508 | // out-going commands |
| 509 | const kickstartWorkflow = (workflowId, workflowRunId) => { |
| 510 | const eventrouter = require('./eventrouter.js'); |
| 511 | |
| 512 | let clients = eventrouter.getWorkflowManagerClients(); |
| 513 | _.forOwn(clients, (client, _clientId) => { |
| 514 | let socket = client.getSocket(); |
| 515 | if(socket) { |
| 516 | socket.emit(serviceEvents.WORKFLOW_KICKSTART, { |
| 517 | workflow_id: workflowId, |
| 518 | workflow_run_id: workflowRunId |
| 519 | }); |
| 520 | } |
| 521 | }); |
| 522 | return; |
| 523 | }; |
| 524 | |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 525 | const checkWorkflowRunStatus = (workflowId, workflowRunId) => { |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 526 | const eventrouter = require('./eventrouter.js'); |
| 527 | |
| 528 | let clients = eventrouter.getWorkflowManagerClients(); |
| 529 | _.forOwn(clients, (client, _clientId) => { |
| 530 | let socket = client.getSocket(); |
| 531 | if(socket) { |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 532 | socket.emit(serviceEvents.WORKFLOW_CHECK_STATUS, { |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 533 | workflow_id: workflowId, |
| 534 | workflow_run_id: workflowRunId |
| 535 | }); |
| 536 | } |
| 537 | }); |
| 538 | return; |
| 539 | }; |
| 540 | |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 541 | const checkWorkflowRunStatusBulk = (requests) => { |
| 542 | // input is an array of |
| 543 | // { |
| 544 | // workflow_id: <workflowId>, |
| 545 | // workflow_run_id: <workflowRunId> |
| 546 | // } |
| 547 | const eventrouter = require('./eventrouter.js'); |
| 548 | |
| 549 | let clients = eventrouter.getWorkflowManagerClients(); |
| 550 | _.forOwn(clients, (client, _clientId) => { |
| 551 | let socket = client.getSocket(); |
| 552 | if(socket) { |
| 553 | socket.emit(serviceEvents.WORKFLOW_CHECK_STATUS_BULK, requests); |
| 554 | } |
| 555 | }); |
| 556 | return; |
| 557 | }; |
| 558 | |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 559 | module.exports = { |
| 560 | serviceEvents: serviceEvents, |
| 561 | getRouter: getRouter, |
Illyoung Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 562 | kickstartWorkflow: kickstartWorkflow, |
Illyoung Choi | ab10903 | 2019-07-29 14:04:10 -0700 | [diff] [blame] | 563 | checkWorkflowRunStatus: checkWorkflowRunStatus, |
| 564 | checkWorkflowRunStatusBulk: checkWorkflowRunStatusBulk |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 565 | }; |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 566 | })(); |