Can I make AASM run a specific method on event fail? - ruby

Is there a nice way to tell AASM that if an exception is raised while processing any assm_event I want that error to be caught by a specific block of code?
eg currently I do something like
assm_state :state_1
assm_state :state_2, :before_enter => :validate_something
assm_state :failed
assm_event :something_risky do
transition :from => :state_1, :to => :state_2
end
assm_event :fail do
transition :from => [:state_1, :state_2], :to => :failed
end
def validate_something
begin
something_that_might_raise_error
rescue
self.record_error
self.fail
end
end
and what I would prefer to do is something like
assm_state :state_1
assm_state :state_2, :before_enter => :validate_something
assm_state :failed
assm_event :something_risky, :on_exception => :log_failure do
transition :from => :state_1, :to => :state_2
end
assm_event :fail do
transition :from => [:state_1, :state_2], :to => :failed
end
def validate_something
something_that_might_raise_exception
end
def log_failure
self.record_error
self.fail
end
and have log_failure be called if something_that_might_raise_exception does raise an exception. Ideally I want to avoid changing AASM so I am safe if I need to upgrade it in the future

If you use SimpleStateMachine you can do:
def something_risky
something_that_might_raise_error
rescue
record_error
raise #reraise the error
end
event :something_risky, :state1 => :state2,
RuntimeError => :failed

I also had this problem. I had two things to do.
Follow the suggestion in this blog http://degenportnoy.blogspot.com/2009/11/event-callbacks-in-aasm.html.
You need to restart your app even if you are working on development mode.

Related

AASM: Separating the state machine definition from the class definition

suppose I have this class (taken directly from the aasm documentation):
class Job < ActiveRecord::Base
include AASM
aasm do
state :sleeping, :initial => true
state :running
state :cleaning
event :run do
transitions :from => :sleeping, :to => :running
end
event :clean do
transitions :from => :running, :to => :cleaning
end
event :sleep do
transitions :from => [:running, :cleaning], :to => :sleeping
end
end
end
I don't like a lot the fact that I have my state machine definition mixed with my class definition (since of course in a real project I will add more methods to the Job class).
I would like to separate the state machine definition in a module so that the Job class can be something along the line of:
class Job < ActiveRecord::Base
include StateMachines::JobStateMachine
end
I then created a job_state_machine.rb file in app/models/state_machines with a content similar to:
module StateMachines::JobStateMachine
include AASM
aasm do
state :sleeping, :initial => true
state :running
state :cleaning
event :run do
transitions :from => :sleeping, :to => :running
end
event :clean do
transitions :from => :running, :to => :cleaning
end
event :sleep do
transitions :from => [:running, :cleaning], :to => :sleeping
end
end
end
but this is not working because AASM is being included in the module not in the Job class... I even tried changing the module to:
module StateMachines::JobStateMachine
def self.included(base)
include AASM
aasm do
state :sleeping, :initial => true
state :running
state :cleaning
event :run do
transitions :from => :sleeping, :to => :running
end
event :clean do
transitions :from => :running, :to => :cleaning
end
event :sleep do
transitions :from => [:running, :cleaning], :to => :sleeping
end
end
end
end
but still it is not working... any hint or suggestion is very appreciated.
Thanks,
Ignazio
EDIT:
Thanks to Alto, the correct solution is this:
module StateMachine::JobStateMachine
def self.included(base)
base.send(:include, AASM)
base.send(:aasm, column: 'status') do
....
end
end
end
and obviously remember to include the state machine definition in the main class like this:
include StateMachine::JobStateMachine
Couldn't you simple do this?
module StateMachines::JobStateMachine
def self.included(base)
base.send(:include, AASM)
aasm do
...
end
end
end

cramp framework sync 'render' correct way using em-synchrony

To describe my problem I attach simple Cramp http://cramp.in/ class.
I add some modification but its mainly work like https://github.com/lifo/cramp-pub-sub-chat-demo/blob/master/app/actions/chat_action.rb
class ChatAction < Cramp::Websocket
use_fiber_pool
on_start :create_redis
on_finish :handle_leave, :destroy_redis
on_data :received_data
def create_redis
#redis = EM::Hiredis.connect('redis://127.0.0.1:6379/0')
end
def destroy_redis
#redis.pubsub.close_connection
#redis.close_connection
end
def received_data(data)
msg = parse_json(data)
case msg[:action]
when 'join'
handle_join(msg)
when 'message'
handle_message(msg)
else
# skip
end
end
def handle_join(msg)
#user = msg[:user]
subscribe
publish(:action => 'control', :user => #user, :message => 'joined the chat room')
end
def handle_leave
publish :action => 'control', :user => #user, :message => 'left the chat room'
end
def handle_message(msg)
publish(msg.merge(:user => #user))
# added only for inline sync tests
render_json(:action => 'message', :user => #user, :message => "this info should appear after published message")
end
private
def subscribe
#redis.pubsub.subscribe('chat') do |message|
render(message)
end
end
def publish(message)
#redis.publish('chat', encode_json(message))
end
def encode_json(obj)
Yajl::Encoder.encode(obj)
end
def parse_json(str)
Yajl::Parser.parse(str, :symbolize_keys => true)
end
def render_json(hash)
render encode_json(hash)
end
end
More about what i try to do is in handle_message method.
I try send messages to client in correct order. First publish message to all subscribers, second render some internal info only for current connected client.
For above code client receives:
{"action":"message","user":"user1","message":"this info should appear after published message"}
{"action":"message","message":"simple message","user":"user1"}
Its not synchronized, because of em-hiredis defferable responses, probably.
So I try to synchronized it this way:
def handle_message(msg)
EM::Synchrony.sync publish(msg.merge(:user => #user))
EM::Synchrony.next_tick do # if I comment this block messages order is still incorrect
render_json(:action => 'message', :user => #user, :message => "this info should appear after published message")
end
end
Now, client handle messages with correct order.
{"action":"message","message":"simple message","user":"user1"}
{"action":"message","user":"user1","message":"this info should appear after published message"}
My questions are:
When I comment EM::Synchrony.next_tick block, messages order is still incorrect. What meaning have EM::Synchrony.next_tick block in this example?
Is this good way to handle inline sync with Cramp or EventMachine ?
Is there a better, clearer way to handle it ?
Thank you!
I found solution of this problem, em-synchrony should work inline out of the box by requiring this library:
require 'em-synchrony/em-hiredis'
class ChatAction < Cramp::Websocket
Using EM::Synchrony.next_tick block is bad idea, with big help of em-synchrony community I add em-hiredis 0.2.1 compatibility patch on github
So now handle_message method looks like this:
def handle_message(msg)
publish(msg.merge(:user => #user))
render_json(:action => 'message', :user => #user, :message => "this info should appear after published message")
end
Don`t forget to take this gem from github
gem 'em-synchrony', :git=> 'git://github.com/igrigorik/em-synchrony.git'
Hope it helps someone.

state_machine using after_failure with ActiveRecord transactions

I'm using ruby state_machine to manage processing documents. As the document progresses the SM states manage the extra validations needed.
If any step fails, such as the document being the wrong dimensions, I want to record this so I can inform a user who is waiting for the conversion to complete.
My plan was to use SM after_failure to fill in a failed_at timestamp in the model. The polling user interface would see the processing has failed and display an appropriate message.
My issue is that if a transition fails it rolls back the transaction including the after_failure method. This means the user never finds out that the processing failed and unfortunately sits in front of their screen indefinitely.
In the example below I want the :fail_transaction method to run despite the transition failing. I'm open to any ideas or alternate ways to notify the user of failure.
Notes:
The :use_transaction => false option for state machine doesn't seem
to make a difference.
I considered putting the fail conversion into a worker queue to escape the transaction but as I'm using delayed_job so queue entries are also wound back.
This project is shamefully still on Rails 2.3.x & ruby 1.8.x
Example code:
state_machine :state, :initial => :new do
state :new
state :original_uploaded do
validates_presence_of :original_file
end
state :original_converted do
validates_presence_of :pdf
end
state :conversion_complete do
validates :pdf_dimensions_correct
end
event :process do
transition :new => :original_uploaded
transition :original_uploaded => :original_converted
transition :original_converted => :conversion_complete
end
after_transition :on => :process, :do => :process
before_transition any => :original_converted, :do => :convert_original_to_pdf
before_transition any => :conversion_complete, :do => :extract_pdf_metadata
after_failure :on => :process, :do => :fail_conversion
end
def fail_conversion
update_attribute(:failed_at, Time.now) # Should update dispite being invalid state
end
def convert_original_to_pdf
#...
end
def extract_pdf_metadata
#...
end
def pdf_dimensions_correct
errors.add(...)
end
Thanks in advance!

triggering intermediary transition between states in ruby state_machine

i am using https://github.com/pluginaweek/state_machine
my code is
event :set_running do
transition any => :runnning
end
event :restart do
transition :failed => :restarting
end
after_transition :failed => :restarting do |job,transition|
job.set_running
end
after_transition :restarting => :running do |job,transition|
job.restart_servers
=begin
this takes some time. and i would like job state to be "restarting" while
it's restarting servers. but it doesn't happen (i suppose because of transaction)
until after_transition :failed => :restarting callback is finished.
so it actually doesnt happen at all because this callback triggers => :running transition
=end
end
in other words i would like to run "restart" event once and trigger intermediary transition while it's transferring from :failed to :running.
can i do that somehow using state_machine?
http://rdoc.info/github/pluginaweek/state_machine/master/StateMachine/Integrations/ActiveRecord
It is now possible to disable transactions:
state_machine :initial => :parked, :use_transactions => false do

Resque worker gives out "NoMethodError: undefined method `perform`"

I have no idea what I have done here, but I have attempted to get one controller in Rails to queue a job onto Resque, which then a worker connects to and does the heavy lifting (I.E. comparisons, database entries).
However, the tasks are not even running, since there are no clear instructions for setting Resque up.
Copy and paste's below:
Also available in Gist format!
This is the exception line from Hoptoad:
NoMethodError: undefined method 'perform' for Violateq:Module
This is the contents of the "worker" file:
module Violateq
#queue = :violateq
def perform(nick, rulenumber)
# Working for the weekend!!!
puts "I got a nick of #{nick} and they broke #{rulenumber}"
#violation = Violation.new(nick, rulenumber)
puts "If you got this far, your OK"
log_in(:worker_log, {:action => "Violate d=perfom", :nick => nick, :rulenumber => rulenumber, :status => "success"})
#rescue => ex
# notify_hoptoad(ex)
# log_in(:worker_log, {:action => "Violate d=perfom", :nick => nick, :rulenumber => rulenumber, :status => "failure"})
end
end
This is the contents of the "web_controller" file:
class IncomingController < ApplicationController
require 'mail'
skip_before_filter :verify_authenticity_token
def create
message = Mail.new(params[:message])
# Push the message into the queue
Resque.enqueue(Violateq, message.from.to_s, message.subject.to_s)
log_in(:endpoint_log, {:action => "IncomingController d=create", :subject => message.subject, :message => message.body.decoded})
render :text => 'success', :status => 200 # a status of 404 would reject the mail
rescue => ex
notify_hoptoad(ex)
render :text => 'failure', :status => 500
end
end
Thank you very much for your time, and if you would like any more information, please do not hesitate to contact me,
Luke Carpenter
Fixed.
Changed def perform to def self.perform
Then it worked
Thanks,
Luke Carpenter

Resources