首页 > 解决方案 > Sidekiq -ra 单个 demo_worker.rb 文件,并且需要该工作人员的积极支持不起作用

问题描述

我想在我的 demo_worker.rb 中要求 active_support。

require 'sidekiq'
require 'active_support'

class DemoWorker
end

然后我跑了sidekiq -r ./demo_worker.rb

它告诉我不能需要active_support,我也尝试需要rails,但仍然没有工作。如何在 sidekiq 的普通模式下需要 active_support?

这是我得到的确切错误

cannot load such file -- active_support/core_ext (LoadError)

我环顾四周,找不到有用的东西。

标签: ruby-on-railsrubysidekiqactivesupport

解决方案


发生此错误的原因是 gemactivesupport尚未安装(在您的本地计算机上),因此只需运行 command gem install activesupport,就可以了。

但请注意,active_support在 Rails 中使用autoload,所以如果我们要在 Rails 之外使用它的子模块,我们需要直接要求它们,例如我想使用Integer#hours我需要要求的扩展active_support/core_ext/integer/time

require 'sidekiq'
require "active_support/core_ext/integer/time"

class Demo
  include Sidekiq::Worker

  def perform(how_hard="super hard", how_long=1)
    sleep how_long
    puts "Workin' #{how_hard} #{8.hours}"
  end
end

推荐阅读