sidekiq 6.4.2 → 6.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq might be problematic. Click here for more details.

data/lib/sidekiq/util.rb DELETED
@@ -1,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
- require "socket"
5
- require "securerandom"
6
- require "sidekiq/exception_handler"
7
-
8
- module Sidekiq
9
- ##
10
- # This module is part of Sidekiq core and not intended for extensions.
11
- #
12
-
13
- class RingBuffer
14
- include Enumerable
15
- extend Forwardable
16
- def_delegators :@buf, :[], :each, :size
17
-
18
- def initialize(size, default = 0)
19
- @size = size
20
- @buf = Array.new(size, default)
21
- @index = 0
22
- end
23
-
24
- def <<(element)
25
- @buf[@index % @size] = element
26
- @index += 1
27
- element
28
- end
29
-
30
- def buffer
31
- @buf
32
- end
33
-
34
- def reset(default = 0)
35
- @buf.fill(default)
36
- end
37
- end
38
-
39
- module Util
40
- include ExceptionHandler
41
-
42
- # hack for quicker development / testing environment #2774
43
- PAUSE_TIME = $stdout.tty? ? 0.1 : 0.5
44
-
45
- # Wait for the orblock to be true or the deadline passed.
46
- def wait_for(deadline, &condblock)
47
- remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
48
- while remaining > PAUSE_TIME
49
- return if condblock.call
50
- sleep PAUSE_TIME
51
- remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
52
- end
53
- end
54
-
55
- def watchdog(last_words)
56
- yield
57
- rescue Exception => ex
58
- handle_exception(ex, {context: last_words})
59
- raise ex
60
- end
61
-
62
- def safe_thread(name, &block)
63
- Thread.new do
64
- Thread.current.name = name
65
- watchdog(name, &block)
66
- end
67
- end
68
-
69
- def logger
70
- Sidekiq.logger
71
- end
72
-
73
- def redis(&block)
74
- Sidekiq.redis(&block)
75
- end
76
-
77
- def tid
78
- Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
79
- end
80
-
81
- def hostname
82
- ENV["DYNO"] || Socket.gethostname
83
- end
84
-
85
- def process_nonce
86
- @@process_nonce ||= SecureRandom.hex(6)
87
- end
88
-
89
- def identity
90
- @@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
91
- end
92
-
93
- def fire_event(event, options = {})
94
- reverse = options[:reverse]
95
- reraise = options[:reraise]
96
-
97
- arr = Sidekiq.options[:lifecycle_events][event]
98
- arr.reverse! if reverse
99
- arr.each do |block|
100
- block.call
101
- rescue => ex
102
- handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
103
- raise ex if reraise
104
- end
105
- arr.clear
106
- end
107
- end
108
- end