Sha256: 3806cc284b7735e29608c9ff20073e56bd8245c3033bde32cf65c269a8eebb2d

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
require 'debug'

module Infield
  module Heartbeat
    module Runner
      class << self
        attr_reader :thread

        def run(interval: 60)
          @thread = Thread.new do
            loop do
              if DeprecationWarning::Runner.thread&.alive?
                send_heartbeat
              else
                stop
                break
              end
              sleep(interval)
            end
          end
        end

        def stop
          @thread&.kill
        end

        private

        def send_heartbeat
          uri = URI.parse(Infield.infield_api_url)
          Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
            http.post('/api/heartbeats',
                    {
                      repo_environment_id: Infield.repo_environment_id,
                      environment: Infield.environment,
                    }.to_json,
                    {
                      'Content-Type' => 'application/json',
                      'Authorization' => "bearer #{Infield.api_key}"
                    })
          end
        rescue *DeprecationWarning::Runner::HTTP_ERRORS => e
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
infield-0.2.0 lib/infield/heartbeat.rb