Add exit api

This commit is contained in:
2023-06-07 09:51:32 +08:00
parent 3960d730c0
commit 2002dd4749
4 changed files with 66 additions and 6 deletions

27
routes/api/exit.ts Normal file
View File

@@ -0,0 +1,27 @@
import { Handlers } from "$fresh/server.ts";
import { parse_bool } from "../../server/parse_form.ts";
import { get_task_manager } from "../../server.ts";
export const handler: Handlers = {
async POST(req, _ctx) {
let force = false;
try {
const form = await req.formData();
force = await parse_bool(form.get("force"), false);
} catch (_) {
null;
}
setTimeout(async () => {
const m = get_task_manager();
const aborted = m.aborted;
m.abort();
if (force) {
m.force_abort();
}
if (aborted) return;
await m.waiting_unfinished_task();
m.close();
}, 1);
return new Response("Aborted.");
},
};