ruby unicorn fork process A, and how to A send it's output to nginx

457 views Asked by At

here is my code, AppLogger.error output msg to stdout. first, i call fork to produce a process pid, in the process, @app.call(env) is a long time task and will output a result. after fork, i call detach to avoid a zombie.

    pid = fork{
        AppLogger.error "fork"
        @app.call(env)
        AppLogger.error "end fork"
    }
    Process.detach(pid)

but web browser get a 500. so what should i do to send result to nginx.

1

There are 1 answers

2
Gavin Miller On

When you use fork with a block the code within the block is going to run in a child process. Therefore your server is likely waiting for the output of the parent process, doesn't receive a response and 500s (if you can post the details of the 500 that would help!)

To pass the output of the child process back to the parent process you can use a pipe:

reader, writer = IO.pipe

pid = fork {
    reader.close

    AppLogger.error "fork"
    writer.write(@app.call(env))
    AppLogger.error "end fork"
}

writer.close
Process.wait(pid)

response_to_client = reader.read

As mentioned by Sergio, this doesn't make a ton of sense, especially in the context of Unicorn, since Unicorn is already forking off child processes to execute your web requests.