Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pm2 global #5286

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 75 additions & 34 deletions packages/cli/src/commands/deploy/baremetal.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const commands = (yargs, ssh) => {
passphrase: serverConfig.passphrase,
agent: serverConfig.agentForward && process.env.SSH_AUTH_SOCK,
agentForward: serverConfig.agentForward,
pm2Global: serverConfig.pm2Global,
}

tasks.push({
Expand Down Expand Up @@ -215,41 +216,81 @@ const commands = (yargs, ssh) => {
// start/restart monitoring processes
for (const process of serverConfig.processNames) {
if (yargs.firstRun) {
tasks.push({
title: `Starting ${process} process for the first time...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'start',
'ecosystem.config.js',
'--only',
process,
])
},
skip: () => !yargs.restart,
})
tasks.push({
title: `Saving ${process} state for future startup...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'save',
])
},
skip: () => !yargs.restart,
})
if (serverConfig.pm2Global) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than repeating these entire commands when only a couple of things change, what if just simplify it into setting the name of the command and the options first, then run the command once but using those vars for the settings? Something like:

// assume local install
let command = 'yarn'
let startOptions = ['pm2', 'start', 'ecosystem.config.js', '--only', process]
let restartOptions = ['pm2', 'restart', process]
let saveOptions = ['pm2', 'save']

if (serverConfig.pm2Global) {
  command = 'pm2'
  startOptions.shift()
  restartOptions.shift()
  saveOptions.shift()
}

tasks.push({
  title: `Starting ${process} process for the first time...`,
  task: async (_ctx, task) => {
    await sshExec(ssh, sshOptions, task, serverConfig.path, command, options)
  },
  skip: () => !yargs.restart,
})

// save...

// restart...

tasks.push({
title: `Starting ${process} process for the first time...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'pm2', [
'start',
'ecosystem.config.js',
'--only',
process,
])
},
skip: () => !yargs.restart,
})
} else {
tasks.push({
title: `Starting ${process} process for the first time...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'start',
'ecosystem.config.js',
'--only',
process,
])
},
skip: () => !yargs.restart,
})
}
if (serverConfig.pm2Global) {
tasks.push({
title: `Saving ${process} state for future startup...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'pm2', [
'save',
])
},
skip: () => !yargs.restart,
})
} else {
tasks.push({
title: `Saving ${process} state for future startup...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'save',
])
},
skip: () => !yargs.restart,
})
}
} else {
tasks.push({
title: `Restarting ${process} process...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'restart',
process,
])
},
skip: () => !yargs.restart,
})
if (serverConfig.pm2Global) {
tasks.push({
title: `Restarting ${process} process...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'pm2', [
'restart',
process,
])
},
skip: () => !yargs.restart,
})
} else {
tasks.push({
title: `Restarting ${process} process...`,
task: async (_ctx, task) => {
await sshExec(ssh, sshOptions, task, serverConfig.path, 'yarn', [
'pm2',
'restart',
process,
])
},
skip: () => !yargs.restart,
})
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ agentForward = true
sides = ["api","web"]
path = "/var/www/app"
processNames = ["serve"]
# pm2Global = true # defaults to false

# If you have separate api and web servers:
#
Expand Down