修改退出进程逻辑

This commit is contained in:
whyour
2022-12-05 15:26:22 +08:00
parent 23fd595582
commit b95fb9cda4
10 changed files with 76 additions and 125 deletions
+15 -6
View File
@@ -7,6 +7,7 @@ import CronService from './cron';
import ScheduleService, { TaskCallbacks } from './schedule';
import config from '../config';
import { LOG_END_SYMBOL } from '../config/const';
import { getPid, killTask } from '../config/util';
@Service()
export default class ScriptService {
@@ -42,16 +43,24 @@ export default class ScriptService {
public async runScript(filePath: string) {
const relativePath = path.relative(config.scriptPath, filePath);
const command = `task -l ${relativePath} now`;
this.scheduleService.runTask(command, this.taskCallbacks(filePath));
const pid = this.scheduleService.runTask(
command,
this.taskCallbacks(filePath),
);
return { code: 200 };
return { code: 200, data: pid };
}
public async stopScript(filePath: string) {
const relativePath = path.relative(config.scriptPath, filePath);
const err = await this.cronService.killTask(`task -l ${relativePath} now`);
public async stopScript(filePath: string, pid: number) {
let str = '';
if (!pid) {
const relativePath = path.relative(config.scriptPath, filePath);
pid = await getPid(`task -l ${relativePath} now`);
}
try {
await killTask(pid);
} catch (error) {}
const str = err ? `\n${err}` : '';
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: `${str}\n## 执行结束... ${new Date()