forked from openfoodfacts/openfoodfacts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_taxonomies.js
108 lines (94 loc) · 2.71 KB
/
refresh_taxonomies.js
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
/*eslint no-console: "off"*/
/*eslint no-await-in-loop: "off"*/
/*global process require*/
const util = require('util');
const { spawn, execFile } = require('child_process');
const execFilePromise = util.promisify(execFile);
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function gitMtime(path) {
const { stdout } = await execFilePromise('git', ['log', '-1', '--format="%at"', '--', path]);
return parseInt(stdout.trim().replace('"', '').trim(), 10);
}
async function main() {
const files = [
'additives_classes',
'additives',
'allergens',
'amino_acids',
'categories',
'countries',
'ingredients',
'ingredients_analysis',
'labels',
'languages',
'minerals',
'nova_groups',
'nucleotides',
'other_nutritional_substances',
'test',
'vitamins'
];
const spawns = [];
for (const file of files) {
const txtPath = `taxonomies/${file}.txt`;
const stoPath = `taxonomies/${file}.result.sto`;
const txtMtimeMs = await gitMtime(txtPath);
const stoMtimeMs = await gitMtime(stoPath);
if (stoMtimeMs >= txtMtimeMs) {
console.log(`${txtPath} is up-to-date`);
}
else {
console.log(`${txtPath} needs to be rebuilt; ${txtMtimeMs} vs ${stoMtimeMs}`);
try {
// Launch the Perl script to rebuild the taxonomy and output STDOUT
// and STDERR to /dev/null, because Travis CI cannot take that much
// build log information.
const perl = spawn(
'perl',
['-CS', '-Ilib', 'scripts/build_tags_taxonomy.pl', file, '1'],
{ stdio: ['pipe', 'ignore', 'ignore'] }
);
spawns.push(perl);
perl.on('close', (code) => {
console.log(`[${file}] child process exited with code ${code}`);
if (code != 0) {
process.exitCode = code;
}
});
} catch (e) {
console.error(e);
process.exitCode = e.code;
throw e;
}
}
}
console.log('Waiting for spawned processed to exit.');
let running = Number.MAX_SAFE_INTEGER;
let lastMsg = new Date();
while (running > 0) {
running = spawns.reduce((pv, cv) => {
if (cv.exitCode === null) {
return pv + 1;
}
else {
return pv;
}
}, 0);
const now = new Date();
const dateDiff = now - lastMsg;
if (dateDiff >= 300000) {
// Output a log statement at most and at least every 5 minutes,
// because Travis CI jobs are assumed to be dead and get killed
// after 10 Minutes of inactivity.
console.log(`${running} processes still running`);
lastMsg = now;
}
if (running > 0) {
await sleep(250);
}
}
console.log('Done!');
}
main();