Skip to content

Commit 5007a34

Browse files
committed
feat: Node 环境支持为同步配置设置独立的 cron
1 parent 5c90301 commit 5007a34

7 files changed

Lines changed: 477 additions & 76 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.23.17",
3+
"version": "2.23.18",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"packageManager": "pnpm@11.0.9",

backend/src/restful/artifacts.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import {
2020
} from '@/restful/errors';
2121
import Gist from '@/utils/gist';
2222
import { archiveArtifact } from '@/utils/archive';
23+
import {
24+
normalizeArtifactCron,
25+
refreshArtifactCronJobs,
26+
} from '@/utils/artifact-cron';
2327

2428
const ARTIFACT_GIST_PLACEHOLDER_FILENAME = '.sub-store-placeholder';
2529
const ARTIFACT_GIST_PLACEHOLDER_CONTENT = [
@@ -109,6 +113,7 @@ async function restoreArtifacts(_, res) {
109113
}
110114
});
111115
$.write(allArtifacts, ARTIFACTS_KEY);
116+
refreshArtifactCronJobs();
112117
} catch (err) {
113118
$.error(`查找 Sub-Store Gist 时发生错误: ${err.message ?? err}`);
114119
throw err;
@@ -133,9 +138,15 @@ function getAllArtifacts(req, res) {
133138
}
134139

135140
function replaceArtifact(req, res) {
136-
const allArtifacts = req.body;
137-
$.write(allArtifacts, ARTIFACTS_KEY);
138-
success(res);
141+
try {
142+
const allArtifacts = req.body;
143+
allArtifacts.forEach(normalizeArtifactCron);
144+
$.write(allArtifacts, ARTIFACTS_KEY);
145+
refreshArtifactCronJobs();
146+
success(res);
147+
} catch (error) {
148+
failed(res, error);
149+
}
139150
}
140151

141152
async function getArtifact(req, res) {
@@ -188,8 +199,15 @@ function updateArtifact(req, res) {
188199
);
189200
return;
190201
}
202+
try {
203+
normalizeArtifactCron(newArtifact);
204+
} catch (error) {
205+
failed(res, error);
206+
return;
207+
}
191208
updateByName(allArtifacts, oldName, newArtifact);
192209
$.write(allArtifacts, ARTIFACTS_KEY);
210+
refreshArtifactCronJobs();
193211
success(res, newArtifact);
194212
} else {
195213
failed(
@@ -244,6 +262,7 @@ function createArtifactItem(artifact) {
244262
}
245263

246264
$.info(`正在创建远程配置:${artifact.name}`);
265+
normalizeArtifactCron(artifact);
247266
const allArtifacts = $.read(ARTIFACTS_KEY);
248267
if (findByName(allArtifacts, artifact.name)) {
249268
throw new RequestInvalidError(
@@ -253,6 +272,7 @@ function createArtifactItem(artifact) {
253272
}
254273
insertByPosition(allArtifacts, artifact, getCreateItemPosition());
255274
$.write(allArtifacts, ARTIFACTS_KEY);
275+
refreshArtifactCronJobs();
256276
return artifact;
257277
}
258278

@@ -299,6 +319,7 @@ async function deleteArtifactItem(name) {
299319
}
300320
deleteByName(allArtifacts, name);
301321
$.write(allArtifacts, ARTIFACTS_KEY);
322+
refreshArtifactCronJobs();
302323
return {
303324
artifact,
304325
remote,

backend/src/restful/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import express from '@/vendor/express';
44
import $ from '@/core/app';
55
import migrate from '@/utils/migration';
66
import download, { downloadFile } from '@/utils/download';
7-
import { syncArtifacts, produceArtifact } from '@/restful/sync';
7+
import {
8+
syncArtifacts,
9+
produceArtifact,
10+
syncArtifactItem,
11+
} from '@/restful/sync';
812
import { gistBackupAction } from '@/restful/miscs';
913
import { TOKENS_KEY, SETTINGS_KEY } from '@/constants';
14+
import { startArtifactCronJobs } from '@/utils/artifact-cron';
1015

1116
import registerSubscriptionRoutes from './subscriptions';
1217
import registerCollectionRoutes from './collections';
@@ -145,6 +150,8 @@ export default function serve() {
145150
$app.start();
146151

147152
if ($.env.isNode) {
153+
startArtifactCronJobs(syncArtifactItem);
154+
148155
// Deprecated: SUB_STORE_BACKEND_CRON, SUB_STORE_CRON
149156
const backend_sync_cron = eval(
150157
'process.env.SUB_STORE_BACKEND_SYNC_CRON',
@@ -158,7 +165,7 @@ export default function serve() {
158165
async function () {
159166
try {
160167
$.info(`[SYNC CRON] ${backend_sync_cron} started`);
161-
await syncArtifacts();
168+
await syncArtifacts({ skipCronArtifacts: true });
162169
$.info(`[SYNC CRON] ${backend_sync_cron} finished`);
163170
} catch (e) {
164171
$.error(

0 commit comments

Comments
 (0)