-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.js
55 lines (44 loc) · 1.24 KB
/
utils.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
"use strict";
const mutex = require('ocore/mutex.js');
let watchedKeys = {};
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function die(msg) {
throw Error(msg);
}
async function checkForDeadlock(key) {
const t = setTimeout(die, 10 * 60 * 1000, `possible deadlock on ${key}`);
const unlock = await mutex.lock(key);
unlock();
clearTimeout(t);
}
function watchForDeadlock(key) {
if (watchedKeys[key])
return console.log('already watching for deadlock on ' + key);
watchedKeys[key] = true;
setInterval(() => checkForDeadlock(key), 10 * 60 * 1000);
}
function getVersion(versions, aa) {
for (let v in versions)
if (versions[v] === aa)
return v;
return null;
}
async function asyncCallWithTimeout(asyncPromise, timeLimit = 60000) {
let timeoutHandle;
const timeoutPromise = new Promise((_resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error(`async call timeout limit ${timeLimit} reached`)),
timeLimit
);
});
return Promise.race([asyncPromise, timeoutPromise]).then(result => {
clearTimeout(timeoutHandle);
return result;
});
}
exports.asyncCallWithTimeout = asyncCallWithTimeout;
exports.wait = wait;
exports.watchForDeadlock = watchForDeadlock;
exports.getVersion = getVersion;