ruby-on-rails - 使用 delay_job 的 ActiveJob 不会在测试中创建 delay_jobs 行

标签 ruby-on-rails delayed-job ruby-on-rails-4.2 rails-activejob

在 Rails 4.2.3 下,我已将 ActiveJob 设置为在所有环境中使用 delay_job 作为其后端:

环境文件

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module MyApp
  class Application < Rails::Application
    # ... snip
    # Use delayed_job for Active Job queueing
    config.active_job.queue_adapter = :delayed_job
  end
end

(并且 queue_adapter 未在 config 下设置)。

我已按如下方式配置了我的 DelayedJob 实例 - 包括来自 this question 的代码向后移植获取作业的数据库行的 Rails 5 功能:

配置/delayed_job.rb
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes
Delayed::Worker.read_ahead = 10
# Delayed::Worker.delay_jobs = true #!Rails.env.test?
Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))

class Delayed::Job < ActiveRecord::Base
  belongs_to :owner, polymorphic: true
end

# Backport Rails 5 code to make the Delayed::Job database id avilable on ActiveJobs
module ActiveJob
  module Core
    # ID optionally provided by adapter
    attr_accessor :provider_job_id
  end

  module QueueAdapters
    class DelayedJobAdapter
      class << self
        send(:prepend, Module.new do
          def enqueue(job)
            provider_job = super
            job.provider_job_id = provider_job.id
            provider_job
          end

          def enqueue_at(job, timestamp)
            provider_job = super
            job.provider_job_id = provider_job.id
            provider_job
          end
        end)
      end
    end
  end
end

我有模型代码稍后执行工作,并 Hook Delayed::Job实例到模型实例上:

应用程序/模型/my_model.rb
def schedule
  job = MyJob.set(wait_until: scheduled_time).perform_later(self)

  # Associate the delayed_job object with the item that has been scheduled
  delayed_job = Delayed::Job.find(job.provider_job_id)
  delayed_job.owner = self
  delayed_job.save
end

(我已经为 Delayed::Job 设置了适当的关联)。

这似乎在开发中运行良好;但在测试中,Delayed::Job.find(job.provider_job_id)失败并引发异常,因为 provider_job_id为零。检查日志显示 delayed_jobs 没有 SQL INSERT table 。

为什么在测试中没有创建延迟作业行?

最佳答案

我遇到了同样的问题。原来是 ActiveJob::TestCase劫持 perform_later逻辑,实际上并没有将它们排队,而是将有关排队作业的数据存储在一个数组中( see here )。

我的解决方案是编写一个继承自 ActiveSupport::TestCase 的测试。相反,它似乎像正常一样排队延迟的作业。我相信有一些测试设置会强制内联执行作业,但据我所知,默认行为是将它们排队,因此它应该可以按预期工作而无需进行太多修改。

关于ruby-on-rails - 使用 delay_job 的 ActiveJob 不会在测试中创建 delay_jobs 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45147715/

相关文章:

mysql - Rails Active Record .paginate,获取第一页(行),如果指定的页面(行)在数据库表中不存在

mysql - rails 搜索方法

ruby-on-rails - ActiveRecord::AssociationTypeMismatch in Controller#create on dropdown select for a Rails self join

ruby-on-rails - 从应用程序中强行停止延迟的工作人员

ruby-on-rails - 在 View 助手中将部分渲染为字符串

ruby-on-rails - 索引中 instance_variable 的 RSpec

mysql2 gem 在安装过程中挂起

ruby-on-rails - 哪些 ERP 具有像样的 Ruby 连接器或完整的 API?

ruby-on-rails - workless 不工作(它没有启动 Heroku worker dyno)

ruby-on-rails - 如何让延迟的工作自己排队