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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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