added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / agenda / jobs / test-execution-job.js
diff --git a/otf-frontend/server/src/agenda/jobs/test-execution-job.js b/otf-frontend/server/src/agenda/jobs/test-execution-job.js
new file mode 100644 (file)
index 0000000..b9a9198
--- /dev/null
@@ -0,0 +1,133 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+const request = require('request');\r
+const requestPromise = require('request-promise');\r
+const logger = require('../../lib/logger');\r
+const emitter = require('../result-emitter').emitter;\r
+const config = require('config');\r
+\r
+const loggerTagExecuteTestSync = '[JOB-sync] ';\r
+const loggerTagExecuteTestAsync = '[JOB-async] ';\r
+\r
+module.exports = function (agenda) {\r
+       // [Job Definition] : Executes a testInstance synchronously.\r
+       agenda.define('executeTestSync', (job) => {\r
+               logger.debug(loggerTagExecuteTestSync + 'Running job %s.', job.attrs._id);\r
+\r
+               // Extact the testSchedule from the job data.\r
+               const testSchedule = job.attrs.data.testSchedule;\r
+\r
+               logger.debug('[POST-' +\r
+                       config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId + ']');\r
+\r
+               // Create and send the request\r
+               requestPromise.post({\r
+                       url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,\r
+                       headers: {\r
+                               'Authorization': job.attrs.data.authorizationHeader,\r
+                               'Content-Type': 'application/json'\r
+                       },\r
+                       body: {\r
+                               'async': false,\r
+                               'executorId': testSchedule._executorId\r
+                       },\r
+                       json: true\r
+               }, function onResponseOk(response) {\r
+                       logger.debug('[POST-ok]: ' + JSON.stringify(response));\r
+                       emitter.emit(job.attrs._id + '_ok', response);\r
+               }, function onResponseError(response) {\r
+                       logger.debug('[POST-error]: ' + JSON.stringify(response));\r
+                       emitter.emit(job.attrs._id + '_error', response);\r
+               });\r
+       });\r
+\r
+       // [Job Definition] : Executes a testInstance asynchronously.\r
+       agenda.define('executeTestAsync', (job, done) => {\r
+               logger.debug(loggerTagExecuteTestAsync + 'Running job %s.', job.attrs._id);\r
+\r
+               // Extact the testSchedule from the job data.\r
+               const testSchedule = job.attrs.data.testSchedule;\r
+\r
+               if (testSchedule._testInstanceEndDate) {\r
+                       const currentDate = Date.now().valueOf();\r
+                       const endDate = Date.parse(testSchedule._testInstanceEndDate).valueOf();\r
+\r
+                       if (currentDate >= endDate) {\r
+                               job.remove(err => {\r
+                                       if (!err) {\r
+                                               logger.debug('Job %s finished.', job.attrs._id);\r
+                                       } else {\r
+                                               logger.error(err);\r
+                                       }\r
+                               });\r
+\r
+                               done();\r
+                               return;\r
+                       }\r
+               }\r
+\r
+               logger.debug('[POST-%s]', config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId);\r
+\r
+               // Create and send the request (we don't care about the response)\r
+               request.post({\r
+                       url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,\r
+                       headers: {\r
+                               'Authorization': job.attrs.data.authorizationHeader,\r
+                               'Content-Type': 'application/json'\r
+                       },\r
+                       body: {\r
+                               'async': true,\r
+                               'executorId': testSchedule._executorId\r
+                       },\r
+                       json: true\r
+               }, function onResponseOk(response) {\r
+                       logger.debug('[POST-ok]: ' + JSON.stringify(response));\r
+                       emitter.emit(job.attrs._id + '_ok', response);\r
+               }, function onResponseError(response) {\r
+                       logger.debug('[POST-error]: ' + JSON.stringify(response));\r
+                       emitter.emit(job.attrs._id + '_error', response);\r
+               });\r
+\r
+               done();\r
+       });\r
+\r
+       agenda.define('executeTestOnInterval', (job, done) => {\r
+               logger.debug('[JOB-executeTestOnInterval] running...');\r
+\r
+               // Extact the testSchedule from the job data.\r
+               const testSchedule = job.attrs.data.testSchedule;\r
+\r
+               if (testSchedule._testInstanceEndDate) {\r
+                       if (new Date().now() > testSchedule._testInstanceEndDate) {\r
+                               job.remove((err) => {\r
+                                       if (err) {\r
+                                               logger.error(err); \r
+                                       }\r
+                               });\r
+                       }\r
+               }\r
+\r
+               logger.info('exec freq ' + testSchedule.testInstanceExecFreqInSeconds());\r
+\r
+               agenda.every(\r
+                       testSchedule._testInstanceExecFreqInSeconds + ' seconds',\r
+                       'executeTestAsync',\r
+                       {testSchedule: job.attrs.data.testSchedule, authorizationHeader: job.attrs.data.authorizationHeader});\r
+\r
+               done();\r
+       });\r
+};\r