如何使用ActiveJob(Resque Adapter)与Rails没有ActiveRecord(使用Mongoid)

时间:2016-06-20 18:32:21

标签: ruby-on-rails ruby activerecord resque rails-activejob

我一直在使用带有Rails的Mongoid Adapter作为应用程序。所以我基本上把项目设置为不使用ActiveRecord(因为我也将它部署到Heroku)。我遵循了一个教程,它对我有用:

Remove database adapter gems from your Gemfile, e.g., mysql2, sqlite3, etc

From application.rb, remove require 'rails/all' and add

require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

Delete database.yml, schema.rb and all the migrations
Delete migration checks from test/test_helper.rb
Delete all activerecord related configuration from config/environments

但是,现在 - 当我在我的网络应用程序(导入csv进程)中优化进程时,我决定使用ActiveJob + Resque来使用延迟作业。

据我所知,我需要Redis for Resque,所以我安装了它并运行了服务器,但是Resque的rake任务设置如下:

require 'resque/tasks'

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

似乎需要ActiveRecord来完成它的工作(当然因为它映射到redis数据库)。当我跑这个时毫不奇怪:

LOGGING=1 QUEUE=* bundle exec rake resque:work

它会发出错误(因为缺少ActiveRecord):

*** Failed to start worker : #<NameError: uninitialized constant ActiveRecord>

如何使这项工作?

1 个答案:

答案 0 :(得分:0)

我知道这是前一段时间被问到的,但这里的答案是你可以安全地删除前/后工作程序与你自己的resque.rake

这是我的:

#lib/tasks/resque.rake

require 'resque/tasks'
require 'resque/scheduler/tasks'

namespace :resque do 
    task setup: :setup_logger do #setup logger includes environment!
        require 'resque'
        require 'resque-scheduler'
    end
 end


#Sets up logging - should only be called from other rake tasks
task setup_logger: :environment do
    logger           = Logger.new(STDOUT)
    logger.level     = Logger.const_get((ENV["LOG_LEVEL"] || "DEBUG").upcase)
    Rails.logger     = logger
end