Resque 看到 mongoid 模型但不是 postgresql 表

Resque sees mongoid models but not postgresql tables

我在 rails 5 应用程序中使用 mongoid 和 postgresql。

我的 resque 作业与 mongoid 模型完美配合,但是,当我尝试在作业中使用我的 postgresql tables 之一时,出现以下错误:

PG::UndefinedTable: ERROR: relation "admins" does not exist LINE 1: SELECT "admins".* FROM "admins" ^ : SELECT "admins".* FROM "admins"

这是我的 lib/tasks/resque.rake 文件

require 'resque/tasks'

task "resque:setup" => :environment do
    ENV['QUEUE'] = '*'
    Resque.before_fork do
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
  end

  Resque.after_fork do
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
  end
end

提到的 postgres table 确实存在,并且与 rails 应用程序完美配合。看起来,至少在主 rails 应用程序之外,ActiveRecord 默认使用 mongoid,因此我的 postgresql 模型的 none 在 worker 中是可见的。或许不是。

我是不是漏掉了什么?

我最终通过稍微调整我的 Procfile 和任务文件来修复它,问题是由于 resque 没有选择正确的环境(因此未定义模型关系)

Procfile:

web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
worker: bundle exec rake resque:pool

我最终使用 lib/tasks/resque.rake 只是为了声明任务,并将初始化代码移至 lib/tasks/resque-pool.rake。基本上 resque:setupresque:pool:setup 之前被调用,它为池管理器预加载 rails 环境。

require 'resque/tasks'
require 'resque/pool/tasks'

task "resque:setup" => :environment do
    Resque.before_fork = Proc.new do
        ActiveRecord::Base.connection.disconnect!
    end
    Resque.after_fork = Proc.new do
        ActiveRecord::Base.establish_connection
    end
end

task "resque:pool:setup" do

end