2017-08-17 52 views
0

我有一個編譯按鈕,在我的Rails應用程序,它確實防止在掛ssh的時候掛軌?

get_pdf_cmd = ['ssh', '-i', '~/.ssh/id_rsa', '-o', 'StrictHostKeyChecking=no', '[email protected]', '/bin/bash', '--login', '/compile.sh', container['host'] ] 

Rails.logger.info(get_pdf_cmd) 

stdin, stdout_and_stderr, wait_thr = Open3.popen2e({}, get_pdf_cmd.join(" ")) 

Rails.logger.info stdout_and_stderr.gets(nil) 
stdout_and_stderr.close 
stdin.close 
exit_code = wait_thr.value 

,雖然這種情況發生的整個Rails應用程序掛起並且不響應。

問題的SSH命令運行時

我怎樣才能防止滑軌從掛?

+2

在ActiveJob中執行它嗎? –

回答

1

正如評論中已經指出的那樣,最好的做法是使用後臺進程來完成繁重的工作。

rails爲可用於rails的許多作業框架提供了一個抽象層。在引導閱讀更多關於這個話題http://guides.rubyonrails.org/active_job_basics.html

如果你的Rails應用程序依賴於這樣的操作的結果,你需要實現某種形式的投票或使用現代客戶溝通風格像ActionCable http://guides.rubyonrails.org/action_cable_overview.html

fork也儘管如此,它並不推薦在跑軌應用程序中使用。