From ab4be7f8b4196667a786438f09da1fad076e9bc1 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 23 May 2023 12:40:59 +0800 Subject: [PATCH] Fix utt can not handle Ctrl + C --- .gitignore | 1 + eh_translation.ts | 11 +++++++++-- main.ts | 5 +++-- signal_handler.ts | 1 - 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 11c55d8..de716d9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config.json test/ downloads/ +utt.lock diff --git a/eh_translation.ts b/eh_translation.ts index c34928b..8f8c3c5 100644 --- a/eh_translation.ts +++ b/eh_translation.ts @@ -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 }); }); } } diff --git a/main.ts b/main.ts index 1f9c18c..b59cf32 100644 --- a/main.ts +++ b/main.ts @@ -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(); } } diff --git a/signal_handler.ts b/signal_handler.ts index 88316c4..977ce43 100644 --- a/signal_handler.ts +++ b/signal_handler.ts @@ -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(); };