【发布时间】:2021-12-19 12:38:18
【问题描述】:
我的 RoR 应用程序从 Linux 操作系统触发数据处理脚本(SAS 技术)。 Scheduler::ProductionExecutionsController 中的 execute 方法驱动与脚本的交互。
调度器::ProductionExecutionsController
# POST /production_executions/1/execute
def execute
@production_execution = ProductionExecution.find(params[:id])
@production_execution.update_attributes(started_at: Time.now,
status_id: statuses.find { |x| x["code"] == "RUNNING" }.id)
--- Scripts interaction ---
@production_execution.update_attributes(ended_at: Time.now,
status_id: statuses.find { |x| x["code"] == "FINISHED" }.id,
source_records_count: global_input_count,
processed_count: global_output_count,
error_message: nil
)
respond_to do |format|
format.html { redirect_back fallback_location: @production_execution, notice: @msg }
format.js
end
使用以下语法调用此方法:link_to "Go!", execute_scheduler_production_execution_path(execution)。路由已定义,并按预期工作。
由于某些脚本可能需要一分钟以上的时间才能执行,我需要在专门的作业中执行脚本,有时还要安排它们。所以我安装了 Sidekiq 并定义了一个 Scheduler::ScriptWorker :
class Scheduler::ScriptWorker
include Sidekiq::Worker
sidekiq_options queue: :default, tags: ['script']
def perform(execution_id)
puts "Sidekiq job start"
puts execution_id
redirect_to execute_scheduler_production_execution_path(execution_id) and return
end
end
执行与Scheduler::ScriptWorker.perform_async(@execution.id)一起排队
Sidekiq 运行正常,但每次调用 worker 时,都会引发以下错误:
WARN: NoMethodError: undefined method `execute_scheduler_production_execution_path' for #<Scheduler::ScriptWorker:0x000000000b15ded8>
这样做是否正确,我该如何解决这个问题? 感谢您的帮助!
【问题讨论】:
-
将执行脚本的代码移动到服务中。从控制器和工作人员调用服务。
标签: ruby-on-rails sidekiq