2010-05-29 46 views
0

我目前使用Workflow。現在statemachine,條件轉換

class Link < ActiveRecord::Base 
    include Workflow 
    workflow do 
    state :new do 
     event :process, :transitions_to => :checking #checking http_response_code & content_type 
    end 

    state :checking do 
     event :process, :transitions_to => :fetching_links # fetching all links 
    end 
    state :fetching_links do 
     event :process, :transitions_to => :checking #ready for next check 
    end 
    end 
end 

,我可以這樣做:

l = Link.new 
l.process! 
l.process! 
l.process! 
l.process! 
# n times l.process! (in a loop, or cron job for example) 

但它可偏偏,有些鏈接將不響應,或者給我一個無效響應德寧檢查過程。

我如何有條件地切換到另一個狀態?

我的意思是這樣的:

class Link < ActiveRecord::Base 
    include Workflow 
    workflow do 
    state :new do 
     event :process, :transitions_to => :checking #checking http_response_code & content_type 
    end 

    state :checking do 
     event :process, :transitions_to => :fetching_links # if all is fine 
     event :process, :transitions_to => :failded # if something goes wrong 
    end 
    state :fetching_links do 
     event :process, :transitions_to => :checking #ready for next check 
    end 
    end 
end 

回答

1

我不知道您的動作將被執行。 on_entryon_exit的狀態。

我認爲最好的是,當轉換到我的下一個狀態時,如果發生錯誤,我可以轉換到另一個狀態,例如, :failed。我沒有嘗試過,但我發現你可以將halt過渡到一個狀態,這意味着轉換失敗。

例如:

state :checking do 
    event :process, :transitions_to => :fetching_links do 
    fetch_links 
    halt if fetch_links_failed? 
    end 
end 

你會使用如下:

l.process 
l.halted? => true # if fetching_links failed 

這是否幫助?