-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaster.js
More file actions
281 lines (268 loc) · 7.36 KB
/
master.js
File metadata and controls
281 lines (268 loc) · 7.36 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var path = require('path');
var utils = require('./utils');
var pass = utils.pass;
var execute = utils.execute;
var writeFile = utils.writeFile;
var findFiles = utils.findFiles;
var readFile = utils.readFile;
var readDir = utils.readDir;
var mkdir = utils.mkdir;
var copyDir = utils.copyDir;
var deleteDir = utils.deleteDir;
var modulePath = utils.modulePath;
var execSync = require('child_process').execSync;
var database = require('./database');
var cluster = require('cluster');
var boardSettings = require('./boardSettings').settings;
/**
* Create control variables
**/
var TMP;
var TMP_SLUG;
var BUILD;
var BUILD_SLUG;
var SKETCHES;
var SKETCHES_SLUG;
var TOOLS;
var TOOLS_SLUG;
var HARDWARE_SLUG;
var LIBRARY_SLUG;
var setup = function(label) {
return new Promise(function(resolve){
TMP_SLUG = '_tm';
BUILD_SLUG = 'b';
SKETCHES_SLUG = 's';
TOOLS_SLUG = 't';
HARDWARE_SLUG = 'h';
LIBRARY_SLUG = 'l';
TMP = path.resolve( process.env.COMPILER_BUILD_ROOT || process.env.BUILD_ROOT || './', TMP_SLUG );
BUILD = path.resolve(TMP, BUILD_SLUG);
SKETCHES = path.resolve(TMP, SKETCHES_SLUG);
TOOLS = path.resolve(TMP, TOOLS_SLUG);
resolve();
});
}
/**
* Clean up temporary directories
**/
var precleanUp = function() {
return new Promise(function(resolve, reject){
pass()
.then(deleteDir(path.resolve(TMP)))
.then(mkdir(path.resolve(TMP)))
.then(mkdir(path.resolve(BUILD)))
.then(mkdir(path.resolve(TOOLS)))
.then(mkdir(path.resolve(SKETCHES)))
.then(copyDir(path.resolve(__dirname, 'firmware', 'firmware.ino'), path.resolve(SKETCHES, 'firmware.ino')))
.then(copyDir(path.resolve(modulePath('quirkbot-arduino-builder')), path.resolve(TOOLS, 'quirkbot-arduino-builder')))
//.then(copyDir(path.resolve(modulePath('quirkbot-avr-gcc')), path.resolve(TOOLS, 'quirkbot-avr-gcc')))
.then(copyDir(path.resolve(modulePath('quirkbot-arduino-hardware')), path.resolve(TOOLS, HARDWARE_SLUG)))
.then(copyDir(path.resolve(modulePath('quirkbot-arduino-library')), path.resolve(TOOLS, LIBRARY_SLUG)))
.then(resolve)
.catch(reject);
});
}
/**
* Save configs regarding the library and hardware info
**/
var saveLibraryConfig = function() {
return new Promise(function(resolve){
pass()
.then(readFile(path.resolve(TOOLS, LIBRARY_SLUG, 'library.properties')))
.then(function (info) {
database.setConfig('library-info',info);
resolve();
})
.catch(function (error) {
console.log('Error saving library-info.', error);
reject(error);
})
});
}
var saveHardwareConfig = function() {
return new Promise(function(resolve){
pass()
.then(readFile(path.resolve(TOOLS, HARDWARE_SLUG, 'avr', 'version.txt')))
.then(function (info) {
database.setConfig('hardware-info',info);
resolve();
})
.catch(function (error) {
console.log('Error saving hardware-info.', error);
reject(error);
});
});
}
/**
* Compile the reset firmware
**/
var compileResetFirmaware = function() {
return new Promise(function(resolve){
var precompileCommand =
'"' + path.resolve(TOOLS, 'quirkbot-arduino-builder', 'tools', 'arduino-builder') + '" ' +
'-hardware="' + path.resolve(TOOLS) + '" ' +
'-hardware="' + path.resolve(TOOLS, 'quirkbot-arduino-builder', 'tools', 'hardware') + '" ' +
'-libraries="' + path.resolve(TOOLS) + '" ' +
//'-tools="' + path.resolve(TOOLS, 'quirkbot-avr-gcc', 'tools') + '" ' +
'-tools="' + path.resolve(modulePath('quirkbot-avr-gcc'), 'tools') + '" ' +
'-tools="' + path.resolve(TOOLS, 'quirkbot-arduino-builder', 'tools', 'tools') + '" ' +
'-fqbn="'+HARDWARE_SLUG+':avr:quirkbot" ' +
'-ide-version=10607 ' +
'-build-path="' + path.resolve(BUILD) + '" ' +
'-verbose ' +
'"' + path.resolve(SKETCHES, 'firmware.ino') + '"';
console.log(precompileCommand);
pass()
.then(execute(precompileCommand))
.then(function(result){
console.log(result.stdout)
})
.then(readFile(path.resolve(BUILD, 'firmware.ino.hex')))
.then(function (hex) {
database.setConfig('firmware-reset', hex);
resolve();
})
.catch(function (error) {
console.log('Error saving reset firmware.', error);
database.removeConfig('firmware-reset');
reject(error)
});
});
}
/**
* Prepare cluster
**/
var prepareCluster = function(){
return new Promise(function(resolve, reject){
var forks = [];
var initForks = function (argument) {
var numForks = process.env.WEB_CONCURRENCY || require('os').cpus().length;
console.log('Number of forks: '+ numForks);
for (var label = 0; label < numForks; label++) {
createFork(label);
}
}
var createFork = function(label){
console.log('Creating fork: '+ label);
var fork = {};
forks[label] = fork;
fork.label = label;
fork.free = false;
fork.worker = cluster.fork();
fork.currentWorkerId = fork.worker.id;
fork.worker.on('message', function(message) {
if(message.type === 'success'){
database.setReady(message.data.id, message.data.hex, message.data.error, message.data.size);
console.log('ask', message.data.worker)
var fork = forks[message.data.worker];
if(!fork.worker.isDead()){
fork.free = true;
doJob(fork);
}
}
else if(message.type === 'init'){
console.log('Fork is ready: '+ label);
var fork = forks[message.data.worker];
if(!fork.worker.isDead()){
fork.free = true;
doJob(fork);
}
}
});
fork.worker.send({
type: 'label',
data: label
})
}
var doJob = function(fork){
database.getNext()
.then(function(instance){
if(!fork.worker.isDead()){
//console.log('do', fork.label, instance.id)
fork.free = false;
fork.worker.send({
type: 'run',
data: {
id: instance.id,
code: instance.code
}
});
}
})
}
var pushJobs = function(){
//console.log('*')
var freeForks = forks.filter(function(fork){
return fork.free;
})
if(freeForks.length){
database.countPending()
.then(function(count){
if(count){
var pushes = 0;
forks.forEach(function(fork){
if(pushes >= count) return;
if(!fork.free) return;
pushes++;
//console.log('push', fork.label)
doJob(fork)
})
//console.log('- job!')
setTimeout(pushJobs, 300);
}
else{
//console.log('- empty')
setTimeout(pushJobs, 2000);
}
})
.catch(function(error) {
//console.log('- error')
setTimeout(pushJobs, 2000);
})
}
else{
//console.log('- busy')
setTimeout(pushJobs, 300);
}
}
cluster.on('exit', function(worker, code, signal) {
var delay = 5000;
console.log('worker ' + worker.id + ' died, reviving in ' + delay + 'ms.');
setTimeout(function() {
console.log('Searching for fork that owns worker: '+ worker.id);
var fork;
Object.keys(forks).forEach(function (label) {
if(forks[label].currentWorkerId === worker.id){
fork = forks[label];
}
});
if(fork){
console.log('Reviving fork: '+ fork.label);
createFork(fork.label);
}
else{
console.log('Fatal error: could not find worker ' + worker.id );
}
}, 5000)
});
initForks();
setTimeout(pushJobs, 100);
resolve();
});
};
/**
* Start application
**/
pass()
.then(setup)
.then(precleanUp)
.then(saveLibraryConfig)
.then(saveHardwareConfig)
.then(compileResetFirmaware)
.then(prepareCluster)
.then(function(){
console.log('Compiler started sucessfully!');
})
.catch(function(error){
console.log('Compiler failed', error);
});