Skip to content

Commit

Permalink
fix: throw on timeout
Browse files Browse the repository at this point in the history
fix: #147
  • Loading branch information
northword committed Feb 17, 2024
1 parent 2fb44f0 commit 7a6c6da
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- 批处理时,每个条目最长持续9秒,超时的抛出 timeout 错误。close: [#147](https://github.com/northword/zotero-format-metadata/issues/147)
- 批处理时,错误的条目自动添加 “linter/error” 标签,重新执行正常退出的自动清除。close: [#147](https://github.com/northword/zotero-format-metadata/issues/147)

## [1.12.0] - 2024-02-01

### Added
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"packageManager": "pnpm@8.14.0",
"dependencies": {
"iso-locales": "1.1.1",
"p-queue": "^8.0.1",
"tinyld": "1.3.4",
"zotero-plugin-toolkit": "2.3.17"
},
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 64 additions & 9 deletions src/modules/rules-runner.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { config } from "../../package.json";
import { getString } from "../utils/locale";
import { logError } from "../utils/logger";
import { waitUtilAsync } from "../utils/wait";
import { timeoutPromise, waitUtilAsync } from "../utils/wait";
import { RuleBase } from "./rules/rule-base";
import PQueue from "p-queue";

export class LintRunner {
items: Zotero.Item[];
rules: RuleBase<any>[];
popWin: any;
// queue;
constructor(items: Zotero.Item[], rules: RuleBase<any> | RuleBase<any>[]) {
this.items = items;
this.rules = Array.isArray(rules) ? rules.flat() : [rules];
// this.queue = new PQueue({ concurrency: 10, autoStart: false, timeout: 9000, throwOnTimeout: true });
}

async run(item: Zotero.Item) {
Expand All @@ -19,6 +22,7 @@ export class LintRunner {
item = await rule.apply(item);
}
await item.saveTx();
// await Zotero.Promise.delay(1000);
}

async runInBatch(): Promise<void> {
Expand Down Expand Up @@ -70,8 +74,12 @@ export class LintRunner {

for (const item of this.items) {
if (!state) break;
await this.run(item)
await timeoutPromise(this.run(item), 9000)
.then(() => {
if (item.getTags().includes({ tag: "linter/error", type: 1 })) {
item.removeTag("linter/error");
item.saveTx();
}
current++;
progress.changeLine({
text: `[${current}/${total}] ${getString("info-batch-running")}`,
Expand All @@ -85,18 +93,65 @@ export class LintRunner {
text: `${getString("info-batch-has-error")}, ${err}`,
});
logError(err, item);
item.setTags(["linter/error"]);
item.saveTx();
errNum++;
});
}

progress.changeLine({
// type: "default",
text: `[✔️${current} ${errNum ? ", ❌" + errNum : ""}] ${getString("info-batch-finish")}`,
progress: 100,
idx: 0,
});
progress.startCloseTimer(5000);
progress
.changeLine({
// type: "default",
text: `[✔️${current} ${errNum ? ", ❌" + errNum : ""}] ${getString("info-batch-finish")}`,
progress: 100,
idx: 0,
})
.changeLine({ text: "Finished", idx: 1 })
.startCloseTimer(5000);
const endTime = new Date();
ztoolkit.log(`[Runner] The batch tasks done in ${(endTime.getTime() - startTime.getTime()) / 1000}s`);

// const tasks = this.items.map((item) => {
// return async () => {
// if (!state) {
// this.queue.clear();
// }
// await this.run(item);
// };
// });

// this.queue.addAll(tasks);

// this.queue
// .on("completed", (result) => {
// current++;
// progress.changeLine({
// text: `[${current}/${total}] ${getString("info-batch-running")}`,
// progress: (current / total) * 100,
// idx: 0,
// });
// })
// .on("error", (error) => {
// progress.createLine({
// type: "fail",
// text: `${getString("info-batch-has-error")}, ${error}`,
// });
// logError(error);
// errNum++;
// })
// .on("idle", () => {
// progress
// .changeLine({
// // type: "default",
// text: `[✔️${current} ${errNum ? ", ❌" + errNum : ""}] ${getString("info-batch-finish")}`,
// progress: 100,
// idx: 0,
// })
// .changeLine({ text: "Finished", idx: 1 });
// progress.startCloseTimer(5000);
// const endTime = new Date();
// ztoolkit.log(`[Runner] The batch tasks done in ${(endTime.getTime() - startTime.getTime()) / 1000}s`);
// });
// this.queue.start();
}
}
15 changes: 15 additions & 0 deletions src/utils/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ export function waitUtilAsync(condition: () => boolean, interval = 100, timeout
}, interval);
});
}

export async function timeoutPromise<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
let timeoutId: NodeJS.Timeout;

const timeoutPromise = new Promise<T>((resolve, reject) => {
timeoutId = setTimeout(() => {
reject(new Error("Timeout"));
}, timeoutMs);
});

// Race between the original promise and the timeout promise
return Promise.race([promise, timeoutPromise]).finally(() => {
clearTimeout(timeoutId);
});
}

0 comments on commit 7a6c6da

Please sign in to comment.