修改订阅执行逻辑

This commit is contained in:
whyour 2022-06-01 00:25:32 +08:00
parent 6116aa6d12
commit b160f914ad
2 changed files with 14 additions and 4 deletions

View File

@ -18,6 +18,7 @@ interface ScheduleTaskType {
} }
export interface TaskCallbacks { export interface TaskCallbacks {
onBefore?: (startTime: dayjs.Dayjs) => Promise<void>;
onStart?: ( onStart?: (
cp: ChildProcessWithoutNullStreams, cp: ChildProcessWithoutNullStreams,
startTime: dayjs.Dayjs, startTime: dayjs.Dayjs,
@ -45,6 +46,8 @@ export default class ScheduleService {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
const startTime = dayjs(); const startTime = dayjs();
await callbacks.onBefore?.(startTime);
const cp = spawn(command, { shell: '/bin/bash' }); const cp = spawn(command, { shell: '/bin/bash' });
// TODO: // TODO:

View File

@ -192,14 +192,13 @@ export default class SubscriptionService {
private taskCallbacks(doc: Subscription): TaskCallbacks { private taskCallbacks(doc: Subscription): TaskCallbacks {
return { return {
onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => { onBefore: async (startTime) => {
const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss'); const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss');
const logPath = `${doc.alias}/${logTime}.log`; const logPath = `${doc.alias}/${logTime}.log`;
await SubscriptionModel.update( await SubscriptionModel.update(
{ {
status: SubscriptionStatus.running, status: SubscriptionStatus.running,
log_path: logPath, log_path: logPath,
pid: cp.pid,
}, },
{ where: { id: doc.id } }, { where: { id: doc.id } },
); );
@ -220,9 +219,17 @@ export default class SubscriptionService {
(error.stderr && error.stderr.toString()) || JSON.stringify(error); (error.stderr && error.stderr.toString()) || JSON.stringify(error);
} }
if (beforeStr) { if (beforeStr) {
fs.appendFileSync(absolutePath, `${beforeStr}\n\n`); fs.appendFileSync(absolutePath, `${beforeStr}\n`);
} }
}, },
onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => {
await SubscriptionModel.update(
{
pid: cp.pid,
},
{ where: { id: doc.id } },
);
},
onEnd: async (cp, endTime, diff) => { onEnd: async (cp, endTime, diff) => {
const sub = await this.getDb({ id: doc.id }); const sub = await this.getDb({ id: doc.id });
const absolutePath = await this.handleLogPath(sub.log_path as string); const absolutePath = await this.handleLogPath(sub.log_path as string);
@ -231,7 +238,7 @@ export default class SubscriptionService {
let afterStr = ''; let afterStr = '';
try { try {
if (sub.sub_after) { if (sub.sub_after) {
fs.appendFileSync(absolutePath, `\n\n## 执行after命令...\n`); fs.appendFileSync(absolutePath, `\n\n## 执行after命令...\n\n`);
afterStr = await this.promiseExec(sub.sub_after); afterStr = await this.promiseExec(sub.sub_after);
} }
} catch (error: any) { } catch (error: any) {