【问题标题】:Rails production server config.cache_classes = true does not reload custom service classes correctly for active jobRails 生产服务器 config.cache_classes = true 不会为活动作业正确重新加载自定义服务类
【发布时间】:2021-09-09 09:28:08
【问题描述】:

我在 Rails 6.1.3.2 中使用带有 sidekiq 适配器的 Active Job

奇怪的是,自定义服务单例方法在 Active Job 中调用时会丢失。

notify_message_event_job.rb

module Chats
  class NotifyMessageEventJob < ApplicationJob
    queue_as: default

    def perform(message:, event:)
      Rails.logger.info(PushNotification::Service.name)
      Rails.logger.info(PushNotification::Service.singleton_methods.inspect)
      PushNotification::Service.create(
        user: message.user,
        notification: message.content
      )
    end
  end
end

lib/push_notification/service.rb

module PushNotification
  class Service
    class << self
      def create(user:, notification:)
        // 
      end
    end
  end
end

下面一行:

NotifyMessageEventJob.perform_now(message: message, event: event)

产生以下错误:

[ActiveJob] [Chats::NotifyMessageEventJob] [b1d934c8-afe0-44a0-bc22-44d40eea7d3d] 来自 Sidekiq(默认)的执行 Chats::NotifyMessageEventJob(作业 ID:b1d934c8-afe0-44a0-bc22-44d40eea7d3d)参数:{:message=>#<0x00007fe5bc4f6b00 gid:>

&gt;

标签: ruby-on-rails sidekiq rails-activejob


【解决方案1】:

似乎这是 zeitwerk autoloading mode 的一个问题,它是在 Rails 6 中作为默认引入的

我在 config/environments/production.rb 中暂时选择退出:

config.load_defaults 6.1
config.autoloader = :classic

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2017-01-05
    • 2014-03-30
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2020-03-08
    相关资源
    最近更新 更多