2016-07-25 83 views
0

我试图在控制器执行操作后3分钟运行一项工作。在X分钟内开始工作

我想这一点,与DelayedJob:

# in my controller 

def index 
    @books = Book.all 
    Delayed::Job.enqueue(ReminderJob.new(params[:user_id])) 
end 

而在ReminderJob.rb文件:

class ReminderJob < Struct.new(:user_id) 
    def perform 
    p "run reminder job" 
    # do something 
    end 
    handle_asynchronously :perform, :run_at => Proc.new { 3.minutes.from_now } 
end 

然而,访问索引页面的时候,我没有在日志中看出什么,和3分钟后没有任何反应。

我做错了什么?是否还有另一种建议的方式来“在现在X分钟内运行任务”而不使用sleep

+0

你读过[共问题#没什么-发生(https://github.com/ collectiveidea/delayed_job的/维基/共问题#没什么-发生)? –

+0

为什么不使用rails的内置'ActiveJob'? – siegy22

+0

@RaVeN我找不到任何可以用activeJob说“用X分钟开始这项工作”的方法。这可能吗 ? –

回答

1

在这个我真的会使用rails的内置组件ActiveJob。 请参阅here如何设置和基本使用。

在你的情况下,该代码将工作:

def index 
    user = User.find(params[:user_id]) 
    ReminderJob.set(wait: 3.minutes).perform_later(user) 
end 

和你的工作:

class ReminderJob < ApplicationJob # or use ActiveJob::Base 
    def perform(user) 
    # do something with the user 
    end 
end