Sha256: 4d932f1f0a5604051e84c1fdd276ea81c977851d59d32c62fa9a265250aa1697

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "redis"

class RailsTransactionalOutbox
  class HealthCheck
    KEY_PREFIX = "__rails_transactional__outbox_worker__running__"
    VALUE = "OK"
    private_constant :KEY_PREFIX, :VALUE

    def self.check(redis_url: ENV.fetch("REDIS_URL", nil), hostname: ENV.fetch("HOSTNAME", nil),
      expiry_time_in_seconds: 120)
      new(redis_url: redis_url, hostname: hostname, expiry_time_in_seconds: expiry_time_in_seconds).check
    end

    attr_reader :redis_client, :hostname, :expiry_time_in_seconds

    def initialize(redis_url: ENV.fetch("REDIS_URL", nil), hostname: ENV.fetch("HOSTNAME", nil),
      expiry_time_in_seconds: 120)
      @redis_client = Redis.new(url: redis_url)
      @hostname = hostname
      @expiry_time_in_seconds = expiry_time_in_seconds
    end

    def check
      value = redis_client.get(key)
      if value == VALUE
        ""
      else
        "[Rails Transactional Outbox Worker - expected #{VALUE} under #{key}, found: #{value}] "
      end
    end

    def register_heartbeat
      redis_client.set(key, VALUE, ex: expiry_time_in_seconds)
    end

    def worker_stopped
      redis_client.del(key)
    end

    private

    def key
      "#{KEY_PREFIX}#{hostname}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails-transactional-outbox-0.2.1 lib/rails_transactional_outbox/health_check.rb
rails-transactional-outbox-0.2.0 lib/rails_transactional_outbox/health_check.rb
rails-transactional-outbox-0.1.0 lib/rails_transactional_outbox/health_check.rb