修复有可能手动运行任务无日志

This commit is contained in:
whyour 2024-05-25 00:28:55 +08:00
parent 8dd379b6b9
commit b309ac7613
7 changed files with 30 additions and 17 deletions

View File

@ -407,7 +407,7 @@ export default class CronService {
}
private async runSingle(cronId: number): Promise<number | void> {
return taskLimit.runWithCronLimit(() => {
return taskLimit.manualRunWithCronLimit(() => {
return new Promise(async (resolve: any) => {
const cron = await this.getDb({ id: cronId });
const params = {
@ -434,9 +434,8 @@ export default class CronService {
}
const logPath = `${uniqPath}/${logTime}.log`;
const absolutePath = path.resolve(config.logPath, `${logPath}`);
const cp = spawn(
`real_log_path=${logPath} no_delay=true ${this.makeCommand(cron)}`,
`real_log_path=${logPath} real_time=true no_delay=true ${this.makeCommand(cron)}`,
{ shell: '/bin/bash' },
);
@ -444,11 +443,14 @@ export default class CronService {
{ status: CrontabStatus.running, pid: cp.pid, log_path: logPath },
{ where: { id } },
);
cp.stdout.on('data', async (data) => {
await fs.appendFile(absolutePath, data.toString());
});
cp.stderr.on('data', async (data) => {
await fs.appendFile(`${absolutePath}`, `${data.toString()}`);
await fs.appendFile(absolutePath, data.toString());
});
cp.on('error', async (err) => {
await fs.appendFile(`${absolutePath}`, `${JSON.stringify(err)}`);
await fs.appendFile(absolutePath, JSON.stringify(err));
});
cp.on('exit', async (code) => {

View File

@ -348,20 +348,13 @@ export default class SystemService {
}
}
public async run(
{ command, logPath }: { command: string; logPath: string },
callback: TaskCallbacks,
) {
public async run({ command }: { command: string }, callback: TaskCallbacks) {
if (!command.startsWith(TASK_COMMAND)) {
command = `${TASK_COMMAND} ${command}`;
}
this.scheduleService.runTask(
`real_log_path=${logPath} real_time=true ${command}`,
callback,
{
command,
},
);
this.scheduleService.runTask(`real_time=true ${command}`, callback, {
command,
});
}
public async stop({ command, pid }: { command: string; pid: number }) {
@ -388,7 +381,9 @@ export default class SystemService {
public async exportData(res: Response) {
try {
await promiseExec(`cd ${config.rootPath} && tar -zcvf ${config.dataTgzFile} data/`);
await promiseExec(
`cd ${config.rootPath} && tar -zcvf ${config.dataTgzFile} data/`,
);
res.download(config.dataTgzFile);
} catch (error: any) {
return res.send({ code: 400, message: error.message });

View File

@ -15,6 +15,9 @@ class TaskLimit {
private cronLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4),
});
private manualCronoLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4),
});
get cronLimitActiveCount() {
return this.cronLimit.pending;
@ -71,6 +74,7 @@ class TaskLimit {
public async setCustomLimit(limit?: number) {
if (limit) {
this.cronLimit.concurrency = limit;
this.manualCronoLimit.concurrency = limit;
return;
}
await SystemModel.sync();
@ -79,6 +83,7 @@ class TaskLimit {
});
if (doc?.info?.cronConcurrency) {
this.cronLimit.concurrency = doc.info.cronConcurrency;
this.manualCronoLimit.concurrency = doc.info.cronConcurrency;
}
}
@ -89,6 +94,13 @@ class TaskLimit {
return this.cronLimit.add(fn, options);
}
public async manualRunWithCronLimit<T>(
fn: () => Promise<T>,
options?: Partial<QueueAddOptions>,
): Promise<T | void> {
return this.manualCronoLimit.add(fn, options);
}
public runDependeny<T>(
dependency: Dependence,
fn: IDependencyFn<T>,

View File

@ -136,6 +136,7 @@ const Config = () => {
lineNumbersMinChars: 3,
folding: false,
glyphMargin: false,
accessibilitySupport: 'off'
}}
onMount={(editor) => {
editorRef.current = editor;

View File

@ -110,6 +110,7 @@ const CronDetailModal = ({
minimap: { enabled: false },
lineNumbersMinChars: 3,
glyphMargin: false,
accessibilitySupport: 'off'
}}
onMount={(editor, monaco) => {
editorRef.current = editor;

View File

@ -242,6 +242,7 @@ const EditModal = ({
minimap: { enabled: false },
lineNumbersMinChars: 3,
glyphMargin: false,
accessibilitySupport: 'off'
}}
onMount={(editor) => {
editorRef.current = editor;

View File

@ -643,6 +643,7 @@ const Script = () => {
fontSize: 12,
lineNumbersMinChars: 3,
glyphMargin: false,
accessibilitySupport: 'off'
}}
onMount={(editor) => {
editorRef.current = editor;