Fix /api/exit can not exit

This commit is contained in:
2023-06-12 13:20:51 +08:00
parent 9b2d696733
commit c26890be4c
2 changed files with 8 additions and 2 deletions

View File

@@ -3,6 +3,7 @@
"tasks": {
"cache": "deno cache main.ts server-dev.ts",
"server-dev": "deno run -A --unstable \"--watch=static/*.css,static/*.ts,static/*/,routes/,translation/\" server-dev.ts",
"server": "deno run -A --unstable server-run.ts",
"test": "deno test --allow-read=./ --allow-net --allow-write=./ --allow-run=tasklist.exe --unstable",
"run": "deno run --allow-read=./ --allow-write=./ --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable",
"compile": "deno compile --allow-read=./ --allow-write=./ --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable",

View File

@@ -1,6 +1,8 @@
import { Handlers } from "$fresh/server.ts";
import { parse_bool } from "../../server/parse_form.ts";
import { get_task_manager } from "../../server.ts";
import { ExitTarget } from "../../signal_handler.ts";
import { AlreadyClosedError } from "../../task_manager.ts";
export const handler: Handlers = {
async POST(req, _ctx) {
@@ -11,7 +13,7 @@ export const handler: Handlers = {
} catch (_) {
null;
}
setTimeout(async () => {
const h = async () => {
const m = get_task_manager();
const aborted = m.aborted;
m.abort();
@@ -20,8 +22,11 @@ export const handler: Handlers = {
}
if (aborted) return;
await m.waiting_unfinished_task();
ExitTarget.dispatchEvent(new Event("close"));
m.close();
}, 1);
throw new AlreadyClosedError();
};
setTimeout(h, 1);
return new Response("Aborted.");
},
};