Module: Dekiru::RakeMonitor

Defined in:
lib/dekiru/rake_monitor.rb

Instance Method Summary collapse

Instance Method Details

#api_keyObject



17
18
19
# File 'lib/dekiru/rake_monitor.rb', line 17

def api_key
  Dekiru.configure.monitor_api_key
end

#connObject



8
9
10
11
12
13
14
15
# File 'lib/dekiru/rake_monitor.rb', line 8

def conn
  @conn ||= Faraday.new(:url => 'https://job-mon.herokuapp.com') do |faraday|
    faraday.request  :url_encoded
    faraday.request  :json
    faraday.response :json
    faraday.adapter  Faraday.default_adapter
  end
end

#job_end(job_id) ⇒ Object



40
41
42
43
44
# File 'lib/dekiru/rake_monitor.rb', line 40

def job_end(job_id)
  if job_id
    response = conn.put "/api/apps/#{api_key}/jobs/#{job_id}/finished.json", nil
  end
end

#job_start(task, estimate_time) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dekiru/rake_monitor.rb', line 25

def job_start(task, estimate_time)
  body = {
    job: {
      name: task.name,
      end_time: Time.current.since(estimate_time),
      email: monitor_email
    }
  }
  response = conn.post "/api/apps/#{api_key}/jobs.json", body
  response.body["id"]
rescue => e
  Dekiru.configure.error_handle.call(e)
  nil
end

#monitor_emailObject



21
22
23
# File 'lib/dekiru/rake_monitor.rb', line 21

def monitor_email
  Dekiru.configure.monitor_email
end

#resolve_args(args) ⇒ Object



3
4
5
6
# File 'lib/dekiru/rake_monitor.rb', line 3

def resolve_args(args)
  estimate_time = args.first.delete(:estimate_time)
  [args, estimate_time]
end

#task_with_monitor(*args, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/dekiru/rake_monitor.rb', line 46

def task_with_monitor(*args, &block)
  args, estimate_time = resolve_args(args)
  task *args do |t|
    job_id = job_start(t, estimate_time)
    block.call
    job_end(job_id)
  end
end