Class: TorqueBox::Messaging::FutureResponder
- Inherits:
-
Object
- Object
- TorqueBox::Messaging::FutureResponder
- Defined in:
- lib/torquebox/messaging/future_responder.rb
Overview
A FutureResponder encapsulates sending the results of a long running process to a Future.
Class Method Summary collapse
-
.current ⇒ Object
Convenience method that returns the thread local responder.
-
.status=(status) ⇒ Object
Convenience method that allows you to send a status message via the FutureResponder.current responder.
Instance Method Summary collapse
-
#error=(error) ⇒ Object
Signal that an error occurred during processing.
-
#initialize(response_queue, correlation_id, message_ttl = nil) ⇒ FutureResponder
constructor
A new instance of FutureResponder.
-
#respond ⇒ Object
Handles started/complete/error for you around the given block.
-
#result=(result) ⇒ Object
Signal that processing has completed.
-
#started ⇒ Object
Signal that processing has started.
-
#status=(status) ⇒ Object
Report the current status back to the client.
Constructor Details
#initialize(response_queue, correlation_id, message_ttl = nil) ⇒ FutureResponder
Returns a new instance of FutureResponder.
31 32 33 34 35 |
# File 'lib/torquebox/messaging/future_responder.rb', line 31 def initialize(response_queue, correlation_id, = nil) @queue = response_queue @correlation_id = correlation_id @message_ttl = || 600_000 end |
Class Method Details
.current ⇒ Object
Convenience method that returns the thread local responder. Only valid inside a block passed to #respond.
80 81 82 |
# File 'lib/torquebox/messaging/future_responder.rb', line 80 def self.current Thread.current[:future_responder] end |
Instance Method Details
#error=(error) ⇒ Object
Signal that an error occurred during processing.
60 61 62 63 |
# File 'lib/torquebox/messaging/future_responder.rb', line 60 def error=(error) @error = error send_response :high end |
#respond ⇒ Object
Handles started/complete/error for you around the given block. The current responder is avaiable inside the block via current, which is useful for sending #status messages.
68 69 70 71 72 73 74 75 76 |
# File 'lib/torquebox/messaging/future_responder.rb', line 68 def respond started Thread.current[:future_responder] = self self.result = yield rescue Exception => e self.error = e $stderr.puts "FutureResponder#respond: An error occured: ", e $stderr.puts e.backtrace.join( "\n" ) end |
#result=(result) ⇒ Object
Signal that processing has completed.
52 53 54 55 56 |
# File 'lib/torquebox/messaging/future_responder.rb', line 52 def result=(result) @result_set = true @result = result send_response :high end |
#started ⇒ Object
Signal that processing has started.
38 39 40 |
# File 'lib/torquebox/messaging/future_responder.rb', line 38 def started send_response(:low) end |
#status=(status) ⇒ Object
Report the current status back to the client. The status value is application specific.
44 45 46 47 48 |
# File 'lib/torquebox/messaging/future_responder.rb', line 44 def status=(status) @status_set = true @status = status send_response end |