diff --git a/db.ts b/db.ts index cd58e3c..ff509c1 100644 --- a/db.ts +++ b/db.ts @@ -90,13 +90,15 @@ export class EhDb { flock_enabled: boolean = eval('typeof Deno.flock !== "undefined"'); file: Deno.FsFile | undefined; _exist_table: Set = new Set(); + #lock_file: string | undefined; readonly version = new SemVer("1.0.0-0"); constructor(base_path: string) { this.db = new DB(join(base_path, "data.db")); this.db.execute("PRAGMA main.locking_mode=EXCLUSIVE;"); if (!this._check_database()) this._create_table(); if (this.flock_enabled) { - this.file = Deno.openSync(join(base_path, "db.lock"), { + this.#lock_file = join(base_path, "db.lock"); + this.file = Deno.openSync(this.#lock_file, { create: true, write: true, }); @@ -200,7 +202,10 @@ export class EhDb { } close() { this.db.close(); - if (this.file) this.file.close(); + if (this.file) { + this.file.close(); + if (this.#lock_file) Deno.remove(this.#lock_file); + } } async commit() { while (1) { diff --git a/main.ts b/main.ts index 8f26a1c..261da06 100644 --- a/main.ts +++ b/main.ts @@ -45,22 +45,32 @@ if (!check_file_permissions(settings.base)) { } async function download() { const manager = new TaskManager(settings); - const urls: ParsedUrl[] = []; - for (const i of args._.slice(1)) { - const r = parseUrl(i.toString()); - if (r) urls.push(r); - } - for (const u of urls) { - if (u.type == UrlType.Gallery || u.type == UrlType.MPV) { - await manager.add_download_task(u.gid, u.token); + try { + const urls: ParsedUrl[] = []; + for (const i of args._.slice(1)) { + const r = parseUrl(i.toString()); + if (r) urls.push(r); } + for (const u of urls) { + if (u.type == UrlType.Gallery || u.type == UrlType.MPV) { + await manager.add_download_task(u.gid, u.token); + } + } + if (args.add_only) { + return; + } + await manager.run(); + } finally { + manager.close(); } - if (args.add_only) return; - await manager.run(); } async function run() { const manager = new TaskManager(settings); - await manager.run(); + try { + await manager.run(); + } finally { + manager.close(); + } } async function main() { await sure_dir(settings.base); diff --git a/task_manager.ts b/task_manager.ts index 9d3d91c..9a7bb55 100644 --- a/task_manager.ts +++ b/task_manager.ts @@ -67,6 +67,9 @@ export class TaskManager { this.running_tasks.delete(id); } } + close() { + this.db.close(); + } async run() { while (1) { await this.check_running_tasks();