Skip to content

Commit 2baddaa

Browse files
author
Guillaume Chau
committed
fix(tasks): new terminate process implementation
1 parent f45af95 commit 2baddaa

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

packages/@vue/cli-shared-utils/lib/env.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ exports.hasProjectGit = (cwd) => {
7676
_gitProjects.set(cwd, result)
7777
return result
7878
}
79+
80+
// OS
81+
exports.isWindows = process.platform === 'win32'
82+
exports.isMacintosh = process.platform === 'darwin'
83+
exports.isLinux = process.platform === 'linux'

packages/@vue/cli-ui/apollo-server/connectors/tasks.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const util = require('util')
21
const execa = require('execa')
3-
const terminate = util.promisify(require('terminate'))
42
const chalk = require('chalk')
53
// Subs
64
const channels = require('../channels')
@@ -15,6 +13,7 @@ const projects = require('./projects')
1513
// Utils
1614
const { log } = require('../util/logger')
1715
const { notify } = require('../util/notification')
16+
const { terminate } = require('../util/terminate')
1817

1918
const MAX_LOGS = 2000
2019
const VIEW_ID = 'vue-project-tasks'
@@ -468,14 +467,21 @@ async function stop (id, context) {
468467
if (task && task.status === 'running' && task.child) {
469468
task._terminating = true
470469
try {
471-
await terminate(task.child.pid)
470+
const { success, error } = await terminate(task.child, cwd.get())
471+
if (success) {
472+
updateOne({
473+
id: task.id,
474+
status: 'terminated'
475+
}, context)
476+
} else if (error) {
477+
throw error
478+
} else {
479+
throw new Error('Unknown error')
480+
}
472481
} catch (e) {
482+
console.log(chalk.red(`Can't terminate process ${task.child.pid}`))
473483
console.error(e)
474484
}
475-
updateOne({
476-
id: task.id,
477-
status: 'terminated'
478-
}, context)
479485
}
480486
return task
481487
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const util = require('util')
2+
const cp = require('child_process')
3+
const path = require('path')
4+
const {
5+
isWindows,
6+
isLinux,
7+
isMacintosh
8+
} = require('@vue/cli-shared-utils')
9+
10+
const execFile = util.promisify(cp.execFile)
11+
const spawn = util.promisify(cp.spawn)
12+
13+
exports.terminate = async function (childProcess, cwd) {
14+
if (isWindows) {
15+
try {
16+
let options = {
17+
stdio: ['pipe', 'pipe', 'ignore']
18+
}
19+
if (cwd) {
20+
options.cwd = cwd
21+
}
22+
await execFile('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options)
23+
} catch (err) {
24+
return { success: false, error: err }
25+
}
26+
} else if (isLinux || isMacintosh) {
27+
try {
28+
let cmd = path.resolve(__dirname, './terminate.sh')
29+
let result = await spawn(cmd, [childProcess.pid.toString()], {
30+
cwd
31+
})
32+
if (result.error) {
33+
return { success: false, error: result.error }
34+
}
35+
} catch (err) {
36+
return { success: false, error: err }
37+
}
38+
} else {
39+
childProcess.kill('SIGKILL')
40+
}
41+
return { success: true }
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
terminateTree() {
4+
for cpid in $(/usr/bin/pgrep -P $1); do
5+
terminateTree $cpid
6+
done
7+
kill -9 $1 > /dev/null 2>&1
8+
}
9+
10+
for pid in $*; do
11+
terminateTree $pid
12+
done

0 commit comments

Comments
 (0)