Ruby on rails Rails ActiveJob从控制器启动
我有一些自定义代码,可以调用一些后端系统并远程更新数据库。我有一个执行任务的ActiveJob:Ruby on rails Rails ActiveJob从控制器启动,ruby-on-rails,rails-activejob,Ruby On Rails,Rails Activejob,我有一些自定义代码,可以调用一些后端系统并远程更新数据库。我有一个执行任务的ActiveJob: ## Runs join code class DataJoin < ApplicationJob queue_as :default def perform join = Joiner.new join.run NotifMailer.sample_email.deliver_now end end ##运行连接代码 类DataJoin
## Runs join code
class DataJoin < ApplicationJob
queue_as :default
def perform
join = Joiner.new
join.run
NotifMailer.sample_email.deliver_now
end
end
##运行连接代码
类DataJoin
我想从控制器/视图手动启动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
class AdminController
如何从控制器启动ActiveJob?试试以下方法:
DataJoin.perform_later
将作业排入指定队列。如果活动工单的perform
方法接受一些参数,您甚至可以稍后将它们传入perform\u
,这些参数在执行时可用
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
请阅读
def join
DataJoin.perform_later
end