Rails ActiveJob从控制器开始

4

我有一些定制代码,调用一些后端系统并远程更新数据库。我有一个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个回答

3

试试这个:

DataJoin.perform_later

perform_later 函数将作业放入指定的队列。如果你的 active job 的 perform 方法接受一些参数,你甚至可以在 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

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接