2011-08-17 70 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