Run background process in Sinatra

I have a Sinatra / Rails application and an activity that starts a long running process.

Normal I am making a queue for background jobs. But this case is too simple and the background process starts very rarely, so the queue is an overhead.

So how do I start a background process out of the queue?

get "/build_logs/:project" do
  LogBuilder.new(params[:project]).generate
  "done"
end

      

I tried to do it as a new fork of Thread or Process, but it didn't help.

+3


source to share


1 answer


I had success with this (simplified) in Sinatra:

get '/start_process'
  @@pid = Process.spawn('external_command_to_run')
end

      



This returns the process ID, which you can use to terminate the process later if you need to. Also, on Linux it won't work on Windows.

+1


source







All Articles