[英]Rails execute script as background job
我有一個ruby腳本,它已實現為獨立功能。 現在,我想在我的rails環境中執行此腳本,但又增加了將其作為后台作業執行的難度,因為它需要大量的時間處理。
添加delay_job gem之后,我嘗試調用以下語句:
delay.system("ruby my_script.rb")
這是我得到的錯誤:
Completed 500 Internal Server Error in 95ms
TypeError (can't dump anonymous module: #<Module:0x007f8a9ce14dc0>):
app/controllers/components_controller.rb:49:in `create'
就像傑夫所說,最好的解決方案是創建自定義作業。 Jef的答案的問題是,據我所知,它的語法不正確,這是他的工作處理單個系統命令,而以下內容將允許您進行更多自定義:
# lib/system_command_job.rb
class SystemCommandJob < Struct.new(:cmd)
def perform
system(cmd)
end
end
請注意Struct初始化程序的cmd參數。 它允許您將參數傳遞給您的工作,因此代碼如下所示:
Delayed::Job.enqueue(SystemCommandJob.new("ruby my_script.rb"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.