diff --git a/back/data/cron.ts b/back/data/cron.ts index ea2daa72..40c0d54d 100644 --- a/back/data/cron.ts +++ b/back/data/cron.ts @@ -7,6 +7,7 @@ export class Crontab { saved?: boolean; _id?: string; status?: CrontabStatus; + isSystem?: 1 | 0; constructor(options: Crontab) { this.name = options.name; @@ -17,11 +18,12 @@ export class Crontab { this.created = options.created; this.status = options.status || CrontabStatus.idle; this.timestamp = new Date().toString(); + this.isSystem = options.isSystem; } } export enum CrontabStatus { - 'idle', 'running', + 'idle', 'disabled', } diff --git a/back/loaders/index.ts b/back/loaders/index.ts index f3dca96a..72629368 100644 --- a/back/loaders/index.ts +++ b/back/loaders/index.ts @@ -1,6 +1,7 @@ import expressLoader from './express'; import dependencyInjectorLoader from './dependencyInjector'; import Logger from './logger'; +import initData from './initData'; export default async ({ expressApp }: { expressApp: any }) => { Logger.info('✌️ DB loaded and connected!'); @@ -12,4 +13,7 @@ export default async ({ expressApp }: { expressApp: any }) => { await expressLoader({ app: expressApp }); Logger.info('✌️ Express loaded'); + + await initData(); + Logger.info('✌️ init data loaded'); }; diff --git a/back/loaders/initData.ts b/back/loaders/initData.ts new file mode 100644 index 00000000..93db55b7 --- /dev/null +++ b/back/loaders/initData.ts @@ -0,0 +1,111 @@ +import { Container } from 'typedi'; +import { Crontab, CrontabStatus } from '../data/cron'; +import CronService from '../services/cron'; + +const initData = [ + { + name: '更新面板', + command: `sleep ${randomSchedule( + 60, + 1, + )} && git_pull >> $QL_DIR/log/git_pull.log 2>&1`, + schedule: `${randomSchedule(60, 1)} ${randomSchedule( + 24, + 7, + ).toString()} * * *`, + status: CrontabStatus.idle, + }, + { + name: '自定义仓库', + command: `sleep ${randomSchedule( + 60, + 1, + )} && diy whyour hundun "quanx/jx|quanx/jd" tokens >> $QL_DIR/log/diy_pull.log 2>&1`, + schedule: `${randomSchedule(60, 1)} ${randomSchedule( + 24, + 6, + ).toString()} * * *`, + status: CrontabStatus.idle, + }, + { + name: '自定义仓库', + command: `sleep ${randomSchedule( + 60, + 1, + )} && diy monk-coder dust "i-chenzhe|normal" >> $QL_DIR/log/diy_pull.log 2>&1`, + schedule: `${randomSchedule(60, 1)} ${randomSchedule( + 24, + 6, + ).toString()} * * *`, + status: CrontabStatus.idle, + }, + { + name: '互助码导出', + command: 'export_sharecodes', + schedule: '48 5 * * *', + status: CrontabStatus.idle, + }, + { + name: 'build面板', + command: 'rebuild >> ${QL_DIR}/log/rebuild.log 2>&1', + schedule: '30 7 */7 * *', + status: CrontabStatus.disabled, + }, + { + name: '删除日志', + command: 'rm_log >/dev/null 2>&1', + schedule: '30 7 */7 * *', + status: CrontabStatus.disabled, + }, + { + name: '重置密码', + command: 'js resetpwd', + schedule: '33 6 */7 * *', + status: CrontabStatus.disabled, + }, + { + name: '运行所以脚本(慎用)', + command: 'js runall', + schedule: '33 6 */7 * *', + status: CrontabStatus.disabled, + }, +]; + +export default async () => { + const cronService = Container.get(CronService); + const cronDb = cronService.getDb(); + + cronDb.count({}, async (err, count) => { + const data = initData.map((x) => { + const tab = new Crontab(x); + tab.created = new Date().valueOf(); + tab.saved = false; + if (tab.name === '更新面板') { + tab.isSystem = 1; + } else { + tab.isSystem = 0; + } + return tab; + }); + if (count === 0) { + cronDb.insert(data); + await cronService.autosave_crontab(); + } + }); +}; + +function randomSchedule(from: number, to: number) { + const result = []; + const arr = [...Array(from).keys()]; + let count = arr.length; + for (let i = 0; i < to; i++) { + const index = ~~(Math.random() * count) + i; + if (result.includes(arr[index])) { + continue; + } + result[i] = arr[index]; + arr[index] = arr[i]; + count--; + } + return result; +} diff --git a/back/services/cron.ts b/back/services/cron.ts index 2c67de42..8515e46c 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -18,6 +18,10 @@ export default class CronService { }); } + public getDb(): DataStore { + return this.cronDb; + } + public async create(payload: Crontab): Promise { const tab = new Crontab(payload); tab.created = new Date().valueOf(); diff --git a/shell/git_diy.sh b/shell/git_diy.sh index 83c0a22d..3f497953 100755 --- a/shell/git_diy.sh +++ b/shell/git_diy.sh @@ -61,7 +61,8 @@ addnewcron() { [ -z "${script_date}" ] && cron_min=$(rand 1 59) && cron_hour=$(rand 7 9) && script_date="${cron_min} ${cron_hour} * * *" local oldCron=$(grep -c -w "$croname" "$ListCronCurrent") if [[ oldCron -eq 0 ]]; then - add_cron_api "$script_date" "js $croname" "$croname" + local name=$(cat "$js" | grep -E "new Env\(" | perl -pe "s|(^.+)new Env\(\'*\"*(.+?)'*\"*\).+|\2|") + add_cron_api "$script_date" "js $croname" "$name" addname="${addname}\n${croname}" echo -e "添加了新的脚本${croname}." fi diff --git a/shell/git_pull.sh b/shell/git_pull.sh index 014673aa..a6d6d3d0 100755 --- a/shell/git_pull.sh +++ b/shell/git_pull.sh @@ -236,8 +236,9 @@ Add_Cron() { if [[ $Cron == jd_bean_sign ]]; then echo "4 0,9 * * * $ShellJs $Cron" >>$ListCronCurrent else - param=$(cat $ListCronRemote | grep -E "\/$Cron\." | perl -pe "s|(^.+)node */scripts/(j[drx]_\w+)\.js.+|\1\:$ShellJs \2:\2|") - add_cron_api "$param" + local name=$(cat "$ScriptsDir/$Cron.js" | grep -E "new Env\(" | perl -pe "s|(^.+)new Env\(\'*\"*(.+?)'*\"*\).+|\2|") + local param=$(cat $ListCronRemote | grep -E "\/$Cron\." | perl -pe "s|(^.+) node */scripts/(j[drx]_\w+)\.js.+|\1\:$ShellJs \2|") + add_cron_api "$param:$name" fi done @@ -278,7 +279,6 @@ echo -e "--------------------------------------------------------------\n" get_token Import_Conf -Random_Pull_Cron # 更新shell [ -f $ShellDir/package.json ] && PanelDependOld=$(cat $ShellDir/package.json) diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 6583df91..261d72f8 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -127,7 +127,7 @@ const Crontab = () => { request .get(`${config.apiPrefix}crons`) .then((data: any) => { - setValue(data.data); + setValue(data.data.sort((a: any, b: any) => a.status - b.status)); }) .finally(() => setLoading(false)); }; @@ -259,21 +259,25 @@ const Crontab = () => { }> 编辑 - - ) : ( - - ) - } - > - {record.status === CrontabStatus.disabled ? '启用' : '禁用'} - - }> - 删除 - + {record.isSystem === 0 && ( + <> + + ) : ( + + ) + } + > + {record.status === CrontabStatus.disabled ? '启用' : '禁用'} + + }> + 删除 + + + )} } >