Skip to content

Commit

Permalink
feat: Node.js 版本体支持定时任务, 环境变量 SUB_STORE_BACKEND_CRON
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jan 14, 2024
1 parent 89931c0 commit 5cbcf4f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.159",
"version": "2.14.160",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand All @@ -17,6 +17,7 @@
"automerge": "1.0.1-preview.7",
"body-parser": "^1.19.0",
"connect-history-api-fallback": "^2.0.0",
"cron": "^3.1.6",
"express": "^4.17.1",
"http-proxy-middleware": "^2.0.6",
"js-base64": "^3.7.2",
Expand Down
25 changes: 25 additions & 0 deletions backend/pnpm-lock.yaml

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

23 changes: 23 additions & 0 deletions backend/src/restful/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from '@/vendor/express';
import $ from '@/core/app';
import migrate from '@/utils/migration';
import download from '@/utils/download';
import { syncArtifacts } from '@/restful/sync';

import registerSubscriptionRoutes from './subscriptions';
import registerCollectionRoutes from './collections';
Expand Down Expand Up @@ -41,6 +42,28 @@ export default function serve() {
$app.start();

if ($.env.isNode) {
const backend_cron = eval('process.env.SUB_STORE_BACKEND_CRON');
if (backend_cron) {
$.info(`[CRON] ${backend_cron} enabled`);
const { CronJob } = eval(`require("cron")`);
new CronJob(
backend_cron,
async function () {
try {
$.info(`[CRON] ${backend_cron} started`);
await syncArtifacts();
$.info(`[CRON] ${backend_cron} finished`);
} catch (e) {
$.error(
`[CRON] ${backend_cron} error: ${e.message ?? e}`,
);
}
}, // onTick
null, // onComplete
true, // start
// 'Asia/Shanghai' // timeZone
);
}
const path = eval(`require("path")`);
const fs = eval(`require("fs")`);
const data_url = eval('process.env.SUB_STORE_DATA_URL');
Expand Down
13 changes: 11 additions & 2 deletions backend/src/restful/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ async function produceArtifact({
}
}

async function syncAllArtifacts(_, res) {
async function syncArtifacts() {
$.info('开始同步所有远程配置...');
const allArtifacts = $.read(ARTIFACTS_KEY);
const files = {};
Expand Down Expand Up @@ -480,6 +480,15 @@ async function syncAllArtifacts(_, res) {

$.write(allArtifacts, ARTIFACTS_KEY);
$.info('全部订阅同步成功!');
} catch (e) {
$.error(`同步订阅失败,原因:${e.message ?? e}`);
throw e;
}
}
async function syncAllArtifacts(_, res) {
$.info('开始同步所有远程配置...');
try {
await syncArtifacts();
success(res);
} catch (err) {
failed(
Expand Down Expand Up @@ -553,4 +562,4 @@ async function syncArtifact(req, res) {
}
}

export { produceArtifact };
export { produceArtifact, syncArtifacts };

0 comments on commit 5cbcf4f

Please sign in to comment.