2011-08-17 52 views
0

我试图拖延以一天一列(yest_shareholder_count)增量每当为了创建一个investment记住这有一个venture一天前的investment用户数量的记录(递增因此我可以使用这些数据生成追踪投资者数量的图表)。使用delayed_job的

我觉得最简单的方法是使用delayed_job,但这是行不通的。 delayed_job是在单独的表中创建的,根据SQLite数据库浏览器,它在一天后运行。 但yest_shareholder_count列未被增加。

Venture型号:

def yest_increment 
     self.increment!(:yest_shareholder_count) 
    end 
    handle_asynchronously :yest_increment, :run_at => Proc.new {1.minute.from_now} 

Investments控制器:

def create 
    @venture = Venture.find(params[:share_id]) 

    @venture_increment= @venture.increment(:shareholder_count) 
    @venture_yest_increment= @venture.yest_increment 

    @investment = current_user.invest!(@venture) 

    if @venture_increment.save and @venture_yest_increment.save 
    respond_to do |format| 
     format.html {redirect_to @venture} 
      end 
    end 

    end 

直截了当的增量情况发生,因为它应该,但延迟增量从来不会。

这些都是在delayed_jobs表中的“处理程序”栏中的内容,以防万一他们是有用的:

--- !ruby/struct:Delayed::PerformableMethod 
object: !ruby/ActiveRecord:Venture 
    attributes: 
    created_at: 2011-06-21 07:00:12.252808 
    onemth_shareholder_count: 0 
    updated_at: 2011-08-09 16:50:36.864354 
    onewk_shareholder_count: 0 
    shareholder_count: 4 
    id: 49 
    user_id: 40 
    yest_shareholder_count: 0 
method_name: :yest_increment_without_delay 
args: [] 

我不知道这里的“yest_increment_without_delay”是从哪里来的,为什么“ args“是空白的...

任何帮助非常感谢。

编辑:我有“1.minute.from_now”而不是“1.day.from_now”只是为了测试代码。

回答

2

当您使用延迟作业定义要异步处理的方法时,延迟作业将创建一个别名方法链(我不太熟悉)。它导致调用由延迟作业处理的原始方法名称,并且当延迟作业接近它时,它会调用original_method_name_without_delay,这是实际的对象方法别名。

没有任何参数,因为您没有将任何参数传递给yest_increment。如果您将任何参数传递给yest_increment,它们将与作业一起保存,并在延迟作业调用时传递给实际方法。

表中的对象是对象的序列化版本,delayed_job存储在db中。当需要运行作业时,它会通过反序列化来创建一个对象,并使用给定的参数调用该方法。由于对象是一个ActiveRecord对象,反序列化涉及从序列化对象中检索id并从数据库中查找对象。

根据您的实际问题,它不工作,不清楚什么是不工作。延迟的作业没有被调用,或者值没有增加,或者delayed_job过早调用。如果是最后一个,将代码更改为以下内容可能会有所帮助:

handle_asynchronously :yest_increment, :run_at => Proc.new {Time.now.beginning_of_day + 1.day} 
+0

感谢您的回答。问题是该值不会递增。 – jonic

+0

对不起 - 这不清楚。刚刚编辑了这个问题。 – jonic

+0

'yest_increment'应该返回什么?它不会返回,因为延迟的工作会拦截呼叫而不会调用该方法。从控制台调用'yest_increment_without_delay'是否正确增加? – rubish