Fix utt can not handle Ctrl + C

This commit is contained in:
2023-05-23 12:40:59 +08:00
parent 5c9712a02c
commit ab4be7f8b4
4 changed files with 13 additions and 5 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
config.json
test/
downloads/
utt.lock

View File

@@ -1,4 +1,5 @@
import { EhDb } from "./db.ts";
import { asyncForEach } from "./utils.ts";
export type GHAuthor = {
name: string;
@@ -54,12 +55,18 @@ export async function load_eht_file(
return JSON.parse(s);
}
export function update_database_tag(db: EhDb, f: EHTTextFile) {
export async function update_database_tag(
db: EhDb,
f: EHTTextFile,
signal: AbortSignal,
) {
await Deno.writeTextFile("./utt.lock", "", { create: true, signal });
for (const d of f.data) {
Object.getOwnPropertyNames(d.data).forEach((name) => {
await asyncForEach(Object.getOwnPropertyNames(d.data), async (name) => {
const tag = `${d.namespace}:${name}`;
const t = d.data[name];
db.update_tags(tag, t.name, t.intro);
await Deno.readTextFile("./utt.lock", { signal });
});
}
}

View File

@@ -3,7 +3,7 @@ import { load_settings } from "./config.ts";
import { check_file_permissions } from "./permissons.ts";
import { AlreadyClosedError, TaskManager } from "./task_manager.ts";
import { ParsedUrl, parseUrl, UrlType } from "./url.ts";
import { sure_dir } from "./utils.ts";
import { sure_dir, try_remove_sync } from "./utils.ts";
import { EhDb } from "./db.ts";
import { load_eht_file, update_database_tag } from "./eh_translation.ts";
import { get_abort_signal } from "./signal_handler.ts";
@@ -94,10 +94,11 @@ async function update_tag_translation() {
args._.length > 1 ? args._[1].toString() : undefined,
signal,
);
update_database_tag(db, f);
await update_database_tag(db, f, signal);
} catch (e) {
if (!signal.aborted) throw e;
} finally {
try_remove_sync("utt.lock");
db.close();
}
}

View File

@@ -32,7 +32,6 @@ export function add_exit_handler(m: TaskManager) {
export function get_abort_signal(callback?: () => void): AbortSignal {
const a = new AbortController();
const handler = () => {
console.log("Aborted.");
a.abort();
if (callback) callback();
};