Sha256: 7a66fed5540001b99a95c2f33e336b140ba3d0531b9715ad53cb8544ad42d65a

Contents?: true

Size: 1.04 KB

Versions: 16

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module SidekiqAlive
  class Worker
    include Sidekiq::Worker
    sidekiq_options retry: false

    def perform(_hostname = SidekiqAlive.hostname)
      # Checks if custom liveness probe passes should fail or return false
      return unless config.custom_liveness_probe.call

      # Writes the liveness in Redis
      write_living_probe
      # schedules next living probe
      self.class.perform_in(config.time_to_live / 2, current_hostname)
    end

    def hostname_registered?(hostname)
      SidekiqAlive.registered_instances.any? do |ri|
        /#{hostname}/ =~ ri
      end
    end

    def write_living_probe
      # Write liveness probe
      SidekiqAlive.store_alive_key
      # Increment ttl for current registered instance
      SidekiqAlive.register_current_instance
      # after callbacks
      begin
        config.callback.call
      rescue StandardError
        nil
      end
    end

    def current_hostname
      SidekiqAlive.hostname
    end

    def config
      SidekiqAlive.config
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
sidekiq_alive-2.1.9 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.8 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.7 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.6 lib/sidekiq_alive/worker.rb
sidekiq-alive-next-3.1.1 lib/sidekiq_alive/worker.rb
sidekiq-alive-next-3.1.0 lib/sidekiq_alive/worker.rb
sidekiq-alive-next-3.0.0 lib/sidekiq_alive/worker.rb
sidekiq-alive-next-2.2.1 lib/sidekiq_alive/worker.rb
sidekiq-alive-next-2.2.0 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.5 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.4 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.3 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.2 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.1 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.1.0 lib/sidekiq_alive/worker.rb
sidekiq_alive-2.0.6 lib/sidekiq_alive/worker.rb