views:

31

answers:

1

I'm currently using Workflow.

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

Now, I can do:

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

But it can happens, some link will not respond or give me an invalid response durning the checking process.

How I can conditionally switch to another state ?

I mean something like this:

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
A: 

I am not sure where your actions are executed. on_entry or on_exit of a state.

What i assumed would be nicest, is that upon transition to my next state, if some error occurred, i could transition into another state, e.g. :failed. I haven't tried that out, but i found that you can halt the transition to a state, which means that the transition failed.

For instance:

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

You would use that as follows:

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

Does that help?

nathanvda