lib/arborist/manager/tree_api.rb in arborist-0.0.1.pre20160606141735 vs lib/arborist/manager/tree_api.rb in arborist-0.0.1.pre20160829140603

- old
+ new

@@ -6,11 +6,12 @@ require 'rbczmq' require 'arborist/manager' unless defined?( Arborist::Manager ) class Arborist::Manager::TreeAPI < ZMQ::Handler - extend Loggability + extend Loggability, + Arborist::MethodUtilities # Loggability API -- log to the arborist logger log_to :arborist @@ -18,14 +19,20 @@ ### Create the TreeAPI handler that will read requests from the specified +pollable+ ### and call into the +manager+ to respond to them. def initialize( pollable, manager ) self.log.debug "Setting up a %p" % [ self.class ] @pollitem = pollable + @enabled = true @manager = manager end + ## + # True if the Tree API is accepting commands + attr_predicate :enabled + + ### ZMQ::Handler API -- Read and handle an incoming request. def on_readable request = self.recv response = self.handle_request( request ) self.send( response ) @@ -34,10 +41,12 @@ ### Handle the specified +raw_request+ and return a response. def handle_request( raw_request ) self.log.debug "Handling request: %p" % [ raw_request ] + raise "Manager is shutting down" unless self.enabled? + header, body = self.parse_request( raw_request ) return self.dispatch_request( header, body ) rescue => err self.log.error "%p: %s" % [ err.class, err.message ] @@ -278,9 +287,15 @@ self.log.debug "Modifying operational attributes of the %s node: %p" % [ identifier, body ] node.modify( body ) return successful_response( nil ) + end + + + ### Disable the API, returning errors for any future requests. + def shutdown + @enabled = false end end # class Arborist::Manager::TreeAPI