-
Notifications
You must be signed in to change notification settings - Fork 1
/
signal_handler.ts
45 lines (42 loc) · 1.29 KB
/
signal_handler.ts
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
import { TaskManager } from "./task_manager.ts";
export const ExitTarget = new EventTarget();
export function add_exit_handler(m: TaskManager) {
let first_aborted = true;
let ignore_signal = false;
const force_kill = () => {
m.abort();
m.force_abort();
ExitTarget.dispatchEvent(new Event("close"));
m.close();
};
const handler = async () => {
if (ignore_signal) return;
if (first_aborted) {
m.abort();
console.log(
"Already abort all tasks. Please wait for a while. You can press Ctrl + C again to force abort.",
);
first_aborted = false;
} else {
m.force_abort();
ignore_signal = true;
return;
}
await m.waiting_unfinished_task();
ExitTarget.dispatchEvent(new Event("close"));
m.close();
};
Deno.addSignalListener("SIGINT", handler);
if (Deno.build.os !== "windows") {
Deno.addSignalListener("SIGTERM", force_kill);
}
}
export function get_abort_signal(callback?: () => void): AbortSignal {
const a = new AbortController();
const handler = () => {
a.abort();
if (callback) callback();
};
Deno.addSignalListener("SIGINT", handler);
return a.signal;
}