Class: Gorgon::Listener

Inherits:
Object
  • Object
show all
Includes:
Configuration, CrashReporter, GLogger
Defined in:
lib/gorgon/listener.rb

Constant Summary

Constants included from CrashReporter

CrashReporter::OUTPUT_LINES_TO_REPORT

Constants included from GLogger

GLogger::SIZE_1_MB

Instance Method Summary collapse

Methods included from CrashReporter

#report_crash, #send_crash_message

Methods included from GLogger

#initialize_logger, #log, #log_error

Methods included from Configuration

#load_configuration_from_file

Constructor Details

#initializeListener

Returns a new instance of Listener.



25
26
27
28
29
30
31
32
33
# File 'lib/gorgon/listener.rb', line 25

def initialize
  @listener_config_filename = Dir.pwd + "/gorgon_listener.json"
  initialize_logger configuration[:log_file]

  log "Listener #{Gorgon::VERSION} initializing"
  connect
  initialize_personal_job_queue
  announce_readiness_to_originators
end

Instance Method Details

#announce_readiness_to_originatorsObject



54
55
56
57
58
# File 'lib/gorgon/listener.rb', line 54

def announce_readiness_to_originators
  exchange = @bunny.exchange(originator_exchange_name, :type => :fanout)
  data = {:listener_queue_name => @job_queue.name}
  exchange.publish(Yajl::Encoder.encode(data))
end

#at_exit_hookObject



102
103
104
# File 'lib/gorgon/listener.rb', line 102

def at_exit_hook
  at_exit { log "Listener will exit!"}
end

#connectObject



43
44
45
46
# File 'lib/gorgon/listener.rb', line 43

def connect
  @bunny = GorgonBunny.new(connection_information)
  @bunny.start
end

#handle_request(json_payload) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gorgon/listener.rb', line 73

def handle_request json_payload
  payload = Yajl::Parser.new(:symbolize_keys => true).parse(json_payload)

  case payload[:type]
  when "job_definition"
    run_job(payload)
  when "ping"
    respond_to_ping payload[:reply_exchange_name]
  when "gem_command"
    GemCommandHandler.new(@bunny).handle payload, configuration
  end
end

#initialize_personal_job_queueObject



48
49
50
51
52
# File 'lib/gorgon/listener.rb', line 48

def initialize_personal_job_queue
  @job_queue = @bunny.queue("job_queue_" + UUIDTools::UUID.timestamp_create.to_s, :auto_delete => true)
  exchange = @bunny.exchange(job_exchange_name, :type => :fanout)
  @job_queue.bind(exchange)
end

#listenObject



35
36
37
38
39
40
41
# File 'lib/gorgon/listener.rb', line 35

def listen
  at_exit_hook
  log "Waiting for jobs..."
  while true
    sleep 2 unless poll
  end
end

#pollObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gorgon/listener.rb', line 60

def poll
  message = @job_queue.pop
  return false if message == [nil, nil, nil]
  log "Received: #{message}"

  payload = message[2]

  handle_request payload

  log "Waiting for more jobs..."
  return true
end

#run_job(payload) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gorgon/listener.rb', line 86

def run_job(payload)
  @job_definition = JobDefinition.new(payload)
  @reply_exchange = @bunny.exchange(@job_definition.reply_exchange_name, :auto_delete => true)

  syncer = SourceTreeSyncer.new(@job_definition.sync)
  syncer.pull do |execution_context|
    if execution_context.success
      log "Command '#{execution_context.command}' completed successfully."
      fork_worker_manager if run_after_sync?
    else

      log_and_send_crash_message(execution_context.command, execution_context.output, execution_context.errors)
    end
  end
end