lib/gitlab_exporter/sidekiq.rb in gitlab-exporter-13.3.0 vs lib/gitlab_exporter/sidekiq.rb in gitlab-exporter-13.3.1
- old
+ new
@@ -21,10 +21,13 @@
# This timeout is configured to higher interval than scrapping
# of Prometheus to ensure that connection is kept instead of
# needed to be re-initialized
POOL_TIMEOUT = 90
+ # Lock for Sidekiq.redis which we need to modify, but is not concurrency safe.
+ SIDEKIQ_REDIS_LOCK = Mutex.new
+
PrometheusMetrics.describe("sidekiq_enqueued_jobs",
"Total number of jobs enqueued by class name. Only inspects the first #{PROBE_JOBS_LIMIT} jobs per queue.") # rubocop:disable Layout/LineLength
def self.connection_pool
@@connection_pool ||= Hash.new do |h, connection_hash| # rubocop:disable Style/ClassVars
@@ -209,19 +212,18 @@
@metrics.add("sidekiq_to_be_retried_jobs", count, **labels.merge({ name: class_name }))
end
end
def with_sidekiq(namespaced = true)
- # TODO: this is not concurrent safe as we change global context
- # It means that we are unable to use many different sidekiq's
- # which is not a problem as of now
- Sidekiq.configure_client do |config|
- config.redis = self.class.connection_pool[redis_options(namespaced)]
- end
+ SIDEKIQ_REDIS_LOCK.synchronize {
+ Sidekiq.configure_client do |config|
+ config.redis = self.class.connection_pool[redis_options(namespaced)]
+ end
- return unless connected?
+ return unless connected?
- yield
+ yield
+ }
end
def redis_options(namespaced = true)
options = {
url: @opts[:redis_url],