From b2b8558fdbb5669d4c59efe78275e5162580008b Mon Sep 17 00:00:00 2001 From: Mikhail Doronin Date: Mon, 23 Mar 2020 23:11:23 +0100 Subject: [PATCH] Do not connect to redis at exit if not needed (#4502) A fix for https://github.com/mperham/sidekiq/issues/4498 introduced a call-back that is executed at ruby VM exit and tries to connect to redis. This call-back is also executed in CI runs and in all other cases where sidekiq is loaded but not used with real redis. As a work-around, keep the at_exit hook but communicate to redis only if there is something to send, i.e. the number of processed or failed jobs is greater than zero Co-authored-by: Mikhail Doronin --- Changes.md | 1 + lib/sidekiq/launcher.rb | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Changes.md b/Changes.md index 3b122f4c..04c566a9 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ HEAD --------- +- Fix: Do not connect to redis at ruby vm exit when not needed. [#4502] - Remove Redis connection naming [#4479] 6.0.6 diff --git a/lib/sidekiq/launcher.rb b/lib/sidekiq/launcher.rb index 11f3d90d..2c7a95e9 100644 --- a/lib/sidekiq/launcher.rb +++ b/lib/sidekiq/launcher.rb @@ -100,15 +100,17 @@ module Sidekiq nowdate = Time.now.utc.strftime("%Y-%m-%d") fails = Processor::FAILURE.reset procd = Processor::PROCESSED.reset - Sidekiq.redis do |conn| - conn.pipelined do - conn.incrby("stat:processed", procd) - conn.incrby("stat:processed:#{nowdate}", procd) - conn.expire("stat:processed:#{nowdate}", STATS_TTL) + if fails + procd > 0 + Sidekiq.redis do |conn| + conn.pipelined do + conn.incrby("stat:processed", procd) + conn.incrby("stat:processed:#{nowdate}", procd) + conn.expire("stat:processed:#{nowdate}", STATS_TTL) - conn.incrby("stat:failed", fails) - conn.incrby("stat:failed:#{nowdate}", fails) - conn.expire("stat:failed:#{nowdate}", STATS_TTL) + conn.incrby("stat:failed", fails) + conn.incrby("stat:failed:#{nowdate}", fails) + conn.expire("stat:failed:#{nowdate}", STATS_TTL) + end end end end