Make sure clean is worked

This commit is contained in:
2023-05-20 09:04:37 +08:00
parent e3fc38ff06
commit 81e8655d3b
3 changed files with 31 additions and 13 deletions

9
db.ts
View File

@@ -90,13 +90,15 @@ export class EhDb {
flock_enabled: boolean = eval('typeof Deno.flock !== "undefined"');
file: Deno.FsFile | undefined;
_exist_table: Set<string> = 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) {

32
main.ts
View File

@@ -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);

View File

@@ -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();