Sha256: 27c1cf95b49bf95a559c446b3b357b46536806ba533d7e790bdba39ad5f725b8

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
require 'socket'
require 'securerandom'
require 'sidekiq/exception_handler'
require 'sidekiq/core_ext'

module Sidekiq
  ##
  # This module is part of Sidekiq core and not intended for extensions.
  #
  module Util
    include ExceptionHandler

    EXPIRY = 60 * 60 * 24

    def watchdog(last_words)
      yield
    rescue Exception => ex
      handle_exception(ex, { context: last_words })
      raise ex
    end

    def safe_thread(name, &block)
      Thread.new do
        Thread.current['sidekiq_label'] = name
        watchdog(name, &block)
      end
    end

    def logger
      Sidekiq.logger
    end

    def redis(&block)
      Sidekiq.redis(&block)
    end

    def hostname
      ENV['DYNO'] || Socket.gethostname
    end

    def process_nonce
      @@process_nonce ||= SecureRandom.hex(6)
    end

    def identity
      @@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
    end

    def fire_event(event, reverse=false)
      arr = Sidekiq.options[:lifecycle_events][event]
      arr.reverse! if reverse
      arr.each do |block|
        begin
          block.call
        rescue => ex
          handle_exception(ex, { context: "Exception during Sidekiq lifecycle event.", event: event })
        end
      end
      arr.clear
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sidekiq-5.0.0 lib/sidekiq/util.rb
sidekiq-5.0.0.rc1 lib/sidekiq/util.rb
sidekiq-5.0.0.beta3 lib/sidekiq/util.rb
sidekiq-4.2.10 lib/sidekiq/util.rb
sidekiq-5.0.0.beta2 lib/sidekiq/util.rb
sidekiq-5.0.0.beta1 lib/sidekiq/util.rb
sidekiq-4.2.9 lib/sidekiq/util.rb
sidekiq-4.2.8 lib/sidekiq/util.rb