Sha256: 898b8bd714f741a2340f6000d17ba16fedb89a06dd9f819684f078414bcaec7b

Contents?: true

Size: 1.33 KB

Versions: 15

Compression:

Stored size: 1.33 KB

Contents

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

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, options={})
      reverse = options[:reverse]
      reraise = options[:reraise]

      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 })
          raise ex if reraise
        end
      end
      arr.clear
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
sidekiq-5.2.10 lib/sidekiq/util.rb
sidekiq-5.2.9 lib/sidekiq/util.rb
sidekiq-5.2.8 lib/sidekiq/util.rb
sidekiq_cleaner-5.3.8 lib/sidekiq/util.rb
sidekiq_cleaner-5.3.6 lib/sidekiq/util.rb
sidekiq-5.2.7 lib/sidekiq/util.rb
sidekiq-5.2.6 lib/sidekiq/util.rb
sidekiq-5.2.5 lib/sidekiq/util.rb
sidekiq-5.2.4 lib/sidekiq/util.rb
sidekiq-5.2.3 lib/sidekiq/util.rb
sidekiq-5.2.2 lib/sidekiq/util.rb
sidekiq-5.2.1 lib/sidekiq/util.rb
sidekiq-5.2.0 lib/sidekiq/util.rb
sidekiq-5.1.3 lib/sidekiq/util.rb
sidekiq-5.1.2 lib/sidekiq/util.rb