2017-07-11 21 views
3

いくつかのバックエンドシステムを呼び出し、リモートでデータベースを更新するカスタムコードがあります。私は、コントローラ/ビューから手動でActiveJobをキックオフしたいRails ActiveJobコントローラから開始

## Runs join code 
class DataJoin < ApplicationJob 
    queue_as :default 

    def perform 
    join = Joiner.new 
    join.run 
    NotifMailer.sample_email.deliver_now 
    end 
end 

::私は、コントローラからActiveJobをキックオフするにはどうすればよい

class AdminController < ApplicationController 
    before_action :verify_is_admin 

    private def verify_is_admin 
     (current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?) 
    end 

    def index 
    @username = current_user.name 
    @intro = "Welcome to the admin console" 
    end 

    def join 
    ## Code to start ActiveJob DataJoin?? 
    end 
end 

私は、タスクを実行ActiveJobがありますか?

答えて

2

はこれを試してみてください:

DataJoin.perform_later 

perform_laterは、指定したキュー内のジョブをエンキューします。アクティブなジョブのメソッドがいくつかの引数を受け入れる場合、それらをperform_laterに渡すこともできます。これらの引数は実行時に利用可能になります。

DataJoin.perform_later(1, 2, 3) 

# DataJoin 
def perform(a1, a2, a3) 
    # a1 will be 1 
    # a2 will be 2 
    # a3 will be 3 
end 
関連する問題