Returning a response using Ruby CGI until the script completes?

Does anyone know how to send a CGI response to Ruby before the CGI script completes?

I am creating a Fire-and-forget HTTP API. I want the client to send me data via HTTP and successfully return a response, and then look through the data and do some processing (without the client waiting for a response).

I tried several things that do not work, including fork. The following will just wait 5 seconds when called via HTTP.

#!/usr/bin/ruby

require 'cgi'

cgi = CGI.new
cgi.out "text/plain" do
  "1"
end

pid = fork
if pid
  # parent
  Process.detach pid
else
  # child
  sleep 5 
end
+3
source share
1 answer

I answered my question. Turns out I just need to close $ stdin, $ stdout and $ stderr in the child process:

#!/usr/bin/ruby

require 'cgi'

cgi = CGI.new
cgi.out "text/plain" do
  "1"
end

pid = fork
if pid
  # parent
  Process.detach pid
else
  # child
  $stdin.close
  $stdout.close
  $stderr.close
  sleep 5 
end
+3
source

Source: https://habr.com/ru/post/1775049/


All Articles