2011-06-03 33 views
1

爲了按照時間表啓動delayed_job,您需要使用執行幫助類來執行delayed_job可以調用的方法。這些需要在使用它們創建預定的delayed_jobs的任何類被調用之前被定義。所有的都很短,其中很多都是我的情況。例如:Rails中DelayedJob的幫助類

class AccountUpdateJob < Struct.new(:account_id) 
    def perform 
    acct = Account.find(account_id) 
    acct.api_update 
    end 
end 

我在一家在初始化稱爲「dj_helper_classes」文件夾的文件做這個。這是正確的做法嗎?

回答

0

我將lib保留在lib/jobs中,每個類一個文件。因此,您的示例將在lib/jobs/account_update_job.rb中

module Jobs 
    class AccountUpdateJob < Struct.new(:account_id) 
     def perform 
      acct = Account.find(account_id) 
      acct.api_update 
     end 
    end 
end 
+0

您如何確保它們都能及時加載? – Rourke 2011-06-04 05:30:38

+0

默認情況下,Rails3不會自動加載lib目錄中的所有內容。您可以通過將以下內容添加到application.rb'config.autoload_paths + =%W(#{config.root}/lib)來實現此目的。' 'config.autoload_paths + = Dir [「#{config.root}/lib/** /「]' – cailinanne 2011-06-04 17:37:29

+0

可能值得注意的是'application.rb'已經包含了相關的lib自動加載行,它只是默認註釋掉了。 – coreyward 2011-06-04 17:39:50