1 | const Worker = require('./../class/Worker')
|
2 | const startWorkers = require('./../module/startWorkers')
|
3 | const getPack = require('./../module/getPack')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = function restart(config, command) {
|
16 | return new Promise((resolve, reject) => {
|
17 | if (global.isResurrectable) {
|
18 | throw new Error('no apps running')
|
19 | }
|
20 |
|
21 |
|
22 | const app = config.apps.find(app => app.name === command.app)
|
23 |
|
24 | if (!app) {
|
25 | throw new Error(`app "${command.app}" not found`)
|
26 | }
|
27 |
|
28 |
|
29 | const pack = getPack(app.dir, app.opt, app.env)
|
30 |
|
31 |
|
32 | const nameChanged = pack.name !== app.name
|
33 | if (nameChanged) {
|
34 |
|
35 | if (config.apps.some(app => app.name === pack.name)) {
|
36 | throw new Error(`new name "${pack.name}" already in use`)
|
37 | }
|
38 |
|
39 |
|
40 | config.apps.push(Object.assign({}, app, {
|
41 | name: pack.name
|
42 | }))
|
43 | }
|
44 |
|
45 |
|
46 | let timeout = (app.env.NODE_ENV === 'development') ? 0 : 30e3
|
47 |
|
48 | if (command.opt.timeout) {
|
49 | if (isNaN(+command.opt.timeout)) {
|
50 | timeout = null
|
51 | } else {
|
52 | timeout = +command.opt.timeout
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 | const workers = Worker.workerList.filter(worker => worker.name === command.app)
|
58 |
|
59 | startWorkers(config, app.dir, pack, app.args, app.env).then(data => {
|
60 |
|
61 | const killed = workers.map(worker => {
|
62 | if (worker.kill(command.opt.signal, timeout)) {
|
63 | return worker.once('exit')
|
64 | }
|
65 | })
|
66 |
|
67 | return Promise.all(killed).then(() => {
|
68 | if (nameChanged) {
|
69 |
|
70 | const oldIndex = config.apps.findIndex(app => app.name === command.app)
|
71 | config.apps.splice(oldIndex, 1)
|
72 | }
|
73 |
|
74 | data.killed = killed.length
|
75 | resolve(data)
|
76 | })
|
77 | }).catch(reject)
|
78 | })
|
79 | }
|