-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmicroWorker.js
More file actions
166 lines (140 loc) · 4.46 KB
/
microWorker.js
File metadata and controls
166 lines (140 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
var self = microWorker;
module.exports = self;
var Adapter = require('./_common/shippable/Adapter.js');
var exec = require('child_process').exec;
var pathPlaceholder = '{{TYPE}}';
var workflowPath = './workflows/' + pathPlaceholder + '.js';
var JobConsoleAdapter = require('./_common/jobConsoleAdapter.js');
var BuildJobConsoleAdapter = require('./_common/buildJobConsoleAdapter.js');
function microWorker(message) {
var bag = {
rawMessage: message
};
bag.who = util.format('%s|%s', msName, self.name);
logger.info(bag.who, 'Inside');
async.series([
_checkInputParams.bind(null, bag),
_instantiateConsoleAdapter.bind(null, bag),
_applyWorkflowStrategy.bind(null, bag)
],
function (err) {
if (err)
logger.error(bag.who, util.format('Failed to process message'));
else
logger.info(bag.who, util.format('Successfully processed message'));
__restartExecContainer(bag);
}
);
}
function _checkInputParams(bag, next) {
var who = bag.who + '|' + _checkInputParams.name;
logger.verbose(who, 'Inside');
if (_.isEmpty(bag.rawMessage)) {
logger.warn(util.format('%s, Message is empty.', who));
return next(true);
}
if (!bag.rawMessage.builderApiToken) {
logger.warn(util.format('%s, No builderApiToken present' +
' in incoming message', who));
return next(true);
}
bag.builderApiToken = bag.rawMessage.builderApiToken;
bag.builderApiAdapter = new Adapter(bag.rawMessage.builderApiToken);
return next();
}
function _instantiateConsoleAdapter(bag, next) {
var who = bag.who + '|' + _instantiateConsoleAdapter.name;
logger.verbose(who, 'Inside');
if (bag.rawMessage.jobId) {
bag.workflow = 'ci';
bag.consoleAdapter = new JobConsoleAdapter(bag.rawMessage.builderApiToken,
bag.rawMessage.jobId, bag.rawMessage.consoleBatchSize,
bag.rawMessage.consoleBufferTimeIntervalInMS);
} else if (bag.rawMessage.buildJobId) {
bag.workflow = 'runSh';
var batchSize = bag.rawMessage.consoleBatchSize ||
(global.systemSettings && global.systemSettings.jobConsoleBatchSize);
var timeInterval = bag.rawMessage.consoleBufferTimeIntervalInMS ||
(global.systemSettings &&
global.systemSettings.jobConsoleBufferTimeIntervalInMS);
bag.consoleAdapter = new BuildJobConsoleAdapter(
bag.rawMessage.builderApiToken, bag.rawMessage.buildJobId,
batchSize, timeInterval);
} else {
logger.warn(util.format('%s, No job/buildJob ID ' +
'in incoming message', who));
return next(true);
}
return next();
}
function _applyWorkflowStrategy(bag, next) {
var who = bag.who + '|' + _applyWorkflowStrategy.name;
logger.verbose(who, 'Inside');
if (bag.workflow === 'ci')
if (bag.rawMessage.payload && bag.rawMessage.payload.resourceId)
bag.workflow = 'runCI';
var strategyPath = workflowPath.replace(pathPlaceholder, bag.workflow);
var workflowStrategy;
try {
workflowStrategy = require(strategyPath);
} catch (e) {
logger.warn(bag.who, util.inspect(e));
}
if (!workflowStrategy) {
logger.warn(util.format(
'Strategy not found workflow: %s', bag.workflow));
return next(true);
}
workflowStrategy(bag,
function (err) {
if (err) {
logger.warn(who,
util.format('Failed to apply strategy for workflow: %s',
bag.workflow)
);
return next(err);
}
return next();
}
);
}
function __restartExecContainer(bag) {
var who = bag.who + '|' + __restartExecContainer.name;
logger.verbose(who, 'Inside');
var retryOpts = {
times: 5,
interval: function (retryCount) {
return 1000 * Math.pow(2, retryCount);
}
};
async.retry(retryOpts,
function (callback) {
var callsPending = 0;
if (bag.consoleAdapter)
callsPending = bag.consoleAdapter.getPendingApiCallCount();
if (callsPending < 1) {
__restartContainer(bag);
return callback();
}
return callback(true);
},
function (err) {
if (err)
logger.error('Still posting build consoles');
// force restarting container
__restartContainer(bag);
}
);
}
function __restartContainer(bag) {
var who = bag.who + '|' + __restartContainer.name;
logger.verbose(who, 'Inside');
exec(util.format('docker restart -t=0 %s', config.reqProcContainerName),
function (err) {
if (err)
logger.error(util.format('Failed to stop container with ' +
'err:%s', err));
}
);
}