Class: OyenCov::Background

Inherits:
Object
  • Object
show all
Defined in:
lib/oyencov/background.rb

Class Method Summary collapse

Class Method Details

.startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/oyencov/background.rb', line 26

def self.start
  OyenCov::Logger.log(<<~TXT)
    Env: #{@config.mode}
    $PROGRAM_NAME: #{$PROGRAM_NAME || "nil"}
    @process_type: #{@config.process_type}
    Env vars set: #{ENV.keys.grep(/^OYENCOV_/)}
  TXT

  # Start `Coverage` as soon as possible before other codes are loaded
  CoveragePeekDelta.start

  # This thread is for production reporting only.
  @thread = Thread.new {
    # Check with backend to get parameters
    sleep(3)

    if @config.mode == "production"
      clearance = @api_conn.get_data_submission_clearance

      if clearance.nil?
        OyenCov::Logger.log "Unable to obtain oyencov submission clearance. Stopping OyenCov background thread."
        Thread.stop
      end

      # OyenCov::Logger.log("clearance.body:-\n" + clearance.body)
    end

    @config.mode == "production" && loop do
      sleep(@loop_interval + 3 - rand(6))
      new_method_hits = CoveragePeekDelta.snapshot_delta
      OyenCov::Logger.log "ControllerTracking.hits = #{ControllerTracking.hits}"
      new_controller_hits = ControllerTracking.snapshot_and_reset!

      runtime_report = {
        controller_action_hits: new_controller_hits,
        method_hits: new_method_hits
      }

      unless runtime_report.values.any?(&:any?)
        OyenCov::Logger.log("All #{runtime_report.keys.join(", ")} are empty. Skipping submission.")
        next
      end

      runtime_report[:git_commit_sha] = @config.release
      runtime_report[:process_type] = @config.process_type

      response = @api_conn.post_runtime_report(runtime_report)

      OyenCov::Logger.log("POST-ing runtime_report: #{runtime_report.to_json}")

      if response && response.body["status"] == "ok"
        OyenCov::Logger.log "POST runtime_report ok."
      else
        OyenCov::Logger.log "POST runtime_report failed. Stopping background thread."
        Thread.stop
      end

      # TODO: Set new interval & wiggle.
    end # loop
  }

  @thread.run

  nil
end

.stopObject

If production/staging etc, we can exit without further processing. For ‘test`, persist controller report.



94
95
96
# File 'lib/oyencov/background.rb', line 94

def self.stop
  @thread.stop
end