mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 23:06:06 +08:00
测试kill任务进程
This commit is contained in:
parent
b86ee5674d
commit
5fd79173d6
|
@ -9,6 +9,7 @@ import cron_parser from 'cron-parser';
|
||||||
import { getFileContentByName } from '../config/util';
|
import { getFileContentByName } from '../config/util';
|
||||||
import PQueue from 'p-queue';
|
import PQueue from 'p-queue';
|
||||||
import { promises, existsSync } from 'fs';
|
import { promises, existsSync } from 'fs';
|
||||||
|
import { promisify } from 'util';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class CronService {
|
export default class CronService {
|
||||||
|
@ -164,32 +165,58 @@ export default class CronService {
|
||||||
|
|
||||||
public async stop(ids: string[]) {
|
public async stop(ids: string[]) {
|
||||||
return new Promise((resolve: any) => {
|
return new Promise((resolve: any) => {
|
||||||
this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => {
|
this.cronDb
|
||||||
for (const doc of docs) {
|
.find({ _id: { $in: ids } })
|
||||||
if (doc.pid) {
|
.exec(async (err, docs: Crontab[]) => {
|
||||||
try {
|
for (const doc of docs) {
|
||||||
process.kill(-doc.pid);
|
if (doc.pid) {
|
||||||
} catch (error) {
|
try {
|
||||||
this.logger.silly(error);
|
process.kill(-doc.pid);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.silly(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const err = await this.killTask(doc.command);
|
||||||
|
if (doc.log_path) {
|
||||||
|
const str = err ? `\n${err}` : '';
|
||||||
|
fs.appendFileSync(
|
||||||
|
`${doc.log_path}`,
|
||||||
|
`${str}\n## 执行结束... ${new Date().toLocaleString()} `,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (doc.log_path) {
|
this.cronDb.update(
|
||||||
fs.appendFileSync(
|
{ _id: { $in: ids } },
|
||||||
`${doc.log_path}`,
|
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
|
||||||
`\n## 执行结束... ${new Date().toLocaleString()} `,
|
{ multi: true },
|
||||||
);
|
);
|
||||||
}
|
this.queue.clear();
|
||||||
}
|
resolve();
|
||||||
this.cronDb.update(
|
});
|
||||||
{ _id: { $in: ids } },
|
|
||||||
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
|
|
||||||
{ multi: true },
|
|
||||||
);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async killTask(name: string) {
|
||||||
|
let taskCommond = `ps -ef | grep "${name}" | grep -v grep | awk '{print $1}'`;
|
||||||
|
const execAsync = promisify(exec);
|
||||||
|
try {
|
||||||
|
let pid = (await execAsync(taskCommond)).stdout;
|
||||||
|
if (pid) {
|
||||||
|
pid = (await execAsync(`pstree -p ${pid}`)).stdout;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const pids = pid.match(/\d+/g);
|
||||||
|
for (const id of pids) {
|
||||||
|
const c = `kill -9 ${id}`;
|
||||||
|
const { stdout, stderr } = await execAsync(c);
|
||||||
|
return stderr;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return JSON.stringify(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async runSingle(id: string): Promise<number> {
|
private async runSingle(id: string): Promise<number> {
|
||||||
return new Promise(async (resolve: any) => {
|
return new Promise(async (resolve: any) => {
|
||||||
const cron = await this.get(id);
|
const cron = await this.get(id);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user