Ruby Gem Delayed_Job:不处理存储在lib文件夹

时间:2016-05-19 05:18:09

标签: ruby-on-rails ruby ruby-on-rails-4 delayed-job

我已经安装了Ruby gem Delayed_Job来运行队列中的任务,但它显示了一些我不理解的行为。 Delayed_Job正在使用我的本地active_record,因此是一个非常标准的安装。

我在/ lib文件夹中名为test_job.rb的文件中有作业代码

    class TestJob
        # Create a entry in the database to track the execution of jobs
        DatabaseJob = Struct.new(:text, :emails) do
            def perform
                # Perform Test Code
            end
        end
    def enqueue
        #enqueue the job
        Delayed::Job.enqueue DatabaseJob.new('lorem ipsum...', 'test email')
    end
end

当我尝试从这样的控制器调用代码时,第一次工作似乎被提交(在rake jobs:work中列出)但它没有运行:

require 'test_job' 
class ExampleController < ApplicationController

      def index
      end

      def job
        # Create a new job instance
        job = TestJob.new
        # Enqueue the job into Delay_Job
        job.enqueue
      end
    end

然后,当我更改控制器代码以执行我的lib类所做的操作时,它完美地运行。该作业不仅会被提交到队列,还会运行并完成而不会失败。

require 'test_job'

class ExampleController < ApplicationController
    # Create a entry in the database to track the execution of jobs
        DatabaseJob = Struct.new(:text, :emails) do
            def perform
                # Perform Test Code
            end
        end

  def index
  end

  def job
    #enqueue the job
    Delayed::Job.enqueue DatabaseJob.new('lorem ipsum...', 'test email')
  end
end

奇怪的是,当我切换回调用lib作业类时,它没有问题。然后,无论结构是直接在控制器中定义还是在lib文件夹中的类中都无关紧要。

在控制器中定义结构并以这种方式将作业提交到队列似乎总是有效,但之后lib类也开始工作,有时lib类甚至在重新启动rails服务器后也能工作。

有什么想法吗?非常感谢您的帮助。

最佳,

的Bastian

0 个答案:

没有答案