1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
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
23 const loggerTagExecuteTestSync = '[JOB-sync] ';
\r
24 const loggerTagExecuteTestAsync = '[JOB-async] ';
\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
31 // Extact the testSchedule from the job data.
\r
32 const testSchedule = job.attrs.data.testSchedule;
\r
34 logger.debug('[POST-' +
\r
35 config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId + ']');
\r
37 // Create and send the request
\r
38 requestPromise.post({
\r
39 url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,
\r
41 'Authorization': job.attrs.data.authorizationHeader,
\r
42 'Content-Type': 'application/json'
\r
46 'executorId': testSchedule._executorId
\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
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
62 // Extact the testSchedule from the job data.
\r
63 const testSchedule = job.attrs.data.testSchedule;
\r
65 if (testSchedule._testInstanceEndDate) {
\r
66 const currentDate = Date.now().valueOf();
\r
67 const endDate = Date.parse(testSchedule._testInstanceEndDate).valueOf();
\r
69 if (currentDate >= endDate) {
\r
72 logger.debug('Job %s finished.', job.attrs._id);
\r
83 logger.debug('[POST-%s]', config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId);
\r
85 // Create and send the request (we don't care about the response)
\r
87 url: config.serviceApi.url + config.serviceApi.uriExecuteTestInstance + testSchedule._testInstanceId,
\r
89 'Authorization': job.attrs.data.authorizationHeader,
\r
90 'Content-Type': 'application/json'
\r
94 'executorId': testSchedule._executorId
\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
108 agenda.define('executeTestOnInterval', (job, done) => {
\r
109 logger.debug('[JOB-executeTestOnInterval] running...');
\r
111 // Extact the testSchedule from the job data.
\r
112 const testSchedule = job.attrs.data.testSchedule;
\r
114 if (testSchedule._testInstanceEndDate) {
\r
115 if (new Date().now() > testSchedule._testInstanceEndDate) {
\r
116 job.remove((err) => {
\r
118 logger.error(err);
\r
124 logger.info('exec freq ' + testSchedule.testInstanceExecFreqInSeconds());
\r
127 testSchedule._testInstanceExecFreqInSeconds + ' seconds',
\r
128 'executeTestAsync',
\r
129 {testSchedule: job.attrs.data.testSchedule, authorizationHeader: job.attrs.data.authorizationHeader});
\r