Sidekiq -r 单个 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)

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

解决方法

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

但请注意,Rails 中的 active_support 使用 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