Skip to content

Commit 73b4485

Browse files
committed
PEER-220: Try fix sleep bug
Signed-off-by: SeeuSim <[email protected]>
1 parent 1976b30 commit 73b4485

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

backend/matching/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ server.listen(port, () => {
1616
});
1717

1818
const shutdown = async () => {
19-
await Promise.all(workers.map((worker) => worker.kill('SIGTERM')));
19+
await Promise.all(workers.map((worker) => worker.kill()));
2020
server.close(() => {
2121
logger.info('App shut down');
2222
});

backend/matching/src/workers/cleaner.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ const logger = {
88
error: (message: unknown) => process.send && process.send(message),
99
};
1010

11-
process.on('SIGTERM', () => {
11+
const sleepTime = 5000;
12+
let stopSignal = false;
13+
let timeout: ReturnType<typeof setTimeout>;
14+
15+
const cancel = () => {
16+
stopSignal = true;
17+
clearTimeout(timeout);
18+
};
19+
const shutdown = () => {
20+
cancel();
1221
client
1322
.disconnect()
1423
.then(() => client.quit())
1524
.then(process.exit(0));
16-
});
25+
};
26+
27+
process.on('SIGINT', shutdown);
28+
process.on('SIGHUP', shutdown);
29+
process.on('SIGTERM', shutdown);
1730

1831
async function clean() {
1932
const redisClient = client.isReady || client.isOpen ? client : await client.connect();
@@ -27,7 +40,7 @@ async function clean() {
2740

2841
if (!response || response.messages.length === 0) {
2942
await new Promise((resolve, _reject) => {
30-
setTimeout(() => resolve('Next Loop'), 5000);
43+
timeout = setTimeout(() => resolve('Next Loop'), sleepTime);
3144
});
3245
return;
3346
}
@@ -55,6 +68,10 @@ async function clean() {
5568
}
5669

5770
(function loop() {
71+
if (stopSignal) {
72+
return;
73+
}
74+
5875
Promise.resolve()
5976
.then(async () => await clean())
6077
.catch((err) => {

backend/matching/src/workers/matcher.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ const logger = {
99
error: (message: unknown) => process.send && process.send(message),
1010
};
1111

12-
process.on('SIGTERM', () => {
12+
const sleepTime = 5000;
13+
let stopSignal = false;
14+
let timeout: ReturnType<typeof setTimeout>;
15+
const cancel = () => {
16+
stopSignal = true;
17+
clearTimeout(timeout);
18+
};
19+
const shutdown = () => {
20+
cancel();
1321
client
1422
.disconnect()
1523
.then(() => client.quit())
1624
.then(process.exit(0));
17-
});
25+
};
26+
27+
process.on('SIGINT', shutdown);
28+
process.on('SIGHUP', shutdown);
29+
process.on('SIGTERM', shutdown);
1830

1931
type RequestorParams = {
2032
requestorUserId: string;
@@ -76,7 +88,7 @@ async function match() {
7688
);
7789
if (!stream || stream.length === 0) {
7890
await new Promise((resolve, _reject) => {
79-
setTimeout(() => resolve('Next Loop'), 5000);
91+
timeout = setTimeout(() => resolve('Next Loop'), sleepTime);
8092
});
8193
return;
8294
}
@@ -160,6 +172,10 @@ async function match() {
160172
}
161173

162174
(function loop() {
175+
if (stopSignal) {
176+
return;
177+
}
178+
163179
Promise.resolve()
164180
.then(async () => await match())
165181
.catch((error) => {

0 commit comments

Comments
 (0)