2015-08-14 74 views

回答

3

您必須創建ActiveJobsrake任務才能觸發它們。例如: 在app/jobs,創建具有以下代碼share_job.rb

require "active_job" 
class ShareJob < ActiveJob::Base 
    def perform 
    func_to_call = rand(n) # n is the number of methods you have 
    case func_to_call 
    when 0 
     # call first_func 
    when 1 
     # call second_func 
    #... 
    end 
end 

然後在lib/tasks,創建一個execute_job.rake文件用下面的代碼:

namespace :execute do 
    task execute_random_function: :environment do 
    ShareJob.perform_later() 
    end 
end 

要手動運行該代碼,輸入你的console

rake execute:execute_random_function 

您可以執行此job在隨機時間使用Scheduler(a HerokuAdd-on