rails - delayed_job:如何将我的代码放入后台

时间:2015-11-26 08:32:06

标签: ruby-on-rails delayed-job

gem 'daemon-spawn'
gem 'delayed_job_active_record'

我有一个班级" DoingJob"有一种方法"执行"。

def perform
  loop do 
     // my code in infinity loop
  end

在我的控制器"简单"中,我创建了一个DoingJob实例" job"并调用Job.delay.perform。没有任何反应,网页上也没有错误。

class SimpleController < ApplicationController
require 'opening_job.rb'
    def welcome
      job = OpeningJob.new
      job.delay.perform     
    end
end

我还在rails控制台上进行了一项实验。

user:~/workspace/blog (master) $ rails c
Loading development environment (Rails 4.2.4)
2.2.1 :001 > require 'job.rb'
 => true 
2.2.1 :002 > ob =  OpeningJob.new
 => #<OpeningJob:0x000000037ff0f0> 
2.2.1 :003 > ob.delay.perform

出现

(0.2ms)  begin transaction
  SQL (0.8ms)  INSERT INTO "delayed_jobs" ("handler", "run_at", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["handler", "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/object:OpeningJob {}\nmethod_name: :perform\nargs: []\n"], ["run_at", "2015-11-26 09:24:27.724717"], ["created_at", "2015-11-26 09:24:27.726736"], ["updated_at", "2015-11-26 09:24:27.726736"]]
   (21.0ms)  commit transaction
 => #<Delayed::Backend::ActiveRecord::Job id: 3, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMethod\nobject...", last_error: nil, run_at: "2015-11-26 09:24:27", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2015-11-26 09:24:27", updated_at: "2015-11-26 09:24:27"> 
2.2.1 :004 > Delayed::Job.last
  Delayed::Backend::ActiveRecord::Job Load (0.4ms)  SELECT  "delayed_jobs".* FROM "delayed_jobs"  ORDER BY "delayed_jobs"."id" DESC LIMIT 1
 => nil 

所以,问题是数据库中没有添加任何作业!

但是当我打开rails console --sandbox时可以添加作业。

0 个答案:

没有答案