Skip to content

Programmatically restarting Command using kill/start API produce wrong termination #463

@odeadglaz

Description

@odeadglaz

Description
I'm using concurrently to run 2 commands in parallel on my application:

  1. Watch's application code using swc
  2. A plain node ./dist/server.js command which starts my HTTP server.

I wanted to implement a logic of which every time SWC outputs a successful watch compilation output, to restart the NodeJS server by combining the Command object kill/start API.

Here is a simplified example of my code:

const commands = [
    { name: 'SWC watcher', command: 'swc src -d dist/server -w -D' },
    { name: 'HTTP Server', command: 'node -r dotenv/config ./dist/server/index.js' },
];

const concurrency = concurrently(commands, {
    killOthers: ['failure'],
    prefixColors: ['magenta', 'yellow']
});

let shouldRestart = false;
const [watcherCommand, serverCommand] = concurrency.commands;

serverCommand.close.subscribe(() => {
    if (shouldRestart) {
        shouldRestart = false;
        serverCommand.start();
    }
});

watcherCommand.stdout.subscribe((data) => {
    const input = data.toString().trim();

    if (compilationCompleteRegex.test(input)) { // Detects compilation end output
        shouldRestart = true;
        serverCommand.kill('SIGTERM'); // HTTP listens and gracefully shuts down
    }
});

Actual Behaviour
After 2 restarts the concurrency top command resolves as both commands was terminated but in fact, both commands still up and running.

Some Technical details
It seems that CompletionListener.listen method actually uses bufferCount to assume its commands were terminated by counting the close events but programmatically using Command.start/Command.kill API's can cause un-expected behaviour in this manner as nothing was actually terminated rather solely restarted

Thanks in advanced 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions