Module: Corn

Defined in:
lib/corn.rb,
lib/corn/rack.rb

Defined Under Namespace

Classes: Rack

Class Method Summary collapse

Class Method Details

.client_idObject



14
15
16
# File 'lib/corn.rb', line 14

def client_id
  ENV['CORN_CLIENT_ID']
end

.configured?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/corn.rb', line 18

def configured?
  !!(host && client_id)
end

.hostObject



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

def host
  ENV['CORN_HOST']
end

.log(msg) ⇒ Object



40
41
42
# File 'lib/corn.rb', line 40

def log(msg)
  $stderr.puts msg
end

.post(data, name) ⇒ Object



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

def post(data, name)
  url = URI.parse(submit_url)
  req = Net::HTTP::Post.new(url.path)
  req.set_form_data("data" => data, 'client_id' => client_id, 'name' => name)
  res = Net::HTTP.start(url.host, url.port) do |http|
    http.use_ssl = url.scheme == 'https'
    http.request(req)
  end
  log("Corn report submitted to #{submit_url}")
rescue Exception => e
  log("post to #{submit_url} failed: #{e.message}")
  log(e.backtrace.join("\n"))
end

.profiler(report_name, sampling_interval = 0.1, output_interval = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/corn.rb', line 22

def profiler(report_name, sampling_interval=0.1, output_interval=nil)
  if !configured?
    log("No CORN_CLIENT_ID or CORN_HOST configured, profiling data is not submitted")
    return
  end
  SamplingProf.new(sampling_interval, true) do |data|
    post(data, report_name)
  end.tap do |prof|
    if output_interval
      prof.output_interval = output_interval
    end
  end
end

.submit_urlObject



36
37
38
# File 'lib/corn.rb', line 36

def submit_url
  File.join(host, 'profile_data')
end