mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 06:46:09 +08:00
修复execSync卡死主进程
This commit is contained in:
parent
31ff0476a6
commit
3f29c1aa4e
|
@ -160,6 +160,18 @@ export default class SubscriptionService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async promiseExec(command: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
exec(
|
||||||
|
command,
|
||||||
|
{ maxBuffer: 200 * 1024 * 1024, encoding: 'utf8' },
|
||||||
|
(err, stdout, stderr) => {
|
||||||
|
resolve(stdout || stderr || JSON.stringify(err));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async handleLogPath(
|
private async handleLogPath(
|
||||||
logPath: string,
|
logPath: string,
|
||||||
data: string = '',
|
data: string = '',
|
||||||
|
@ -175,28 +187,8 @@ export default class SubscriptionService {
|
||||||
private taskCallbacks(doc: Subscription): TaskCallbacks {
|
private taskCallbacks(doc: Subscription): TaskCallbacks {
|
||||||
return {
|
return {
|
||||||
onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => {
|
onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => {
|
||||||
// 执行sub_before
|
|
||||||
let beforeStr = '';
|
|
||||||
try {
|
|
||||||
if (doc.sub_before) {
|
|
||||||
beforeStr = execSync(doc.sub_before).toString();
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
|
||||||
beforeStr =
|
|
||||||
(error.stderr && error.stderr.toString()) || JSON.stringify(error);
|
|
||||||
}
|
|
||||||
if (beforeStr) {
|
|
||||||
beforeStr += '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss');
|
const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss');
|
||||||
const logPath = `${doc.alias}/${logTime}.log`;
|
const logPath = `${doc.alias}/${logTime}.log`;
|
||||||
await this.handleLogPath(
|
|
||||||
logPath as string,
|
|
||||||
`${beforeStr}## 开始执行... ${startTime.format(
|
|
||||||
'YYYY-MM-DD HH:mm:ss',
|
|
||||||
)}\n`,
|
|
||||||
);
|
|
||||||
await SubscriptionModel.update(
|
await SubscriptionModel.update(
|
||||||
{
|
{
|
||||||
status: SubscriptionStatus.running,
|
status: SubscriptionStatus.running,
|
||||||
|
@ -205,14 +197,45 @@ export default class SubscriptionService {
|
||||||
},
|
},
|
||||||
{ where: { id: doc.id } },
|
{ where: { id: doc.id } },
|
||||||
);
|
);
|
||||||
|
const absolutePath = await this.handleLogPath(
|
||||||
|
logPath as string,
|
||||||
|
`## 开始执行... ${startTime.format('YYYY-MM-DD HH:mm:ss')}\n`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 执行sub_before
|
||||||
|
let beforeStr = '';
|
||||||
|
try {
|
||||||
|
if (doc.sub_before) {
|
||||||
|
fs.appendFileSync(absolutePath, `\n## 执行开始前命令... `);
|
||||||
|
beforeStr = await this.promiseExec(doc.sub_before);
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
beforeStr =
|
||||||
|
(error.stderr && error.stderr.toString()) || JSON.stringify(error);
|
||||||
|
}
|
||||||
|
if (beforeStr) {
|
||||||
|
fs.appendFileSync(absolutePath, `${beforeStr}\n\n`);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onEnd: async (cp, endTime, diff) => {
|
onEnd: async (cp, endTime, diff) => {
|
||||||
const sub = await this.getDb({ id: doc.id });
|
const sub = await this.getDb({ id: doc.id });
|
||||||
await SubscriptionModel.update(
|
|
||||||
{ status: SubscriptionStatus.idle, pid: undefined },
|
|
||||||
{ where: { id: sub.id } },
|
|
||||||
);
|
|
||||||
const absolutePath = await this.handleLogPath(sub.log_path as string);
|
const absolutePath = await this.handleLogPath(sub.log_path as string);
|
||||||
|
|
||||||
|
// 执行 sub_after
|
||||||
|
let afterStr = '';
|
||||||
|
try {
|
||||||
|
if (sub.sub_after) {
|
||||||
|
fs.appendFileSync(absolutePath, `\n\n## 执行结束后命令... `);
|
||||||
|
afterStr = await this.promiseExec(sub.sub_after);
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
afterStr =
|
||||||
|
(error.stderr && error.stderr.toString()) || JSON.stringify(error);
|
||||||
|
}
|
||||||
|
if (afterStr) {
|
||||||
|
fs.appendFileSync(absolutePath, `${afterStr}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
fs.appendFileSync(
|
fs.appendFileSync(
|
||||||
absolutePath,
|
absolutePath,
|
||||||
`\n## 执行结束... ${endTime.format(
|
`\n## 执行结束... ${endTime.format(
|
||||||
|
@ -220,21 +243,10 @@ export default class SubscriptionService {
|
||||||
)} 耗时 ${diff} 秒`,
|
)} 耗时 ${diff} 秒`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// 执行 sub_after
|
await SubscriptionModel.update(
|
||||||
let afterStr = '';
|
{ status: SubscriptionStatus.idle, pid: undefined },
|
||||||
try {
|
{ where: { id: sub.id } },
|
||||||
if (sub.sub_after) {
|
);
|
||||||
afterStr = execSync(sub.sub_after).toString();
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
|
||||||
afterStr =
|
|
||||||
(error.stderr && error.stderr.toString()) || JSON.stringify(error);
|
|
||||||
}
|
|
||||||
if (afterStr) {
|
|
||||||
afterStr = `\n\n${afterStr}`;
|
|
||||||
const absolutePath = await this.handleLogPath(sub.log_path as string);
|
|
||||||
fs.appendFileSync(absolutePath, afterStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sockService.sendMessage({
|
this.sockService.sendMessage({
|
||||||
type: 'runSubscriptionEnd',
|
type: 'runSubscriptionEnd',
|
||||||
|
|
|
@ -211,8 +211,8 @@ const SubscriptionModal = ({
|
||||||
dependences,
|
dependences,
|
||||||
branch,
|
branch,
|
||||||
extensions,
|
extensions,
|
||||||
|
alias: formatAlias(url, branch),
|
||||||
});
|
});
|
||||||
form.validateFields(['url']);
|
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user