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 = fetch_config
end

Instance Method Details

#build_request_object(request_id) ⇒ Diffend::RequestObject

Parameters:

  • request_id (String)

Returns:



76
77
78
79
80
81
82
83
# File 'lib/diffend/track.rb', line 76

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
55
56
57
58
59
60
61
62
# File 'lib/diffend/track.rb', line 52

def exec_request
  Diffend::Voting.call(
    Diffend::Commands::EXEC,
    @config,
    Diffend::BuildBundlerDefinition.call(
      Diffend::Commands::EXEC,
      Bundler.default_gemfile,
      Bundler.default_lockfile
    )
  )
end

#fetch_configOpenStruct?

Fetch diffend config file

Returns:

  • (OpenStruct, nil)

    configuration object

Raises:



90
91
92
93
94
# File 'lib/diffend/track.rb', line 90

def fetch_config
  Config::Fetcher.call(
    File.expand_path('..', Bundler.bin_path)
  )
end

#perform(request_id) ⇒ Object

Parameters:

  • request_id (String)


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

Parameters:

  • request_id (String)


67
68
69
70
71
# File 'lib/diffend/track.rb', line 67

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

#track_url(project_id, request_id) ⇒ String

Parameters:

  • project_id (String)

    diffend project_id

  • request_id (String)

Returns:

  • (String)


100
101
102
# File 'lib/diffend/track.rb', line 100

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