Class: Diffend::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/diffend/track.rb

Overview

Track what is run in production

Constant Summary collapse

TRACK_SLEEP =

Time that we want to wait between track requests

15
RETRY_SLEEP =

Time that we want to wait before we retry

15

Instance Method Summary collapse

Constructor Details

#initializeTrack

Initialize tracking



12
13
14
15
# File 'lib/diffend/track.rb', line 12

def initialize
  @mutex = Mutex.new
  @config = Diffend::Config.call
end

Instance Method Details

#build_request_object(request_id) ⇒ Diffend::RequestObject



68
69
70
71
72
73
74
75
# File 'lib/diffend/track.rb', line 68

def build_request_object(request_id)
  Diffend::RequestObject.new(
    config: @config,
    url: track_url(@config.project_id, request_id),
    payload: { id: request_id }.freeze,
    request_method: :put
  ).freeze
end

#exec_requestObject

Perform an exec request



52
53
54
# File 'lib/diffend/track.rb', line 52

def exec_request
  Diffend::Execute.call(Diffend::Commands::EXEC, @config)
end

#perform(request_id) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/diffend/track.rb', line 41

def perform(request_id)
  loop do
    @mutex.synchronize do
      track_request(request_id)
    end

    sleep(TRACK_SLEEP)
  end
end

#startObject

Start tracking



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/diffend/track.rb', line 18

def start
  response = exec_request

  perform(response['id'])
rescue Diffend::Errors::HandledException
  sleep(RETRY_SLEEP)

  retry
rescue StandardError => e
  Diffend::HandleErrors::Report.call(
    exception: e,
    config: @config,
    message: :unhandled_exception,
    report: true,
    raise_exception: false
  )

  sleep(RETRY_SLEEP)

  retry
end

#track_request(request_id) ⇒ Object

Perform a track request



59
60
61
62
63
# File 'lib/diffend/track.rb', line 59

def track_request(request_id)
  Diffend::Request.call(
    build_request_object(request_id)
  )
end

#track_url(project_id, request_id) ⇒ String



81
82
83
# File 'lib/diffend/track.rb', line 81

def track_url(project_id, request_id)
  "https://my.diffend.io/api/projects/#{project_id}/bundle/#{request_id}/track"
end