Skip to content

Commit 3850af2

Browse files
committed
♻️ 增强后台脚本任务检查,减少错误 #714
1 parent ec56278 commit 3850af2

5 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/app/repo/repo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,12 @@ function deleteStorage(key: string) {
128128
});
129129
}
130130
function deletesStorage(keys: string[]) {
131-
return new Promise<void>((resolve, reject) => {
131+
return new Promise<void>((resolve) => {
132132
chrome.storage.local.remove(keys, () => {
133133
const lastError = chrome.runtime.lastError;
134134
if (lastError) {
135135
console.error("chrome.runtime.lastError in chrome.storage.local.remove:", lastError);
136136
// 无视storage API错误,继续执行
137-
reject();
138137
}
139138
resolve();
140139
});

src/app/service/offscreen/script.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { type MessageQueue } from "@Packages/message/message_queue";
44
import { type WindowMessage } from "@Packages/message/window_message";
55
import { ResourceClient, ScriptClient, ValueClient } from "../service_worker/client";
66
import type { ScriptRunResource } from "@App/app/repo/scripts";
7-
import { SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL } from "@App/app/repo/scripts";
7+
import {
8+
SCRIPT_STATUS_ENABLE,
9+
SCRIPT_TYPE_BACKGROUND,
10+
SCRIPT_TYPE_CRONTAB,
11+
SCRIPT_TYPE_NORMAL,
12+
} from "@App/app/repo/scripts";
813
import { disableScript, enableScript, runScript, stopScript } from "../sandbox/client";
914
import { type Group } from "@Packages/message/server";
1015
import type { MessageSend } from "@Packages/message/types";
@@ -65,8 +70,11 @@ export class ScriptService {
6570
}
6671
});
6772
this.messageQueue.subscribe<TDeleteScript[]>("deleteScripts", async (data) => {
68-
for (const { uuid } of data) {
69-
await disableScript(this.windowMessage, uuid);
73+
for (const { uuid, type } of data) {
74+
// 只发送后台脚本和定时脚本
75+
if (type === SCRIPT_TYPE_BACKGROUND || type === SCRIPT_TYPE_CRONTAB) {
76+
await disableScript(this.windowMessage, uuid);
77+
}
7078
}
7179
});
7280

src/app/service/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type TInstallScriptParams = {
1515

1616
export type TInstallScript = { script: TInstallScriptParams; update: boolean; upsertBy?: InstallSource };
1717

18-
export type TDeleteScript = { uuid: string; storageName: string };
18+
export type TDeleteScript = { uuid: string; storageName: string; type: SCRIPT_TYPE };
1919

2020
export type TSortedScript = { uuid: string; sort: number };
2121

src/app/service/sandbox/runtime.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,21 @@ export class Runtime {
102102
}
103103
}
104104

105-
disableScript(uuid: string) {
105+
async disableScript(uuid: string) {
106106
// 关闭脚本
107107
// 停止定时任务
108-
this.stopCronJob(uuid);
108+
// 检查是否有定时器
109+
if (this.cronJob.has(uuid)) {
110+
this.stopCronJob(uuid);
111+
}
109112
// 移除重试队列
110113
this.removeRetryList(uuid);
111-
// 发送运行状态变更
112-
proxyUpdateRunStatus(this.windowMessage, { uuid, runStatus: SCRIPT_RUN_STATUS_COMPLETE });
114+
if (!this.execScripts.has(uuid)) {
115+
// 没有在运行
116+
return false;
117+
}
113118
// 停止脚本运行
114-
return this.stopScript(uuid);
119+
return await this.stopScript(uuid);
115120
}
116121

117122
// 执行脚本

src/app/service/service_worker/script.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export class ScriptService {
284284
.then(async () => {
285285
await this.scriptCodeDAO.delete(uuid);
286286
logger.info("delete success");
287-
const data = [{ uuid, storageName }];
287+
const data = [{ uuid, storageName, type: script.type }];
288288
this.mq.publish<TDeleteScript[]>("deleteScripts", data);
289289
return true;
290290
})
@@ -296,28 +296,20 @@ export class ScriptService {
296296

297297
async deleteScripts(uuids: string[]) {
298298
const logger = this.logger.with({ uuids });
299-
const scripts = await this.scriptDAO.gets(uuids);
300-
const uuids2: string[] = [];
301-
const storageNames: string[] = [];
302-
for (let i = 0, l = uuids.length; i < l; i++) {
303-
const script = scripts[i];
304-
if (script && script.uuid && script.uuid === uuids[i]) {
305-
uuids2.push(script.uuid);
306-
storageNames.push(getStorageName(script));
307-
}
308-
}
309-
if (!uuids2.length) {
299+
const scripts = (await this.scriptDAO.gets(uuids)).filter((s) => !!s);
300+
if (!scripts.length) {
310301
logger.error("scripts not found");
311302
throw new Error("scripts not found");
312303
}
313304
return this.scriptDAO
314-
.deletes(uuids2)
305+
.deletes(uuids)
315306
.then(async () => {
316-
await this.scriptCodeDAO.deletes(uuids2);
307+
await this.scriptCodeDAO.deletes(uuids);
317308
logger.info("delete success");
318-
const data = uuids2.map((uuid, i) => ({
319-
uuid,
320-
storageName: storageNames[i],
309+
const data = scripts.map((script) => ({
310+
uuid: script.uuid,
311+
storageName: getStorageName(script),
312+
type: script.type,
321313
}));
322314
this.mq.publish<TDeleteScript[]>("deleteScripts", data);
323315
return true;

0 commit comments

Comments
 (0)