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

.create_prof(sampling_interval, output) ⇒ Object



22
23
24
25
26
# File 'lib/corn.rb', line 22

def create_prof(sampling_interval, output)
  SamplingProf.new(sampling_interval).tap do |prof|
    prof.output_file = output if output
  end
end

.hostObject



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

def host
  ENV['CORN_HOST']
end

.log(msg) ⇒ Object



46
47
48
# File 'lib/corn.rb', line 46

def log(msg)
  $stderr.puts msg
end

.post(data, name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/corn.rb', line 56

def post(data, name)
  url = URI.parse(submit_url)
  req = Net::HTTP::Post::Multipart.new(url.path,
                                       "data" => UploadIO.new(data, 'text/plain', 'profile.txt'),
                                       '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("upload #{file} to #{submit_url} failed: #{e.message}")
  log(e.backtrace.join("\n"))
end

.start(output = nil, sampling_interval = 0.1) ⇒ Object



28
29
30
31
# File 'lib/corn.rb', line 28

def start(output=nil, sampling_interval=0.1)
  @prof ||= create_prof(sampling_interval, output)
  @prof.start
end

.submit(name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/corn.rb', line 33

def submit(name)
  @prof.stop
  if configured?
    upload(@prof.output_file, name)
  else
    log("No CORN_CLIENT_ID or CORN_HOST configured, profiling data is not submitted")
  end
end

.submit_urlObject



42
43
44
# File 'lib/corn.rb', line 42

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

.upload(file, name) ⇒ Object



50
51
52
53
54
# File 'lib/corn.rb', line 50

def upload(file, name)
  File.open(file) do |f|
    post(f, name)
  end
end