使用线程安全时,模型类未在“延迟的作业”中加载 [英] model classes not loading in Delayed Job when using threadsafe

查看:56
本文介绍了使用线程安全时,模型类未在“延迟的作业”中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期运行良好的应用程序。我打开了线程安全!,现在延迟作业无法正常工作,并说找不到我的模型文件之一。

I've got an app that has been working well for a long time. I'm turning on threadsafe!, and now Delayed Job is not working, saying it can't find one of my model files.


Job failed to load: undefined class/module Foo

这不是我在/ lib中定义的自定义作业类,这是一个应用程序/模型中的模型文件

This isn't a custom job class I have defined in /lib, this is a model file in app/models

什么可能导致此类无法加载?

what could be causing this class to not be loaded?

推荐答案

如您所记录的问题上所述,以防其他人首先找到该帖子:

As posted on the issue you logged, in case others find this SO post first:

我也遇到了这个问题...并且这就是我的发现:如果通过rake任务加载应用程序,Rails不会急于加载类(这就是DJ的工作方式)。

I just hit this problem as well... and here's what I found: Rails does not eager load classes if the app is loaded via a rake task (and that's how DJ does its thing).

这是我的 production.rb 中的代码段:

# Enable threaded mode, unless a rake task (likely Delayed Job) is running:
config.threadsafe! unless defined?($rails_rake_task) && $rails_rake_task

Rails设置了由rake任务加载的全局变量。丑陋,但现在看来对我来说还不错...当然,如果您有多线程的rake任务,那么这不是理想的选择,您应该调用Rails.application.eager_load!这些任务。我猜想多线程耙任务很少见。

Rails sets that global variable when it's loaded by a rake task. Ugly, but seems to be working for me fine now... Of course, if you have rake tasks that are multi-threaded, then this is not ideal, and you should probably invoke Rails.application.eager_load! for those tasks. I'm guessing multi-threaded rake tasks are rare though.

这篇关于使用线程安全时,模型类未在“延迟的作业”中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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