Class: Gorgon::Originator

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/gorgon/originator.rb

Constant Summary collapse

SPEC_SUCCESS_EXIT_STATUS =
0
SPEC_FAILURE_EXIT_STATUS =
1
SYNC_ERROR_EXIT_STATUS =
2
ERROR_EXIT_STATUS =
3

Instance Method Summary collapse

Methods included from Configuration

#load_configuration_from_file

Constructor Details

#initializeOriginator

Returns a new instance of Originator.



28
29
30
# File 'lib/gorgon/originator.rb', line 28

def initialize
  @configuration = nil
end

Instance Method Details

#callback_handlerObject



106
107
108
# File 'lib/gorgon/originator.rb', line 106

def callback_handler
  @callback_handler ||= CallbackHandler.new(configuration[:job][:callbacks])
end

#cancel_jobObject



52
53
54
# File 'lib/gorgon/originator.rb', line 52

def cancel_job
  ShutdownManager.new(protocol: @protocol, job_state: @job_state).cancel_job
end

#cleanup_if_job_completeObject



123
124
125
126
127
128
# File 'lib/gorgon/originator.rb', line 123

def cleanup_if_job_complete
  if @job_state.is_job_complete?
    @logger.log "Job is done"
    @protocol.disconnect
  end
end

#connection_informationObject



178
179
180
# File 'lib/gorgon/originator.rb', line 178

def connection_information
  configuration[:connection]
end

#create_job_state_and_observersObject



166
167
168
169
170
171
172
# File 'lib/gorgon/originator.rb', line 166

def create_job_state_and_observers
  @job_state = JobState.new files.count
  RuntimeRecorder.new @job_state, configuration[:runtime_file]
  @progress_bar_view = ProgressBarView.new @job_state
  @progress_bar_view.show
  FailuresPrinter.new(configuration, @job_state)
end

#ctrl_cObject



56
57
58
59
# File 'lib/gorgon/originator.rb', line 56

def ctrl_c
  puts "\nCtrl-C received! Just wait a moment while I clean up..."
  cancel_job
end

#filesObject



182
183
184
# File 'lib/gorgon/originator.rb', line 182

def files
  @files ||= RuntimeFileReader.new(configuration).sorted_files
end

#handle_new_listener_notification(payload) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/gorgon/originator.rb', line 155

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

  if payload[:listener_queue_name]
    @protocol.publish_job_to_one(job_definition, payload[:listener_queue_name])
  else
    puts "Received unexpected payload on originator queue"
    ap payload
  end
end

#handle_reply(payload) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gorgon/originator.rb', line 130

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

  # at some point this will probably need to be fancy polymorphic type based responses, or at least a nice switch statement
  if payload[:action] == "finish"
    @job_state.file_finished payload
  elsif payload[:action] == "start"
    @job_state.file_started payload
  elsif payload[:type] == "crash"
    @job_state.gorgon_crash_message payload
  elsif payload[:type] == "exception"
    # TODO
    ap payload
  else
    ap payload
  end

  @logger.log_message payload
  # Uncomment this to see each message received by originator
  # ap payload

  cleanup_if_job_complete
  exit_status(payload)
end

#job_definitionObject



186
187
188
189
190
191
192
# File 'lib/gorgon/originator.rb', line 186

def job_definition
  # TODO: remove duplication. Use sync_configuration
  job_config = configuration[:job]
  job_config[:sync] = {} unless job_config.has_key?(:sync)
  job_config[:sync][:source_tree_path] = source_tree_path(job_config[:sync])
  JobDefinition.new(configuration[:job])
end

#on_disconnectObject



174
175
176
# File 'lib/gorgon/originator.rb', line 174

def on_disconnect
  EventMachine.stop
end

#originateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gorgon/originator.rb', line 32

def originate
  begin
    Signal.trap("INT") { ctrl_c }
    Signal.trap("TERM") { ctrl_c }

    exit_status = publish
    @logger.log "Originator finished successfully"
    exit_status
  rescue StandardError
    $stderr.puts "Unhandled exception in originator:"
    $stderr.puts $!.message
    $stderr.puts $!.backtrace.join("\n")
    $stderr.puts "----------------------------------"
    $stderr.puts "Now attempting to cancel the job."
    @logger.log_error "Unhandled Exception!" if @logger
    cancel_job
    exit ERROR_EXIT_STATUS
  end
end

#publishObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gorgon/originator.rb', line 61

def publish
  exit_status = SPEC_SUCCESS_EXIT_STATUS

  @logger = OriginatorLogger.new configuration[:originator_log_file]

  if files.empty?
    $stderr.puts "There are no files to test! Quitting."
    exit ERROR_EXIT_STATUS
  end

  cluster_id = callback_handler.before_originate

  push_source_code

  @protocol = OriginatorProtocol.new(@logger, cluster_id)

  EventMachine.run do
    publish_files_and_job

    @protocol.receive_payloads do |payload|
      exit_status |= handle_reply(payload)
    end

    @protocol.receive_new_listener_notifications do |payload|
      handle_new_listener_notification(payload)
    end
  end

  callback_handler.after_job_finishes
  exit_status
end

#publish_files_and_jobObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gorgon/originator.rb', line 93

def publish_files_and_job
  @logger.log "Connecting..."
  @protocol.connect connection_information, :on_closed => method(:on_disconnect)

  @logger.log "Publishing files..."
  @protocol.publish_files files
  create_job_state_and_observers

  @logger.log "Publishing Job..."
  @protocol.publish_job_to_all job_definition
  @logger.log "Job Published"
end

#push_source_codeObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gorgon/originator.rb', line 110

def push_source_code
  syncer = SourceTreeSyncer.new(sync_configuration)
  execution_context = syncer.push
  if execution_context.success
    @logger.log "Command '#{execution_context.command}' completed successfully."
  else
    $stderr.puts "Command '#{execution_context.command}' failed!"
    $stderr.puts "Stdout:\n#{execution_context.output}"
    $stderr.puts "Stderr:\n#{execution_context.errors}"
    exit SYNC_ERROR_EXIT_STATUS
  end
end