mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
Fix task duplication: add single/multi-instance support with UI configuration and stop all running instances (#2837)
* Initial plan * Stop running tasks before starting new scheduled instance Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Add multi-instance support and fix stop to kill all running instances - Add allow_multiple_instances field to Crontab model (default: 0 for single instance) - Add validation for new field in commonCronSchema - Add getAllPids and killAllTasks utility functions - Update stop method to kill ALL running instances of a task - Update runCron to respect allow_multiple_instances config - Backward compatible: defaults to single instance mode Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Add UI support for allow_multiple_instances configuration - Add allow_multiple_instances field to ICrontab interface - Add instance mode selector in task creation/edit modal - Add translations for instance mode in Chinese and English - Default to single instance mode for backward compatibility Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Add allow_multiple_instances column migration and optimize db.ts - Add allow_multiple_instances column to Crontabs table migration - Refactor migration code to use data-driven approach - Replace 11 individual try-catch blocks with single loop - Improve code maintainability and readability Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
+21
-10
@@ -9,6 +9,7 @@ import {
|
||||
getFileContentByName,
|
||||
fileExist,
|
||||
killTask,
|
||||
killAllTasks,
|
||||
getUniqPath,
|
||||
safeJSONParse,
|
||||
isDemoEnv,
|
||||
@@ -28,7 +29,7 @@ import { logStreamManager } from '../shared/logStreamManager';
|
||||
|
||||
@Service()
|
||||
export default class CronService {
|
||||
constructor(@Inject('logger') private logger: winston.Logger) { }
|
||||
constructor(@Inject('logger') private logger: winston.Logger) {}
|
||||
|
||||
private isNodeCron(cron: Crontab) {
|
||||
const { schedule, extra_schedules } = cron;
|
||||
@@ -57,7 +58,9 @@ export default class CronService {
|
||||
}
|
||||
let uniqPath = await getUniqPath(command, `${id}`);
|
||||
if (log_name) {
|
||||
const normalizedLogName = log_name.startsWith('/') ? log_name : path.join(config.logPath, log_name);
|
||||
const normalizedLogName = log_name.startsWith('/')
|
||||
? log_name
|
||||
: path.join(config.logPath, log_name);
|
||||
if (normalizedLogName.startsWith(config.logPath)) {
|
||||
uniqPath = log_name;
|
||||
}
|
||||
@@ -162,7 +165,7 @@ export default class CronService {
|
||||
let cron;
|
||||
try {
|
||||
cron = await this.getDb({ id });
|
||||
} catch (err) { }
|
||||
} catch (err) {}
|
||||
if (!cron) {
|
||||
continue;
|
||||
}
|
||||
@@ -462,12 +465,17 @@ export default class CronService {
|
||||
public async stop(ids: number[]) {
|
||||
const docs = await CrontabModel.findAll({ where: { id: ids } });
|
||||
for (const doc of docs) {
|
||||
if (doc.pid) {
|
||||
try {
|
||||
await killTask(doc.pid);
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
// Kill all running instances of this task
|
||||
try {
|
||||
const command = this.makeCommand(doc);
|
||||
await killAllTasks(command);
|
||||
this.logger.info(
|
||||
`[panel][停止所有运行中的任务实例] 任务ID: ${doc.id}, 命令: ${command}`,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`[panel][停止任务失败] 任务ID: ${doc.id}, 错误: ${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -498,7 +506,10 @@ export default class CronService {
|
||||
|
||||
let { id, command, log_name } = cron;
|
||||
|
||||
const uniqPath = log_name === '/dev/null' ? (await getUniqPath(command, `${id}`)) : log_name;
|
||||
const uniqPath =
|
||||
log_name === '/dev/null'
|
||||
? await getUniqPath(command, `${id}`)
|
||||
: log_name;
|
||||
const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss-SSS');
|
||||
const logDirPath = path.resolve(config.logPath, `${uniqPath}`);
|
||||
await fs.mkdir(logDirPath, { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user