mutex.js
2.11 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
'use strict';
const DEBUGMODE = process.env.NODE_ENV !== 'production';
const locks = require('locks');
const logger = require('komodo-sdk/logger');
const mutexWaitForOK = locks.createMutex();
const mutexCommand = locks.createMutex();
const mutexSubCommand = locks.createMutex();
function setLockWaitForOK() {
if (DEBUGMODE) logger.verbose('Entering MUTEX OK');
return new Promise((resolve) => {
mutexWaitForOK.lock(() => {
if (DEBUGMODE) logger.verbose('MUTEX OK entered');
resolve(true);
});
});
}
function releaseLockWaitForOK() {
try {
if (DEBUGMODE) logger.verbose('Leaving MUTEX OK');
mutexWaitForOK.unlock();
} catch (e) {
//
}
}
function setLockWaitForCommand() {
if (DEBUGMODE) logger.verbose('Entering MUTEX Command');
return new Promise((resolve) => {
mutexCommand.lock(() => {
if (DEBUGMODE) logger.verbose('MUTEX Command entered');
resolve(true);
});
});
}
function tryLockWaitForCommand() {
return mutexCommand.tryLock();
}
function releaseLockWaitForCommand() {
try {
if (DEBUGMODE) logger.verbose('Leaving MUTEX Command');
mutexCommand.unlock();
} catch (e) {
//
}
}
function setLockWaitForSubCommand() {
if (DEBUGMODE) logger.verbose('Entering MUTEX SUBCOMMAND');
return new Promise((resolve) => {
mutexSubCommand.lock(() => {
if (DEBUGMODE) logger.verbose('MUTEX SUBCOMMAND entered');
resolve(true);
});
});
}
function releaseLockWaitForSubCommand() {
try {
if (DEBUGMODE) logger.verbose('Leaving MUTEX SUBCOMMAND');
mutexSubCommand.unlock();
} catch (e) {
//
}
}
exports.setLockWaitForOK = setLockWaitForOK;
exports.releaseLockWaitForOK = releaseLockWaitForOK;
exports.setLockWaitForCommand = setLockWaitForCommand;
exports.releaseLockWaitForCommand = releaseLockWaitForCommand;
exports.tryLockWaitForCommand = tryLockWaitForCommand;
exports.setLockWaitForSubCommand = setLockWaitForSubCommand;
exports.releaseLockWaitForSubCommand = releaseLockWaitForSubCommand;