Run background process in Sinatra

大城市里の小女人 提交于 2019-12-07 15:27:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9911566/run-background-process-in-sinatra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!