Class: Sonar::Connector::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/sonar_connector/consumer.rb

Overview

Listens to thread message queue and processes messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, base_config) ⇒ Consumer

Returns a new instance of Consumer.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sonar_connector/consumer.rb', line 33

def initialize(controller, base_config)
  @controller = controller
  @base_config = base_config
  
  # Consumer holds the status object because changes 
  # to status should be centrally moderated.
  @status = Sonar::Connector::Status.new(@base_config)
  
  # Creat logger and inherit the logger settings from the base controller config
  @log_file = File.join(base_config.log_dir, "consumer.log")
  @log = Sonar::Connector::Utils.stdout_logger base_config
  
  @run = true
end

Instance Attribute Details

#base_configObject (readonly)

Returns the value of attribute base_config.



26
27
28
# File 'lib/sonar_connector/consumer.rb', line 26

def base_config
  @base_config
end

#controllerObject (readonly)

Returns the value of attribute controller.



28
29
30
# File 'lib/sonar_connector/consumer.rb', line 28

def controller
  @controller
end

#logObject (readonly)

Returns the value of attribute log.



30
31
32
# File 'lib/sonar_connector/consumer.rb', line 30

def log
  @log
end

#queueObject (readonly)

Returns the value of attribute queue.



27
28
29
# File 'lib/sonar_connector/consumer.rb', line 27

def queue
  @queue
end

#runObject (readonly)

Returns the value of attribute run.



31
32
33
# File 'lib/sonar_connector/consumer.rb', line 31

def run
  @run
end

#statusObject (readonly)

Returns the value of attribute status.



29
30
31
# File 'lib/sonar_connector/consumer.rb', line 29

def status
  @status
end

Instance Method Details

#cleanupObject



60
61
62
63
# File 'lib/sonar_connector/consumer.rb', line 60

def cleanup
  log.info "Shut down consumer"
  log.close
end

#prepare(queue) ⇒ Object

It’s kinda evil to be passing in the controller here. The better option is to refactor the consumer to be part of the controller.



50
51
52
53
# File 'lib/sonar_connector/consumer.rb', line 50

def prepare(queue)
  @queue = queue
  switch_to_log_file
end

#run_onceObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sonar_connector/consumer.rb', line 81

def run_once
  begin
    command = queue.pop
    command.execute ExecutionContext.new(:log=>log, :status=>status, :controller=>controller)
  rescue ThreadTerminator => e
    raise
  rescue Exception => e
    log.error ["Command #{command.class} raised an unhandled exception: ",
               e.class.to_s, e.message, *e.backtrace].join('\n')
  end
end

#switch_to_log_fileObject



55
56
57
58
# File 'lib/sonar_connector/consumer.rb', line 55

def switch_to_log_file
  FileUtils.mkdir_p(base_config.log_dir) unless File.directory?(base_config.log_dir)
  @log = Sonar::Connector::Utils.disk_logger(@log_file, base_config)
end

#watchObject

Main loop to watch the command queue and process commands.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sonar_connector/consumer.rb', line 67

def watch
  while run
    begin
      run_once
    rescue ThreadTerminator
      break
    end
  end
  
  @run = false
  cleanup
  true
end