修复调试脚本日志

This commit is contained in:
whyour 2022-12-06 00:22:05 +08:00
parent 310ce55b09
commit 3b389259c1
2 changed files with 13 additions and 14 deletions

View File

@ -42,15 +42,21 @@ export default class ScheduleService {
constructor(@Inject('logger') private logger: winston.Logger) {}
async runTask(command: string, callbacks: TaskCallbacks = {}) {
async runTask(
command: string,
callbacks: TaskCallbacks = {},
completionTime: 'start' | 'end' = 'end',
) {
return new Promise(async (resolve, reject) => {
const startTime = dayjs();
await callbacks.onBefore?.(startTime);
const cp = spawn(command, { shell: '/bin/bash' });
try {
const startTime = dayjs();
await callbacks.onBefore?.(startTime);
const cp = spawn(command, { shell: '/bin/bash' });
// TODO:
callbacks.onStart?.(cp, startTime);
completionTime === 'start' && resolve(cp.pid);
cp.stdout.on('data', async (data) => {
await callbacks.onLog?.(data.toString());
@ -100,7 +106,6 @@ export default class ScheduleService {
);
await callbacks.onError?.(JSON.stringify(error));
}
resolve(cp.pid);
});
}

View File

@ -43,9 +43,10 @@ export default class ScriptService {
public async runScript(filePath: string) {
const relativePath = path.relative(config.scriptPath, filePath);
const command = `task -l ${relativePath} now`;
const pid = this.scheduleService.runTask(
const pid = await this.scheduleService.runTask(
command,
this.taskCallbacks(filePath),
'start',
);
return { code: 200, data: pid };
@ -61,13 +62,6 @@ export default class ScriptService {
await killTask(pid);
} catch (error) {}
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: `${str}\n## 执行结束... ${new Date()
.toLocaleString('zh', { hour12: false })
.replace(' 24:', ' 00:')}${LOG_END_SYMBOL}`,
});
return { code: 200 };
}
}