Class: 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.



23
24
25
26
27
28
29
30
# File 'lib/gorgon/listener.rb', line 23

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
end

Instance Method Details

#at_exit_hookObject



94
95
96
# File 'lib/gorgon/listener.rb', line 94

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

#connectObject



40
41
42
43
# File 'lib/gorgon/listener.rb', line 40

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

#handle_request(json_payload) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gorgon/listener.rb', line 64

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



45
46
47
48
49
# File 'lib/gorgon/listener.rb', line 45

def initialize_personal_job_queue
  @job_queue = @bunny.queue("", :exclusive => true)
  exchange = @bunny.exchange("gorgon.jobs", :type => :fanout)
  @job_queue.bind(exchange)
end

#listenObject



32
33
34
35
36
37
38
# File 'lib/gorgon/listener.rb', line 32

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

#pollObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gorgon/listener.rb', line 51

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gorgon/listener.rb', line 77

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

  @callback_handler = CallbackHandler.new(@job_definition.callbacks)
  copy_source_tree(@job_definition.source_tree_path, @job_definition.sync_exclude)

  if !@syncer.success? || !run_after_sync
    clean_up
    return
  end

  fork_worker_manager

  clean_up
end