class Pantry::Command
Commands
are where the task-specific functionality is implemented, the core of how Pantry
works. All Commands
are required to implement the perform
method, which receives the Pantry::Message
requesting the Command
.
All commands must be registered with Pantry
before they will be available for execution. Use Pantry.add_client_command
or Pantry.add_server_command
to register the Command
.
Attributes
Public Class Methods
Expose this Command
to the CLI
and configure the options and information that this Command
needs from the CLI
to function.
See OptParsePlus
for documentation
# File lib/pantry/command.rb, line 16 def command(name, &block) @command_name = name @command_config = block end
The Type of this command, used to differentiate Messages. Defaults to the full scope of the name, though with the special
- case of removing any “Pantry” related scoping such as
Pantry
- and
Pantry::Commands
- and
-
This value must be unique across the system or the messages will not be processed reliably.
Override this for a custom name.
# File lib/pantry/command.rb, line 134 def self.message_type self.name.gsub(/Pantry::Commands::/, '').gsub(/Pantry::/, '') end
Public Instance Methods
Notify all listeners that this command has completed its tasks
# File lib/pantry/command.rb, line 111 def finished completion_future.signal(OpenStruct.new(:value => nil)) end
Is this command finished?
# File lib/pantry/command.rb, line 116 def finished? completion_future.ready? end
Set up the Message
that needs to be created to send this Command
to the server to be processed. Used by the CLI
. This method is given the ClientFilter of which clients should respond to this message (if any) and the extra arguments given on the command line.
If work needs to be done prior to the network communication for CLI
use, override method to take care of that logic.
The message returned here is then passed through the network to the appropriate recipients (Clients, Server
, or both) and used to trigger perform
on said recipient.
# File lib/pantry/command.rb, line 41 def prepare_message(options) to_message end
When a message comes back from the server as a response to or because of this command’s perform
, the command object on the CLI
will receive that message here. This method will dispatch to either receive_server_response
or receive_client_response
depending on the type of Command
run. In most cases, Commands
should override the server/client specific receivers. Only override this method to fully customize Message
response handling.
# File lib/pantry/command.rb, line 58 def receive_response(response) @response_tracker ||= TrackResponses.new @response_tracker.new_response(response) if @response_tracker.from_server? receive_server_response(response) finished elsif @response_tracker.from_client? receive_client_response(response) finished if @response_tracker.all_response_received? end end
Send a request out, returning the Future which will eventually contain the response Message
# File lib/pantry/command.rb, line 87 def send_request(message) @server_or_client.send_request(message) end
Send a request out and wait for the response. Will return the response once it is received.
This is a blocking call.
# File lib/pantry/command.rb, line 95 def send_request!(message) send_request(message).value end
Get the server or client object handling this command
# File lib/pantry/command.rb, line 148 def server_or_client @server_or_client end
Set a link back to the Server
or Client
that’s handling this command. This will be set by the CommandHandler
before calling perform
.
# File lib/pantry/command.rb, line 141 def server_or_client=(server_or_client) @server_or_client = server_or_client end
Blocking call that returns when the command has completed Can be given a timeout (in seconds) to wait for a response
# File lib/pantry/command.rb, line 106 def wait_for_finish(timeout = nil) completion_future.value(timeout) end
Protected Instance Methods
# File lib/pantry/command.rb, line 120 def completion_future @completion_future ||= Celluloid::Future.new end