使用delayed_job递增

时间:2011-08-17 17:51:52

标签: ruby-on-rails ruby-on-rails-3 delayed-job increment

每当创建yest_shareholder_count时,我都会尝试将列(investment)的增量延迟一天,以便记录具有{{1}的用户数在investment前一天(所以我可以使用这些数据生成跟踪投资者数量的图表)。

我认为最简单的方法是使用venture,但这不起作用。 delayed_job是在一个单独的表中创建的,根据SQLite数据库浏览器,它会在一天后运行。 但是yest_shareholder_count列没有递增。

delayed_job型号:

Venture

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

Investments

直接的增量应该发生,但是延迟的增量永远不会发生。

这些是delayed_jobs表中“handler”列的内容,以防它们有用:

  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

我不确定“yest_increment_without_delay”来自哪里,或者为什么“args”是空白的......

任何帮助都非常感激。

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

1 个答案:

答案 0 :(得分: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}
相关问题