Sha256: 068b13ffa050081a7d94191afd3dd860d1df97469d8de74f83213362315fbdc6

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Diffend
  # Track what is run in production
  class Track
    # Time that we want to wait between track requests
    TRACK_SLEEP = 15
    # Time that we want to wait before we retry
    RETRY_SLEEP = 15

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

    # Start tracking
    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

    # @param request_id [String]
    def perform(request_id)
      loop do
        @mutex.synchronize do
          track_request(request_id)
        end

        sleep(TRACK_SLEEP)
      end
    end

    # Perform an exec request
    def exec_request
      Diffend::Execute.call(Diffend::Commands::EXEC, @config)
    end

    # Perform a track request
    #
    # @param request_id [String]
    def track_request(request_id)
      Diffend::Request.call(
        build_request_object(request_id)
      )
    end

    # @param request_id [String]
    #
    # @return [Diffend::RequestObject]
    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

    # @param project_id [String] diffend project_id
    # @param request_id [String]
    #
    # @return [String]
    def track_url(project_id, request_id)
      "https://my.diffend.io/api/projects/#{project_id}/bundle/#{request_id}/track"
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
diffend-monitor-0.2.31 lib/diffend/track.rb
diffend-0.2.31 lib/diffend/track.rb
diffend-monitor-0.2.30 lib/diffend/track.rb
diffend-0.2.30 lib/diffend/track.rb