Skip to content

Commit b1618c3

Browse files
committed
feat: 支持使用环境变量 SUB_STORE_PRODUCE_CRON 在后台定时处理订阅, 格式为 0 */2 * * *,sub,a;0 */3 * * *,col,b
1 parent 1b4c046 commit b1618c3

3 files changed

Lines changed: 37 additions & 4 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.16.59",
3+
"version": "2.16.60",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/restful/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from '@/vendor/express';
22
import $ from '@/core/app';
33
import migrate from '@/utils/migration';
44
import download from '@/utils/download';
5-
import { syncArtifacts } from '@/restful/sync';
5+
import { syncArtifacts, produceArtifact } from '@/restful/sync';
66
import { gistBackupAction } from '@/restful/miscs';
77
import { TOKENS_KEY } from '@/constants';
88

@@ -75,6 +75,39 @@ export default function serve() {
7575
// 'Asia/Shanghai' // timeZone
7676
);
7777
}
78+
// 格式: 0 */2 * * *,sub,a;0 */3 * * *,col,b
79+
// 每 2 小时处理一次单条订阅 a, 每 3 小时处理一次组合订阅 b
80+
const produce_cron = eval('process.env.SUB_STORE_PRODUCE_CRON');
81+
if (produce_cron) {
82+
$.info(`[PRODUCE CRON] ${produce_cron} enabled`);
83+
const { CronJob } = eval(`require("cron")`);
84+
produce_cron.split(/\s*;\s*/).map((item) => {
85+
const [cron, type, name] = item.split(/\s*,\s*/);
86+
new CronJob(
87+
cron.trim(),
88+
async function () {
89+
try {
90+
$.info(
91+
`[PRODUCE CRON] ${type} ${name} ${cron} started`,
92+
);
93+
await produceArtifact({ type, name });
94+
$.info(
95+
`[PRODUCE CRON] ${type} ${name} ${cron} finished`,
96+
);
97+
} catch (e) {
98+
$.error(
99+
`[PRODUCE CRON] ${type} ${name} ${cron} error: ${
100+
e.message ?? e
101+
}`,
102+
);
103+
}
104+
}, // onTick
105+
null, // onComplete
106+
true, // start
107+
// 'Asia/Shanghai' // timeZone
108+
);
109+
});
110+
}
78111
const backend_download_cron = eval(
79112
'process.env.SUB_STORE_BACKEND_DOWNLOAD_CRON',
80113
);

backend/src/restful/sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function produceArtifact({
4343
}) {
4444
platform = platform || 'JSON';
4545

46-
if (type === 'subscription') {
46+
if (['subscription', 'sub'].includes(type)) {
4747
let sub;
4848
if (name) {
4949
const allSubs = $.read(SUBS_KEY);
@@ -190,7 +190,7 @@ async function produceArtifact({
190190
}
191191
// produce
192192
return ProxyUtils.produce(proxies, platform, produceType, produceOpts);
193-
} else if (type === 'collection') {
193+
} else if (['collection', 'col'].includes(type)) {
194194
const allSubs = $.read(SUBS_KEY);
195195
const allCols = $.read(COLLECTIONS_KEY);
196196
const collection = findByName(allCols, name);

0 commit comments

Comments
 (0)