class Smith::Commands::Stop

Public Instance Methods

execute() click to toggle source
# File lib/smith/commands/agency/stop.rb, line 10
def execute
  if target.first == 'agency' || target.first == 'all'
    send("stop_#{target.first}") { |ret| responder.succeed(ret) }
  else
    stop_agent { |ret| responder.succeed(ret) }
  end
end

Private Instance Methods

format_error_message(errors) click to toggle source
# File lib/smith/commands/agency/stop.rb, line 87
def format_error_message(errors)
  errors = errors.compact
  case errors.size
  when 0
    ''
  when 1
    "Agent does not exist: #{errors.first}"
  else
    "Agents do not exist: #{errors.join(", ")}"
  end
end
options_spec() click to toggle source
# File lib/smith/commands/agency/stop.rb, line 99
def options_spec
  banner "Stop an agent/agents.", "<uuid[s]>"

  opt    :group, "Stop everything in the specified group", :type => :string, :short => :g
  opt    :name,  "Stop an agent(s) by name", :type => :string, :short => :n
  opt    :force, "If stopping the agency and there are agents running stop anyway"
end
stop_agency(&blk) click to toggle source
# File lib/smith/commands/agency/stop.rb, line 20
def stop_agency(&blk)
  running_agents = agents.state(:running)
  if running_agents.empty?
    logger.info { "Agency shutting down." }
     blk.call('')
    Smith.stop
  else
    if options[:force]
      blk.call('')
      Smith.stop
    else
      logger.warn { "Agents are still running: #{running_agents.map(&:name).join(", ")}." }
      logger.info { "Agency not shutting down. Use --force if you really want to shut it down." }
      blk.call("Not shutting down, agents are still running: #{running_agents.map(&:name).join(", ")}.")
    end
  end
end
stop_agent(&blk) click to toggle source
# File lib/smith/commands/agency/stop.rb, line 45
def stop_agent(&blk)

  #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  #!!!!!!!!!!!! See note about target at end of this file !!!!!!!!!!!!!!
  #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  # Sort out any groups. If the group option is set it will override
  # any other specified agents.
  if options[:group]
    begin
      agents_to_stop = agents.find_by_name(agent_group(options[:group])).map(&:uuid)
      if agents_to_stop.empty?
        blk.call("Empty group: #{options[:group]}. No agents stopped")
      end
    rescue RuntimeError => e
      return blk.call(e.message)
    end
  else
    if options[:name]
      agents_to_stop = agents.find_by_name(options[:name]).map(&:uuid)
    else
      agents_to_stop = target
    end
  end

  errors = agents_to_stop.inject([]) { |acc,uuid| acc << stop_if_running(uuid) }
  blk.call(format_error_message(errors))
end
stop_all(&blk) click to toggle source
# File lib/smith/commands/agency/stop.rb, line 38
def stop_all(&blk)
  agents.state(:running).each do |agent|
    agent.stop
  end
  blk.call('')
end
stop_if_running(uuid) click to toggle source
# File lib/smith/commands/agency/stop.rb, line 74
def stop_if_running(uuid)
  agent = agents[uuid]
  if agent
    if agent.running?
      agent.stop
      nil
    end
  else
    logger.warn { "Agent does not exist: #{uuid}" }
    uuid
  end
end