Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run background process in Sinatra

I have got Sinatra/Rails app and an action which starts some long process.

Ordinary I make a queue for background jobs. But this case is too simple and background process starts very rarely, so queue is an overhead.

So how could I run background process without queue?

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

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

like image 819
fl00r Avatar asked Aug 29 '26 00:08

fl00r


1 Answers

I have 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. Also, this is on Linux, it will not work on Windows.

like image 72
Paul Hoffer Avatar answered Aug 30 '26 13:08

Paul Hoffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!