15
15
require "sidekiq/job_logger"
16
16
require "sidekiq/logging"
17
17
require "sidekiq/worker"
18
- elsif Sidekiq ::VERSION . to_i > 5
18
+ elsif Sidekiq ::VERSION . to_i == 6
19
19
require "sidekiq/exception_handler"
20
20
require "sidekiq/job_logger"
21
21
require "sidekiq/worker"
22
+ else
23
+ require "sidekiq/config"
24
+ require "sidekiq/job_logger"
25
+ require "sidekiq/worker"
22
26
end
23
27
24
28
module Sidekiq
25
29
if Sidekiq ::VERSION . to_i > 4
26
30
# Let Semantic Logger handle duration logging
27
31
class JobLogger
28
32
def call ( item , queue )
29
- klass = item [ "wrapped" ] || item [ "class" ]
33
+ klass = item [ "wrapped" ] || item [ "class" ]
30
34
metric = "Sidekiq/#{ klass } /perform" if klass
31
35
logger = klass ? SemanticLogger [ klass ] : Sidekiq . logger
32
36
logger . info ( "Start #perform" )
33
37
logger . measure_info (
34
38
"Completed #perform" ,
35
39
on_exception_level : :error ,
36
- log_exception : :full ,
37
- metric : metric
40
+ log_exception : :full ,
41
+ metric : metric
38
42
) do
39
43
yield
40
44
end
@@ -50,6 +54,16 @@ def prepare(job_hash, &block)
50
54
SemanticLogger . tagged ( job_hash_context ( job_hash ) , &block )
51
55
end
52
56
end
57
+
58
+ def job_hash_context ( job_hash )
59
+ h = {
60
+ class : job_hash [ "display_class" ] || job_hash [ "wrapped" ] || job_hash [ "class" ] ,
61
+ jid : job_hash [ "jid" ]
62
+ }
63
+ h [ :bid ] = job_hash [ "bid" ] if job_hash [ "bid" ]
64
+ h [ :tags ] = job_hash [ "tags" ] if job_hash [ "tags" ]
65
+ h
66
+ end
53
67
end
54
68
end
55
69
@@ -67,20 +81,31 @@ def self.job_hash_context(job_hash)
67
81
event
68
82
end
69
83
end
70
- end
71
84
72
- # Exception is already logged by Semantic Logger during the perform call
73
- module ExceptionHandler
74
- class Logger
75
- def call ( ex , ctx )
76
- Sidekiq . logger . warn ( ctx ) if !ctx . empty?
85
+ # Exception is already logged by Semantic Logger during the perform call
86
+ module ExceptionHandler
87
+ class Logger
88
+ def call ( ex , ctx )
89
+ Sidekiq . logger . warn ( ctx ) if !ctx . empty?
90
+ end
77
91
end
78
92
end
79
93
end
80
94
81
- module Worker
82
- # Logging within each worker should use its own logger
83
- if Sidekiq ::VERSION . to_i == 4
95
+ if Sidekiq ::VERSION . to_i >= 7
96
+ module Config
97
+ remove_const :ERROR_HANDLER
98
+
99
+ # Exception is already logged by Semantic Logger during the perform call
100
+ ERROR_HANDLER = -> ( ex , ctx , cfg = Sidekiq . default_configuration ) {
101
+ cfg . logger . warn ( ctx ) unless ctx . empty?
102
+ }
103
+ end
104
+ end
105
+
106
+ # Logging within each worker should use its own logger
107
+ if Sidekiq ::VERSION . to_i == 4
108
+ module Worker
84
109
def self . included ( base )
85
110
raise ArgumentError , "You cannot include Sidekiq::Worker in an ActiveJob: #{ base . name } " if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
86
111
@@ -90,7 +115,9 @@ def self.included(base)
90
115
base . class_attribute :sidekiq_retry_in_block
91
116
base . class_attribute :sidekiq_retries_exhausted_block
92
117
end
93
- elsif Sidekiq ::VERSION . to_i == 5
118
+ end
119
+ elsif Sidekiq ::VERSION . to_i == 5
120
+ module Worker
94
121
def self . included ( base )
95
122
raise ArgumentError , "You cannot include Sidekiq::Worker in an ActiveJob: #{ base . name } " if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
96
123
@@ -100,10 +127,22 @@ def self.included(base)
100
127
base . sidekiq_class_attribute :sidekiq_retry_in_block
101
128
base . sidekiq_class_attribute :sidekiq_retries_exhausted_block
102
129
end
103
- elsif Sidekiq ::VERSION . to_i > 5
130
+ end
131
+ elsif Sidekiq ::VERSION . to_i == 6
132
+ module Worker
104
133
def self . included ( base )
105
134
raise ArgumentError , "Sidekiq::Worker cannot be included in an ActiveJob: #{ base . name } " if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
106
135
136
+ base . include ( Options )
137
+ base . extend ( ClassMethods )
138
+ base . include ( SemanticLogger ::Loggable )
139
+ end
140
+ end
141
+ else
142
+ module Job
143
+ def self . included ( base )
144
+ raise ArgumentError , "Sidekiq::Job cannot be included in an ActiveJob: #{ base . name } " if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
145
+
107
146
base . include ( Options )
108
147
base . extend ( ClassMethods )
109
148
base . include ( SemanticLogger ::Loggable )
@@ -130,8 +169,8 @@ def call(worker, item, queue)
130
169
worker . logger . measure_info (
131
170
"Completed #perform" ,
132
171
on_exception_level : :error ,
133
- log_exception : :full ,
134
- metric : "Sidekiq/#{ worker . class . name } /perform"
172
+ log_exception : :full ,
173
+ metric : "Sidekiq/#{ worker . class . name } /perform"
135
174
) do
136
175
yield
137
176
end
0 commit comments