How to handle error in event machine using transitions gem on rails?

165 views Asked by At

I am working on event machine (transitions gem) in rails 4.2 , I wrote a method named send_data and when state changed from pending to deliver, the send_data will be fired.

def send_data
    data = { 'remote_id' => order.remote_id,
             'items' => order.line_items
           }
    SendLineItemsToWebshop.call(data)        
  end

SendLineItemsToWebshop is an another class which calls call method and waiting for some response, if response come , then event will be fired(state will be changed) , otherwise, state will be same.

require 'bunny'
require 'thread'
class SendLineItemsToWebshop
  def self.call(data)
    conn = Bunny.new(automatically_recover: false)
    conn.start
    channel = conn.create_channel
    response = call_client(channel, data)
    channel.queue(ENV['BLISS_RPC_QUEUE_NAME']).pop
    channel.close
    conn.close
    self.response(response)
  end

  def self.call_client(channel, data)
    client = RpcClient.new(channel, ENV['BLISS_RPC_QUEUE_NAME'])
    client.call(data)
  end

  def self.response(response)
    return response
    JSON.parse response
  end
end

But problem is that when event deliver is called, it does not check send_data's response comes or not, it changes the state. Here is me deliver event:

event :deliver do
      transitions :to => :delivered, :from => [:editing, :pending] , on_transition: [ :send_data ]
    end

But I want that if response is false or nil, transition state will not be changed. State will be changed only when response comes true. Please help me about this issue.

1

There are 1 answers

0
Mike K On BEST ANSWER

The Transitions gem has a nice guard feature. Add a simple logical test like can_be_delivered? and try this:

event :deliver do
  transitions :to => :delivered, :from => [:editing, :pending], guard: [:can_be_delivered?]
end