Class: ZK::ZKEventMachine::Callback::Base
- Inherits:
-
Object
- Object
- ZK::ZKEventMachine::Callback::Base
- Includes:
- Deferred, Logging
- Defined in:
- lib/z_k/z_k_event_machine/callback.rb
Overview
Used by ZooKeeper to return an asynchronous result.
If callbacks or errbacks are set on the instance, they will be called with just the data returned from the call (much like their synchronous versions).
If a block was given to #new or #on_result, then that block is called with a ZK::Exceptions::KeeperException instance or nil, then the rest of the arguments defined for that callback type
the node-style and deferred-style results are NOT exclusive, so if you use both _you will be called with results in both formats_.
Direct Known Subclasses
ACLCallback, ChildrenCallback, DataCallback, SetCallback, StatCallback, StringCallback, VoidCallback
Class Method Summary collapse
-
.async_result_keys(*syms) ⇒ Object
set the result keys that should be used by node_style_result and to call the deferred_style_result blocks.
Instance Method Summary collapse
- #async_result_keys ⇒ Object protected
-
#call(hash) ⇒ Object
ZK will call this instance with a hash of data, which is the result of the asynchronous call.
-
#check_async_rc(hash) ⇒ Object
Checks the return code from the async call.
- #deferred_style_result(hash) ⇒ Object abstract
-
#exception_for(hash) ⇒ ZK::Exceptions::KeeperException?
Returns an instance of a sublcass ZK::Exceptions::KeeperException based on the asynchronous return_code.
-
#initialize(prok = nil, &block) ⇒ Base
constructor
A new instance of Base.
-
#node_style_result(hash) ⇒ Object
call the user block with the correct Exception class as the first arg (or nil if no error) and then the appropriate args for the type of asynchronous call.
-
#on_result(prok = nil, &block) ⇒ Object
register a block that should be called (node.js style) with the results.
-
#success?(hash) ⇒ true, false
returns true if the request was successful (if return_code was Zookeeper::ZOK).
Constructor Details
#initialize(prok = nil, &block) ⇒ Base
Returns a new instance of Base.
36 37 38 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 36 def initialize(prok=nil, &block) on_result(prok, &block) end |
Class Method Details
.async_result_keys(*syms) ⇒ Object
set the result keys that should be used by node_style_result and to call the deferred_style_result blocks
28 29 30 31 32 33 34 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 28 def self.async_result_keys(*syms) if syms.empty? @async_result_keys || [] else @async_result_keys = syms.map { |n| n.to_sym } end end |
Instance Method Details
#async_result_keys ⇒ Object (protected)
126 127 128 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 126 def async_result_keys self.class.async_result_keys end |
#call(hash) ⇒ Object
ZK will call this instance with a hash of data, which is the result of the asynchronous call. Depending on the style of callback in use, we take the appropriate actions
delegates to #deferred_style_result and #node_style_result
61 62 63 64 65 66 67 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 61 def call(hash) # logger.debug { "#{self.class.name}#call hash: #{hash.inspect}" } EM.schedule do deferred_style_result(hash) node_style_result(hash) end end |
#check_async_rc(hash) ⇒ Object
Checks the return code from the async call. If the return code was not ZOK, then fire the errbacks and do the node-style error call otherwise, does nothing
52 53 54 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 52 def check_async_rc(hash) call(hash) unless success?(hash) end |
#deferred_style_result(hash) ⇒ Object
should call set_deferred_status with the appropriate args for the result and type of call
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 99 def deferred_style_result(hash) # ensure this calls the callback on the reactor if success?(hash) vals = hash.values_at(*async_result_keys) # logger.debug { "#{self.class.name}#deferred_style_result async_result_keys: #{async_result_keys.inspect}, vals: #{vals.inspect}" } self.succeed(*hash.values_at(*async_result_keys)) else self.fail(exception_for(hash)) end end |
#exception_for(hash) ⇒ ZK::Exceptions::KeeperException?
Returns an instance of a sublcass ZK::Exceptions::KeeperException based on the asynchronous return_code.
facilitates using case statements for error handling
91 92 93 94 95 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 91 def exception_for(hash) return nil if success?(hash) return_code = hash.fetch(:rc) ZK::Exceptions::KeeperException.by_code(return_code).new end |
#node_style_result(hash) ⇒ Object
call the user block with the correct Exception class as the first arg (or nil if no error) and then the appropriate args for the type of asynchronous call
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 114 def node_style_result(hash) return unless @block vals = hash.values_at(*async_result_keys) # logger.debug { "#{self.class.name}#node_style_result async_result_keys: #{async_result_keys.inspect}, vals: #{vals.inspect}" } if exc = exception_for(hash) @block.call(exc) else @block.call(nil, *vals) end end |
#on_result(prok = nil, &block) ⇒ Object
replaces the block given to #new
register a block that should be called (node.js style) with the results
45 46 47 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 45 def on_result(prok=nil, &block) @block = (prok || block) end |
#success?(hash) ⇒ true, false
returns true if the request was successful (if return_code was Zookeeper::ZOK)
74 75 76 |
# File 'lib/z_k/z_k_event_machine/callback.rb', line 74 def success?(hash) hash[:rc] == Zookeeper::ZOK end |