Module: Cognizant::Commands
- Defined in:
- lib/cognizant/commands.rb,
lib/cognizant/commands/use.rb,
lib/cognizant/commands/help.rb,
lib/cognizant/commands/load.rb,
lib/cognizant/commands/status.rb,
lib/cognizant/commands/actions.rb,
lib/cognizant/commands/shutdown.rb
Constant Summary collapse
- @@commands =
{}
Class Method Summary collapse
- .command(name, description = nil, &block) ⇒ Object
- .command_descriptions ⇒ Object
- .format_process_or_group_status(output_processes) ⇒ Object
- .generate_response(connection, command) ⇒ Object
- .process_command(connection, command) ⇒ Object
-
.processes_for_name_or_group(app, args) ⇒ Object
command(‘reload’, ‘Reload Cognizant’) do |connection, _| # TODO: make reload actually work (command socket reopening is # an issue).
- .run_command(connection, request, command, command_name) ⇒ Object
- .send_message(connection, response) ⇒ Object
- .send_process_or_group_status(app, args = []) ⇒ Object
- .unrecognized_command(connection, request) ⇒ Object
Class Method Details
.command(name, description = nil, &block) ⇒ Object
9 10 11 |
# File 'lib/cognizant/commands.rb', line 9 def self.command(name, description = nil, &block) @@commands[name] = { :description => description, :block => block } end |
.command_descriptions ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/cognizant/commands.rb', line 61 def self.command_descriptions command_specs = @@commands.select do |_, spec| spec[:description] end.sort_by {|name, _| name} command_specs.map do |name, spec| "#{name}: #{spec[:description]}" end.join("\n") end |
.format_process_or_group_status(output_processes) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cognizant/commands.rb', line 140 def self.format_process_or_group_status(output_processes) output = [] output_processes.each do |process| pid = process.cached_pid output << { "Process" => process.name, "PID" => pid, "Group" => process.group, "State" => process.state, "Since" => process.last_transition_time, "% CPU" => Cognizant::System.cpu_usage(pid).to_f, "Memory" => Cognizant::System.memory_usage(pid).to_f # in KBs. } end output end |
.generate_response(connection, command) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cognizant/commands.rb', line 31 def self.generate_response(connection, command) begin request = Cognizant::Client::Transport.(command) rescue ArgumentError => e return { 'message' => "Could not parse command: #{e}" } end unless command_name = request['command'] return { 'message' => 'No "command" parameter provided; not sure what you want me to do.' } end run_command(connection, request, command, command_name) end |
.process_command(connection, command) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/cognizant/commands.rb', line 13 def self.process_command(connection, command) response = generate_response(connection, command) if !response.nil? (connection, response) else Log[self].debug("Got back nil response, so not responding to command.") end # connection.close_connection_after_writing end |
.processes_for_name_or_group(app, args) ⇒ Object
command(‘reload’, ‘Reload Cognizant’) do |connection, _|
# TODO: make reload actually work (command socket reopening is
# an issue). Would also be nice if user got a confirmation that
# the reload completed, though that's not strictly necessary.
# In the normal case, this will do a write
# synchronously. Otherwise, the bytes will be stuck into the
# buffer and lost upon reload.
(connection, 'Reloading, as commanded')
Cognizant.reload
# Reload should not return
raise "Not reachable"
end
118 119 120 121 122 123 124 125 126 |
# File 'lib/cognizant/commands.rb', line 118 def self.processes_for_name_or_group(app, args) processes = [] if app.to_s.size > 0 Cognizant::Controller.daemon.applications[app.to_sym].processes.values.each do |process| processes << process if args.include?(process.name) or args.include?(process.group) end end processes end |
.run_command(connection, request, command, command_name) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cognizant/commands.rb', line 45 def self.run_command(connection, request, command, command_name) if command_spec = @@commands[command_name] # Log[self].debug("Received command: #{command.inspect}") begin return command_spec[:block].call(connection, request) rescue StandardError => e msg = "Error while processing command #{command_name.inspect}: #{e} (#{e.class})\n #{e.backtrace.join("\n ")}" Log[self].error(msg) return msg end else Log[self].debug("Received unrecognized command: #{command.inspect}") return unrecognized_command(connection, request) end end |
.send_message(connection, response) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/cognizant/commands.rb', line 23 def self.(connection, response) if response.kind_of?(String) response = { 'message' => response } end = Cognizant::Client::Transport.(response) connection.send_data() end |
.send_process_or_group_status(app, args = []) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cognizant/commands.rb', line 128 def self.send_process_or_group_status(app, args = []) output_processes = [] if args.size > 0 output_processes = processes_for_name_or_group(app, args) return %Q{No such process or group: #{args.join(', ')}} if output_processes.size == 0 else output_processes = Cognizant::Controller.daemon.applications[app.to_sym].processes.values end format_process_or_group_status(output_processes) end |
.unrecognized_command(connection, request) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/cognizant/commands.rb', line 71 def self.unrecognized_command(connection, request) <<EOF Unrecognized command: #{request['command'].inspect} #{command_descriptions} EOF end |