Class: DeployAgent::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_agent/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Agent

Returns a new instance of Agent.



9
10
11
12
# File 'lib/deploy_agent/agent.rb', line 9

def initialize(options = {})
  @retries = 0
  @options = options
end

Instance Method Details

#loggerObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/deploy_agent/agent.rb', line 48

def logger
  @logger ||= begin
    if $background
      logger = Logger.new(LOG_PATH, 5, 10240)
    else
      logger = Logger.new(STDOUT)
    end
    logger.level = @options[:verbose] ? Logger::DEBUG : Logger::INFO
    logger
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deploy_agent/agent.rb', line 14

def run
  nio_selector = NIO::Selector.new
  timers = Timers::Group.new
  target = ENV['DEPLOY_AGENT_PROXY_IP'] || 'agent.deployhq.com'
  server_connection = ServerConnection.new(self, target, nio_selector, !ENV['DEPLOY_AGENT_NOVERIFY'])
  timers.every(60) { server_connection.keepalive }
  loop do
    wait_interval = timers.wait_interval
    wait_interval = 0 if wait_interval < 0
    nio_selector.select(wait_interval) do |monitor|
      monitor.value.rx_data if monitor.readable?
      monitor.value.tx_data if monitor.writeable?
    end
    timers.fire

    @retries = 0
  end
rescue OpenSSL::SSL::SSLError => e
  @retries += 1

  if @retries == 4
    raise e
  else
    retry
  end
rescue ServerConnection::ServerDisconnected
  retry
rescue Interrupt, SignalException => e
  logger.info("Stopping")
rescue Exception => e
  logger.debug("#{e.class}: #{e.message}")
  raise e
end