修复调试脚本目录

This commit is contained in:
whyour 2022-06-05 21:25:09 +08:00
parent 73c15cb3bc
commit 0c169e3f6c
2 changed files with 17 additions and 9 deletions

View File

@ -218,7 +218,7 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
filename: Joi.string().required(), filename: Joi.string().required(),
content: Joi.string().optional(), content: Joi.string().optional().allow(''),
path: Joi.string().optional().allow(''), path: Joi.string().optional().allow(''),
}), }),
}), }),
@ -227,7 +227,7 @@ export default (app: Router) => {
try { try {
let { filename, content, path } = req.body; let { filename, content, path } = req.body;
const { name, ext } = parse(filename); const { name, ext } = parse(filename);
const filePath = join(path, `${name}.swap${ext}`); const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
fs.writeFileSync(filePath, content || '', { encoding: 'utf8' }); fs.writeFileSync(filePath, content || '', { encoding: 'utf8' });
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
@ -254,7 +254,7 @@ export default (app: Router) => {
try { try {
let { filename, content, path } = req.body; let { filename, content, path } = req.body;
const { name, ext } = parse(filename); const { name, ext } = parse(filename);
const filePath = join(path, `${name}.swap${ext}`); const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const result = await scriptService.stopScript(filePath); const result = await scriptService.stopScript(filePath);

View File

@ -1,9 +1,11 @@
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process'; import fs from 'fs';
import path from 'path';
import SockService from './sock'; import SockService from './sock';
import CronService from './cron'; import CronService from './cron';
import ScheduleService, { TaskCallbacks } from './schedule'; import ScheduleService, { TaskCallbacks } from './schedule';
import config from '../config';
@Service() @Service()
export default class ScriptService { export default class ScriptService {
@ -14,7 +16,7 @@ export default class ScriptService {
private scheduleService: ScheduleService, private scheduleService: ScheduleService,
) {} ) {}
private taskCallbacks(): TaskCallbacks { private taskCallbacks(filePath: string): TaskCallbacks {
return { return {
onEnd: async (cp, endTime, diff) => { onEnd: async (cp, endTime, diff) => {
this.sockService.sendMessage({ this.sockService.sendMessage({
@ -23,6 +25,9 @@ export default class ScriptService {
'YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm:ss',
)} ${diff} `, )} ${diff} `,
}); });
try {
fs.unlinkSync(filePath);
} catch (error) {}
}, },
onError: async (message: string) => { onError: async (message: string) => {
this.sockService.sendMessage({ this.sockService.sendMessage({
@ -40,14 +45,17 @@ export default class ScriptService {
} }
public async runScript(filePath: string) { public async runScript(filePath: string) {
const command = `task -l ${filePath} now`; const relativePath = path.relative(config.scriptPath, filePath);
this.scheduleService.runTask(command, this.taskCallbacks()); const command = `task -l ${relativePath} now`;
this.scheduleService.runTask(command, this.taskCallbacks(filePath));
return { code: 200 }; return { code: 200 };
} }
public async stopScript(path: string) { public async stopScript(filePath: string) {
const err = await this.cronService.killTask(`task -l ${path} now`); const relativePath = path.relative(config.scriptPath, filePath);
const err = await this.cronService.killTask(`task -l ${relativePath} now`);
const str = err ? `\n${err}` : ''; const str = err ? `\n${err}` : '';
this.sockService.sendMessage({ this.sockService.sendMessage({
type: 'manuallyRunScript', type: 'manuallyRunScript',