1. Rails Finite State Machine: AASM != perfect, but > Workflow

    I recently re-wrote an old model that desperately needed a formalized state machine definition (instead of the fragile logic I originally strung together).  After reviewing both AASM (formerly acts_as_state_machine) and Workflow, I went with the former for a couple of reasons.

    1. AASM seemed to be a bit more mature
    2. It made more sense to describe an event (i.e. state machine input) once, with all the transitions it could cause and its to/from states, rather than defining it a separate time for each state-transition pair it could affect.

    For example, one might define a traffic light in AASM as follows:

    aasm_state :red, :yellow, :green
    aasm_event :timer_done do
    	transitions :from => :red, :to => :green
    	transitions :from => :green, :to => :yellow
    	transitions :from => :yellow, :to => :red
    end

    Whereas Workflow would have one define it:

    workflow do
    	state :red do
    		event :timer_done, :transitions_to => :green
    	end
    	state :green do
    		event :timer_done, :transitions_to => :yellow
    	end
    	state :yellow do
    		event :timer_done, :transitions_to => :red
    	end
    end

    I suppose it depends on the situation whether it’s clearer to define states - and everything that can cause one to leave that state - or to define events - and all the states on which they operate.

    But beware: AASM is not a perfect finite state machine.  You cannot auto-transition out of the initial state via the :after_enter method.  The initializer code will set your model’s state variable to whatever is defined by aasm_initial_state after all callbacks are run, even if the state machine transitions to a new state due to those initial states’ callbacks.

    For example, the following code would result in a :red initial state, not a :green initial state as would be expected:

    aasm_initial_state :red
    aasm_state :red, :after_enter => Proc.new { |x| x.go_green! }
    aasm_event :go_green do
    	transitions :from => :red, :to => :green
    end

     

    tags:  rails  bug  gem  state machine 

    Comments
  2. Negative content-length in HTTP header

    I recently debugged a fairly interesting problem one of our HomeField users was having with uploads.  The file was > 2GB so the Flash uploader was not an option, and they were using our HTTP uploader.  But every time they tried, after a couple of minutes, the connection would fail due to timing out.  So I got in touch with their IT staff who graciously captured all the traffic on the uplink during a failed upload via wireshark.

    After filtering for the ip address of our upload server, I was surprised to see only a handful of packets, most of which were retransmits.  A deep dive showed that the TCP handshake completed, but the first HTTP packet never heard any response back from us.  It was retransmitted by the client, as per TCP specification, and the receiving software on our server never responded.  Closer examination of the HTTP portion of that packet revealed an interesting header field: content-length: -194931179

    HTTP 1.1 specification says that “any content-length greater than or equal to zero is a valid value.”  Unfortunately, Windows NT 5.1 (Windows XP) and IE 8 - according to the same header - have an integer overflow issue when dealing with files greater than 2GB.

    This OS level issue was causing the negative content-length, which prevented our server from responding, which caused a confusing (and poor) user experience.  Now I just need to get this user on a *nix OS…

     

    tags:  bug  http  homefield  customer support 

    Comments
  3. Rails InvalidAuthenticityToken

    I am a big fan of the Rails plugin exception_notification.  It’s dead simple to integrate with your app and it alerts you to any problems as they happen.  You get an email with all the information you need to resolve the issue.  So, after reading the email you 1) fix the bug 2) cap deploy 3) email the affected user and 4) profit (i.e. make your user happy)!

    For a few weeks now I’ve been getting emails with the subject ActionController::InvalidAuthenticityToken.  I wasn’t sure why, and couldn’t track it down since I couldn’t reproduce it.  When using restful_authentication you get two logout methods: logout_keeping_session! and logout_killing_session!. As it turns out, I was killing the session in a couple of controllers.  If the user then tries to log in, their authenticity token will no longer be valid, which produces the aforementioned InvalidAuthenticityToken error.  (The plugin expressly warns against using logout_killing_session!, by the way).

    Rails is powerful.  You can express a lot with little code, and the framework supports you from all over.  This results in a learning curve that isn’t steep, but is longer than most.  I learn something new about Rails (and Ruby) weekly, usually right after I break it.  That’s how I (like to) learn.

     

    tags:  ruby  rails  error  bug 

    Comments
  4. blog comments powered by Disqus