简体   繁体   English

如何使用Rails上的transitions gem处理事件机器中的错误?

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

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. 我正在Rails 4.2上的事件机(转换gem)上工作,我编写了一个名为send_data的方法,当状态从暂挂更改为交付时,将触发send_data

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. SendLineItemsToWebshop是另一个类,它调用call方法并等待一些响应,如果响应来了,则事件将被触发(状态将被更改),否则状态将相同。

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. 但是问题在于,调用事件deliver时,它不检查send_data的响应是否到来,而是更改状态。 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. 但我希望如果响应为false或nil,则过渡状态将不会更改。 State will be changed only when response comes true. 仅当响应为真时,状态才会更改。 Please help me about this issue. 请帮我解决这个问题。

The Transitions gem has a nice guard feature. Transitions宝石具有良好的guard功能。 Add a simple logical test like can_be_delivered? 添加一个简单的逻辑测试,如can_be_delivered? and try this: 并尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM