feat: Node.js 版本体支持定时任务, 环境变量 SUB_STORE_BACKEND_CRON

This commit is contained in:
xream
2024-01-14 18:45:31 +08:00
parent 89931c0032
commit 5cbcf4fce4
4 changed files with 61 additions and 3 deletions

View File

@@ -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';
@@ -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');

View File

@@ -441,7 +441,7 @@ async function produceArtifact({
}
}
async function syncAllArtifacts(_, res) {
async function syncArtifacts() {
$.info('开始同步所有远程配置...');
const allArtifacts = $.read(ARTIFACTS_KEY);
const files = {};
@@ -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(
@@ -553,4 +562,4 @@ async function syncArtifact(req, res) {
}
}
export { produceArtifact };
export { produceArtifact, syncArtifacts };