Class: Sonar::Connector::SonarPushConnector

Inherits:
Base
  • Object
show all
Defined in:
lib/sonar_push_connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



10
11
12
# File 'lib/sonar_push_connector.rb', line 10

def batch_size
  @batch_size
end

#connector_credentialsObject (readonly)

Returns the value of attribute connector_credentials.



11
12
13
# File 'lib/sonar_push_connector.rb', line 11

def connector_credentials
  @connector_credentials
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/sonar_push_connector.rb', line 9

def uri
  @uri
end

Instance Method Details

#actionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sonar_push_connector.rb', line 27

def action
  source_connectors.each {|c| c.connector_filestore.flip(:complete, filestore, :working) }
  
  begin
    count = filestore.process_batch(@batch_size, :working) do |files|
      paths = files.map{|f| filestore.file_path(:working, f)}
      begin
        log.debug"pushing #{files.length} files"
        push_batch(paths)
      rescue Exception => e
        log.warn ["caught an exception : leaving files in working area", 
                  e.class.to_s, e.message, e.backtrace].join('\n')
        raise Sonar::Connector::FileStore::LeaveInSourceArea, e.message
      end
    end
  end while count>0
end

#parse(settings) ⇒ Object

Raises:

  • (Sonar::Connector::InvalidConfig)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sonar_push_connector.rb', line 13

def parse(settings)
  @uri = settings["uri"]
  raise Sonar::Connector::InvalidConfig.new("Connector '#{name}': parameter 'uri' required.") if @uri.blank?

  @connector_credentials = settings["connector_credentials"]
  raise Sonar::Connector::InvalidConfig.new("Connector '#{name}': parameter 'connector_credentials' required") if @connector_credentials.blank?
  
  # ensure that there's a source connector to pull data from
  raise Sonar::Connector::InvalidConfig.new("Connector '#{name}': parameter 'source_connectors' required.") if settings["source_connectors"].blank? || settings["source_connectors"].empty?
  

  @batch_size = settings["batch_size"] || 50
end

#push_batch(files) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sonar_push_connector.rb', line 45

def push_batch(files)
  params = {"messages" => files.map{|file| JSON.parse(File.read file)}.to_json,
    "connector_credentials" => connector_credentials}
  res = Net::HTTP.post_form URI.parse(uri), params
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    log.info "pushed #{files.size} messages to #{uri}"
  else
    res.error!
  end
end