修复删除JavaScript进程,修改依赖重新安装逻辑

This commit is contained in:
whyour 2021-11-27 13:41:38 +08:00
parent cd9ba084ae
commit 6e0523c6d7
3 changed files with 110 additions and 100 deletions

View File

@ -22,14 +22,14 @@ export default async () => {
); );
// 初始化时安装所有处于安装中,安装成功,安装失败的依赖 // 初始化时安装所有处于安装中,安装成功,安装失败的依赖
dependenceDb.find({ status: { $in: [0, 1, 2] } }).exec((err, docs) => { dependenceDb.find({ status: { $in: [0, 1, 2] } }).exec(async (err, docs) => {
const groups = _.groupBy(docs, 'type'); const groups = _.groupBy(docs, 'type');
for (const key in groups) { for (const key in groups) {
if (Object.prototype.hasOwnProperty.call(groups, key)) { if (Object.prototype.hasOwnProperty.call(groups, key)) {
const group = groups[key]; const group = groups[key];
const depIds = group.map((x) => x._id); const depIds = group.map((x) => x._id);
for (const dep of depIds) { for (const dep of depIds) {
dependenceService.reInstall([dep]); await dependenceService.reInstall([dep]);
} }
} }
} }

View File

@ -245,16 +245,20 @@ export default class CronService {
} else { } else {
return; return;
} }
const pids = pid.match(/\(\d+/g); let pids = pid.match(/\(\d+/g);
const killLogs = []; const killLogs = [];
for (const id of pids) { if (pids && pids.length > 0) {
const c = `kill -9 ${id.slice(1)}`; // node 执行脚本时还会有10个子进程但是ps -ef中不存在所以截取前三个
const { stdout, stderr } = await execAsync(c); pids = pids.slice(0, 3);
if (stderr) { for (const id of pids) {
killLogs.push(stderr); const c = `kill -9 ${id.slice(1)}`;
} const { stdout, stderr } = await execAsync(c);
if (stdout) { if (stderr) {
killLogs.push(stdout); killLogs.push(stderr);
}
if (stdout) {
killLogs.push(stdout);
}
} }
} }
return killLogs.length > 0 ? JSON.stringify(killLogs) : ''; return killLogs.length > 0 ? JSON.stringify(killLogs) : '';

View File

@ -131,7 +131,7 @@ export default class DependenceService {
{ $set: { status: DependenceStatus.installing, log: [] } }, { $set: { status: DependenceStatus.installing, log: [] } },
{ multi: true, returnUpdatedDocs: true }, { multi: true, returnUpdatedDocs: true },
async (err, num, docs: Dependence[]) => { async (err, num, docs: Dependence[]) => {
this.installOrUninstallDependencies(docs); await this.installOrUninstallDependencies(docs);
resolve(docs); resolve(docs);
}, },
); );
@ -178,103 +178,109 @@ export default class DependenceService {
dependencies: Dependence[], dependencies: Dependence[],
isInstall: boolean = true, isInstall: boolean = true,
) { ) {
if (dependencies.length === 0) { return new Promise((resolve) => {
return; if (dependencies.length === 0) {
} resolve(null);
const depNames = dependencies.map((x) => x.name).join(' '); return;
const depRunCommand = ( }
isInstall const depNames = dependencies.map((x) => x.name).join(' ');
? InstallDependenceCommandTypes const depRunCommand = (
: unInstallDependenceCommandTypes isInstall
)[dependencies[0].type as any]; ? InstallDependenceCommandTypes
const actionText = isInstall ? '安装' : '删除'; : unInstallDependenceCommandTypes
const depIds = dependencies.map((x) => x._id) as string[]; )[dependencies[0].type as any];
const cp = spawn(`${depRunCommand} ${depNames}`, { shell: '/bin/bash' }); const actionText = isInstall ? '安装' : '删除';
const startTime = Date.now(); const depIds = dependencies.map((x) => x._id) as string[];
this.sockService.sendMessage({ const cp = spawn(`${depRunCommand} ${depNames}`, { shell: '/bin/bash' });
type: 'installDependence', const startTime = Date.now();
message: `开始${actionText}依赖 ${depNames},开始时间 ${new Date(
startTime,
).toLocaleString()}`,
references: depIds,
});
this.updateLog(
depIds,
`开始${actionText}依赖 ${depNames},开始时间 ${new Date(
startTime,
).toLocaleString()}\n`,
);
cp.stdout.on('data', (data) => {
this.sockService.sendMessage({ this.sockService.sendMessage({
type: 'installDependence', type: 'installDependence',
message: data.toString(), message: `开始${actionText}依赖 ${depNames},开始时间 ${new Date(
references: depIds, startTime,
}); ).toLocaleString()}`,
this.updateLog(depIds, data.toString());
});
cp.stderr.on('data', (data) => {
this.sockService.sendMessage({
type: 'installDependence',
message: data.toString(),
references: depIds,
});
this.updateLog(depIds, data.toString());
});
cp.on('error', (err) => {
this.sockService.sendMessage({
type: 'installDependence',
message: JSON.stringify(err),
references: depIds,
});
this.updateLog(depIds, JSON.stringify(err));
});
cp.on('close', (code) => {
const endTime = Date.now();
const isSucceed = code === 0;
const resultText = isSucceed ? '成功' : '失败';
this.sockService.sendMessage({
type: 'installDependence',
message: `依赖${actionText}${resultText},结束时间 ${new Date(
endTime,
).toLocaleString()} ${(endTime - startTime) / 1000} `,
references: depIds, references: depIds,
}); });
this.updateLog( this.updateLog(
depIds, depIds,
`依赖${actionText}${resultText},结束时间 ${new Date( `开始${actionText}依赖 ${depNames},开始时间 ${new Date(
endTime, startTime,
).toLocaleString()} ${(endTime - startTime) / 1000} `, ).toLocaleString()}\n`,
); );
cp.stdout.on('data', (data) => {
this.sockService.sendMessage({
type: 'installDependence',
message: data.toString(),
references: depIds,
});
this.updateLog(depIds, data.toString());
});
let status = null; cp.stderr.on('data', (data) => {
if (isSucceed) { this.sockService.sendMessage({
status = isInstall type: 'installDependence',
? DependenceStatus.installed message: data.toString(),
: DependenceStatus.removed; references: depIds,
} else { });
status = isInstall this.updateLog(depIds, data.toString());
? DependenceStatus.installFailed });
: DependenceStatus.removeFailed;
}
this.dependenceDb.update(
{ _id: { $in: depIds } },
{
$set: { status },
$unset: { pid: true },
},
{ multi: true },
);
// 如果删除依赖成功3秒后删除数据库记录 cp.on('error', (err) => {
if (isSucceed && !isInstall) { this.sockService.sendMessage({
setTimeout(() => { type: 'installDependence',
this.removeDb(depIds); message: JSON.stringify(err),
}, 5000); references: depIds,
} });
this.updateLog(depIds, JSON.stringify(err));
resolve(null);
});
cp.on('close', (code) => {
const endTime = Date.now();
const isSucceed = code === 0;
const resultText = isSucceed ? '成功' : '失败';
this.sockService.sendMessage({
type: 'installDependence',
message: `依赖${actionText}${resultText},结束时间 ${new Date(
endTime,
).toLocaleString()} ${(endTime - startTime) / 1000} `,
references: depIds,
});
this.updateLog(
depIds,
`依赖${actionText}${resultText},结束时间 ${new Date(
endTime,
).toLocaleString()} ${(endTime - startTime) / 1000} `,
);
let status = null;
if (isSucceed) {
status = isInstall
? DependenceStatus.installed
: DependenceStatus.removed;
} else {
status = isInstall
? DependenceStatus.installFailed
: DependenceStatus.removeFailed;
}
this.dependenceDb.update(
{ _id: { $in: depIds } },
{
$set: { status },
$unset: { pid: true },
},
{ multi: true },
);
// 如果删除依赖成功3秒后删除数据库记录
if (isSucceed && !isInstall) {
setTimeout(() => {
this.removeDb(depIds);
}, 5000);
}
resolve(null);
});
}); });
} }
} }