Skip to content

Commit

Permalink
allows you to run a script or all start commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rslucena committed May 14, 2024
1 parent 1149458 commit 1221a8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/commands/exec-process.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from 'child_process'
import pm2Workspace from './pm2-workspace'

const worker = process.env.npm_config_worker
const worker = process.env.npm_config_worker === 'all' ? undefined : process.env.npm_config_worker
const err = new Error('Unable to locate the script, provider, or container for execution.')

if (!worker) {
Expand All @@ -21,6 +21,12 @@ const child = spawn(command, { stdio: 'inherit', shell: true })

child.on('message', (message) => console.warn(message))

child.on('error', (error) => console.error('command error:', error))
child.on('error', (error) => {
console.error('command error:', error)
process.exit()
})

child.on('close', (code) => console.error(`command exited with code ${code}`))
child.on('close', (code) => {
console.error(`command exited with code ${code}`)
process.exit()
})
16 changes: 8 additions & 8 deletions src/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import { messages } from '@infrastructure/messages/actions'
import pm2 from 'pm2'
import pm2Worker from './commands/pm2-worker'
import pm2Workspace from './commands/pm2-workspace'
const enginer = process.env.npm_lifecycle_event === 'workers' ? 'tsx' : 'node'

const worker = process.env.npm_config_worker

if (!worker || worker.split('/').length <= 1) {
console.debug('Worker not found, running all workspace applications.')
}
const enginer = process.env.npm_lifecycle_event === 'workers' ? 'tsx' : 'node'
const worker = process.env.npm_config_worker === 'all' ? undefined : process.env.npm_config_worker

pm2.connect(async function (err) {
if (err) {
Expand All @@ -19,18 +15,22 @@ pm2.connect(async function (err) {
const jobs = worker ? pm2Workspace.find((configs) => configs.name === worker) : pm2Workspace

if (!jobs) {
console.log(new Error('Unable to locate the script, provider, or container for execution.'))
console.error(new Error('Unable to locate the script, provider, or container for execution.'))
process.exit()
}

const workspace = Array.isArray(jobs) ? jobs : [jobs]

const workers = Promise.all(workspace.map((worker) => pm2Worker.start(enginer, worker)))
workers.catch((err) => console.log(err))

workers.catch((err) => console.error(err))

workers.then(() => pm2Worker.list(enginer))

messages.sub('workers:server:info', async (message: string) =>
message.length ? pm2Worker.info(message, enginer) : undefined
)

messages.sub('workers:server:restart', async (message: string) =>
message.length ? pm2Worker.restart(message) : undefined
)
Expand Down

0 comments on commit 1221a8f

Please sign in to comment.