added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / agenda / jobs / test-execution-job.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 const request = require('request');\r
18 const requestPromise = require('request-promise');\r
19 const logger = require('../../lib/logger');\r
20 const emitter = require('../result-emitter').emitter;\r
21 const config = require('config');\r
22 \r
23 const loggerTagExecuteTestSync = '[JOB-sync] ';\r
24 const loggerTagExecuteTestAsync = '[JOB-async] ';\r
25 \r
26 module.exports = function (agenda) {\r
27         // [Job Definition] : Executes a testInstance synchronously.\r
28         agenda.define('executeTestSync', (job) => {\r
29                 logger.debug(loggerTagExecuteTestSync + 'Running job %s.', job.attrs._id);\r
30 \r
31                 // Extact the testSchedule from the job data.\r
32                 const testSchedule = job.attrs.data.testSchedule;\r
33 \r
34                 logger.debug('[POST-' +\r
35                         config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId + ']');\r
36 \r
37                 // Create and send the request\r
38                 requestPromise.post({\r
39                         url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,\r
40                         headers: {\r
41                                 'Authorization': job.attrs.data.authorizationHeader,\r
42                                 'Content-Type': 'application/json'\r
43                         },\r
44                         body: {\r
45                                 'async': false,\r
46                                 'executorId': testSchedule._executorId\r
47                         },\r
48                         json: true\r
49                 }, function onResponseOk(response) {\r
50                         logger.debug('[POST-ok]: ' + JSON.stringify(response));\r
51                         emitter.emit(job.attrs._id + '_ok', response);\r
52                 }, function onResponseError(response) {\r
53                         logger.debug('[POST-error]: ' + JSON.stringify(response));\r
54                         emitter.emit(job.attrs._id + '_error', response);\r
55                 });\r
56         });\r
57 \r
58         // [Job Definition] : Executes a testInstance asynchronously.\r
59         agenda.define('executeTestAsync', (job, done) => {\r
60                 logger.debug(loggerTagExecuteTestAsync + 'Running job %s.', job.attrs._id);\r
61 \r
62                 // Extact the testSchedule from the job data.\r
63                 const testSchedule = job.attrs.data.testSchedule;\r
64 \r
65                 if (testSchedule._testInstanceEndDate) {\r
66                         const currentDate = Date.now().valueOf();\r
67                         const endDate = Date.parse(testSchedule._testInstanceEndDate).valueOf();\r
68 \r
69                         if (currentDate >= endDate) {\r
70                                 job.remove(err => {\r
71                                         if (!err) {\r
72                                                 logger.debug('Job %s finished.', job.attrs._id);\r
73                                         } else {\r
74                                                 logger.error(err);\r
75                                         }\r
76                                 });\r
77 \r
78                                 done();\r
79                                 return;\r
80                         }\r
81                 }\r
82 \r
83                 logger.debug('[POST-%s]', config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId);\r
84 \r
85                 // Create and send the request (we don't care about the response)\r
86                 request.post({\r
87                         url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,\r
88                         headers: {\r
89                                 'Authorization': job.attrs.data.authorizationHeader,\r
90                                 'Content-Type': 'application/json'\r
91                         },\r
92                         body: {\r
93                                 'async': true,\r
94                                 'executorId': testSchedule._executorId\r
95                         },\r
96                         json: true\r
97                 }, function onResponseOk(response) {\r
98                         logger.debug('[POST-ok]: ' + JSON.stringify(response));\r
99                         emitter.emit(job.attrs._id + '_ok', response);\r
100                 }, function onResponseError(response) {\r
101                         logger.debug('[POST-error]: ' + JSON.stringify(response));\r
102                         emitter.emit(job.attrs._id + '_error', response);\r
103                 });\r
104 \r
105                 done();\r
106         });\r
107 \r
108         agenda.define('executeTestOnInterval', (job, done) => {\r
109                 logger.debug('[JOB-executeTestOnInterval] running...');\r
110 \r
111                 // Extact the testSchedule from the job data.\r
112                 const testSchedule = job.attrs.data.testSchedule;\r
113 \r
114                 if (testSchedule._testInstanceEndDate) {\r
115                         if (new Date().now() > testSchedule._testInstanceEndDate) {\r
116                                 job.remove((err) => {\r
117                                         if (err) {\r
118                                                 logger.error(err); \r
119                                         }\r
120                                 });\r
121                         }\r
122                 }\r
123 \r
124                 logger.info('exec freq ' + testSchedule.testInstanceExecFreqInSeconds());\r
125 \r
126                 agenda.every(\r
127                         testSchedule._testInstanceExecFreqInSeconds + ' seconds',\r
128                         'executeTestAsync',\r
129                         {testSchedule: job.attrs.data.testSchedule, authorizationHeader: job.attrs.data.authorizationHeader});\r
130 \r
131                 done();\r
132         });\r
133 };\r