Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attach to forked process on node using http

Exploring a project where I use Node http server. I am perhaps trying to push quite a bit into this, but that’s on me.

Basic concept:

Having a main.js script that:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Parses options (address, port, etc.)
  2. fork a HTTP server script (which process GET / POST ++).
  3. Initiate a CLI interface to control the server by using subprocess.send() to the forked process (restart, stop, quit, dump-info, log requests etc.)

Now that is all good. I can start the server, control it etc. For intended use however, the server is started as a background process. If another request to start it is done a replay that it is running is posted and one use normal GET / POST request to communicate.

My question is if there is a way to attach to a running fork. Something like:

  1. Server is started as a background process for Program A.
  2. I open a shell and want to attach to the running server and use the subprocess.send() procedure. Partially because it is already implemented from the existing code, but mainly because it gives the option to send more verbose commands then kill, it seems cleaner as it is not part of the HTTP protocol: communicating with the process on a deeper level …

It is perhaps not possible due to restrictions and I could rewrite for it to be all GET / POST based. (I.e. Start server and only use POST / GET / PUT etc.) – or there is a way to attach to the process and use send() as is.

In other words: I have a communication procedure that is for the end use (POST / GET) and other http usage; But also have a control interface that rely on direct process communication (process.send()).

On starting a CLI communication line I could ask any existing server to quit and then fork it, but that complicates things further.

Yet another way to describe it:

Having a HTTP server that accepts communication both by HTTP protocol and process messages. Is there a way to attach / send messages to an existing HTTP server process by a subprocess.send() like interface?

>Solution :

Attaching to a running forked process directly using subprocess.send() is not possible, as you can only communicate with the forked process from its parent process using this method. However, you can achieve similar functionality using inter-process communication (IPC) mechanisms like named pipes, Unix domain sockets, or even a WebSocket server.

One approach is to use named pipes (FIFO) for communication between the main CLI process and the HTTP server process. Here’s an example of how you can implement this:

In the main CLI process, create a named pipe:

const fs = require('fs');
const path = require('path');
const pipePath = path.join(__dirname, 'server-pipe');

// Remove the pipe if it already exists
try {
  fs.unlinkSync(pipePath);
} catch (err) {}

// Create a named pipe
fs.mkfifoSync(pipePath, 0o600);

Start the HTTP server as a separate process, and listen for incoming messages on the named pipe:

const http = require('http');
const fs = require('fs');
const path = require('path');
const pipePath = path.join(__dirname, 'server-pipe');

// Start the HTTP server
const server = http.createServer((req, res) => {
  // Your request handling logic here
});

server.listen(/* your desired port */);

// Listen for incoming messages on the named pipe
const pipeStream = fs.createReadStream(pipePath);
pipeStream.on('data', (data) => {
  // Handle commands from the CLI process
  const command = data.toString().trim();

  switch (command) {
    case 'restart':
      // Restart the server
      break;
    case 'stop':
      // Stop the server
      break;
    // Add more commands here as needed
  }
});

In the main CLI process, write commands to the named pipe:

const fs = require('fs');
const path = require('path');
const pipePath = path.join(__dirname, 'server-pipe');

// Write a command to the named pipe
fs.writeFileSync(pipePath, 'restart');

This way, you can use the named pipe to send commands from the CLI process to the HTTP server process, while still using the HTTP protocol for client communication.

Remember to clean up the named pipe when you’re done using it to avoid any potential issues.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading