fix(tasks): new terminate process implementation

This commit is contained in:
Guillaume Chau
2018-11-26 20:08:20 +01:00
parent f45af9528f
commit 2baddaa35e
4 changed files with 72 additions and 7 deletions

View File

@@ -76,3 +76,8 @@ exports.hasProjectGit = (cwd) => {
_gitProjects.set(cwd, result)
return result
}
// OS
exports.isWindows = process.platform === 'win32'
exports.isMacintosh = process.platform === 'darwin'
exports.isLinux = process.platform === 'linux'

View File

@@ -1,6 +1,4 @@
const util = require('util')
const execa = require('execa')
const terminate = util.promisify(require('terminate'))
const chalk = require('chalk')
// Subs
const channels = require('../channels')
@@ -15,6 +13,7 @@ const projects = require('./projects')
// Utils
const { log } = require('../util/logger')
const { notify } = require('../util/notification')
const { terminate } = require('../util/terminate')
const MAX_LOGS = 2000
const VIEW_ID = 'vue-project-tasks'
@@ -468,14 +467,21 @@ async function stop (id, context) {
if (task && task.status === 'running' && task.child) {
task._terminating = true
try {
await terminate(task.child.pid)
const { success, error } = await terminate(task.child, cwd.get())
if (success) {
updateOne({
id: task.id,
status: 'terminated'
}, context)
} else if (error) {
throw error
} else {
throw new Error('Unknown error')
}
} catch (e) {
console.log(chalk.red(`Can't terminate process ${task.child.pid}`))
console.error(e)
}
updateOne({
id: task.id,
status: 'terminated'
}, context)
}
return task
}

View File

@@ -0,0 +1,42 @@
const util = require('util')
const cp = require('child_process')
const path = require('path')
const {
isWindows,
isLinux,
isMacintosh
} = require('@vue/cli-shared-utils')
const execFile = util.promisify(cp.execFile)
const spawn = util.promisify(cp.spawn)
exports.terminate = async function (childProcess, cwd) {
if (isWindows) {
try {
let options = {
stdio: ['pipe', 'pipe', 'ignore']
}
if (cwd) {
options.cwd = cwd
}
await execFile('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options)
} catch (err) {
return { success: false, error: err }
}
} else if (isLinux || isMacintosh) {
try {
let cmd = path.resolve(__dirname, './terminate.sh')
let result = await spawn(cmd, [childProcess.pid.toString()], {
cwd
})
if (result.error) {
return { success: false, error: result.error }
}
} catch (err) {
return { success: false, error: err }
}
} else {
childProcess.kill('SIGKILL')
}
return { success: true }
}

View File

@@ -0,0 +1,12 @@
#!/bin/bash
terminateTree() {
for cpid in $(/usr/bin/pgrep -P $1); do
terminateTree $cpid
done
kill -9 $1 > /dev/null 2>&1
}
for pid in $*; do
terminateTree $pid
done