延迟的工作:在开发模式下的每次调用期间如何重新加载有效载荷类 [英] Delayed job: How to reload the payload classes during every call in Development mode

查看:64
本文介绍了延迟的工作:在开发模式下的每次调用期间如何重新加载有效载荷类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在安排一名延迟工作的工人。每当我调用 foo 方法时,工作人员都会打印 hello

I am running a delayed job worker. When ever I invoke the foo method, worker prints hello.

class User
  def foo
    puts "Hello"
  end
  handle_asynchronously :foo
end

如果我对 foo做一些更改方法,我必须重新启动工作程序才能反映所做的更改。在开发模式下,这可能会变得很累。

If I make some changes to the foo method, I have to restart the worker for the changes to reflect. In the development mode this can become quite tiresome.

我正在尝试找到一种方法来为每个请求重新加载有效负载类(在这种情况下为User类)。我试过猴子修补DelayedJob库,以在调用有效负载方法之前调用 require_dependency

I am trying to find a way to reload the payload class(in this case User class) for every request. I tried monkey patching the DelayedJob library to invoke require_dependency before the payload method invocation.

module Delayed::Backend::Base
  def payload_object_with_reload
    if Rails.env.development? and @payload_object_with_reload.nil?
      require_dependency(File.join(Rails.root, "app", "models", "user.rb"))
    end
    @payload_object_with_reload ||= payload_object_without_reload
  end
  alias_method_chain :payload_object, :reload
end

这种方法不起作用使用 require_dependency 注册的类需要在调用之前重新加载,但我还不知道该怎么做。我花了一些时间阅读调度程序代码,以了解Rails如何为每个请求重新加载类。我找不到重新加载代码。

This approach doesn't work as the classes registered using require_dependency needs to be reloaded before the invocation and I haven't figured out how to do it. I spent some time reading the dispatcher code to figure out how Rails reloads the classes for every request. I wasn't able to locate the reload code.

有人曾经尝试过吗?您如何建议我继续?还是您有找到Rails类重载代码的指针?

Has anybody tried this before? How would you advise me to proceed? Or do you have any pointers for locating the Rails class reload code?

推荐答案

我设法找到了解决方案。我使用 ActiveSupport :: Dependencies.clear 方法清除加载的类。

I managed to find a solution. I used ActiveSupport::Dependencies.clear method to clear the loaded classes.

添加一个名为 config / initializers / delayed_job.rb

Delayed::Worker.backend = :active_record
if Rails.env.development?
  module Delayed::Backend::Base
    def payload_object_with_reload
      if @payload_object_with_reload.nil?
        ActiveSupport::Dependencies.clear
      end
      @payload_object_with_reload ||= payload_object_without_reload
    end
    alias_method_chain :payload_object, :reload
  end
end

这篇关于延迟的工作:在开发模式下的每次调用期间如何重新加载有效载荷类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆