From a96314bdaea0e1bff93a507a94d7665415019181 Mon Sep 17 00:00:00 2001 From: Daria Mayorova Date: Thu, 10 Oct 2024 12:03:56 +0200 Subject: [PATCH] Some more tweaks --- .../synchronization/nowait_lock_service.rb | 2 ++ .../synchronization/unsafe_unlock_service.rb | 2 ++ curl-output.txt | 0 lib/tasks/sidekiq/cleanup.rake | 7 ++++--- {app/lib => lib}/three_scale/patterns/service.rb | 0 lib/three_scale/sidekiq_batch_cleanup_service.rb | 14 ++++++++++++-- 6 files changed, 20 insertions(+), 5 deletions(-) delete mode 100644 curl-output.txt rename {app/lib => lib}/three_scale/patterns/service.rb (100%) diff --git a/app/services/synchronization/nowait_lock_service.rb b/app/services/synchronization/nowait_lock_service.rb index a0174faf82..ef0241ed96 100644 --- a/app/services/synchronization/nowait_lock_service.rb +++ b/app/services/synchronization/nowait_lock_service.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'three_scale/patterns/service' + class Synchronization::NowaitLockService < ThreeScale::Patterns::Service # @param str_resource [String] a lock key # @param timeout [Integer] milliseconds lock timeout diff --git a/app/services/synchronization/unsafe_unlock_service.rb b/app/services/synchronization/unsafe_unlock_service.rb index 6c0a4547d1..45117a5218 100644 --- a/app/services/synchronization/unsafe_unlock_service.rb +++ b/app/services/synchronization/unsafe_unlock_service.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'three_scale/patterns/service' + class Synchronization::UnsafeUnlockService < ThreeScale::Patterns::Service # unconditional lock release, dangerous to create race conditions based on a redlock key # should only be used for manual intervention in case of extraordinary circumstances diff --git a/curl-output.txt b/curl-output.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/lib/tasks/sidekiq/cleanup.rake b/lib/tasks/sidekiq/cleanup.rake index a2cba08e55..a053c480f7 100644 --- a/lib/tasks/sidekiq/cleanup.rake +++ b/lib/tasks/sidekiq/cleanup.rake @@ -5,10 +5,11 @@ require 'three_scale/sidekiq_batch_cleanup_service' namespace :sidekiq do desc 'cleanup BID-* keys from sidekiq-batch, specify the max age in seconds as an argument' task :cleanup_batches, [:max_age_seconds] => :environment do |task, args| - max_age_seconds = Integer(args[:max_age_seconds]) + params = args[:max_age_seconds] ? { max_age_seconds: Integer(args[:max_age_seconds]) } : {} - Rails.logger.info "Cleaning up BID-* keys older than #{max_age_seconds.seconds.in_hours} hours from sidekiq-batch..." + message = params[:max_age_seconds] ? "#{params[:max_age_seconds].seconds.in_hours} hours" : "the default age" + puts "Cleaning up the sidekiq-batch BID-* keys older than #{message}" - ThreeScale::SidekiqBatchCleanupService.call(max_age_seconds: max_age_seconds) + ThreeScale::SidekiqBatchCleanupService.call(**params) end end diff --git a/app/lib/three_scale/patterns/service.rb b/lib/three_scale/patterns/service.rb similarity index 100% rename from app/lib/three_scale/patterns/service.rb rename to lib/three_scale/patterns/service.rb diff --git a/lib/three_scale/sidekiq_batch_cleanup_service.rb b/lib/three_scale/sidekiq_batch_cleanup_service.rb index 7e98b38680..e85c285637 100644 --- a/lib/three_scale/sidekiq_batch_cleanup_service.rb +++ b/lib/three_scale/sidekiq_batch_cleanup_service.rb @@ -1,6 +1,6 @@ require 'progress_counter' -require Rails.root.join('app/lib/three_scale/patterns/service') +require 'three_scale/patterns/service' module ThreeScale class SidekiqBatchCleanupService < ThreeScale::Patterns::Service @@ -22,7 +22,7 @@ def initialize(max_age_seconds: DEFAULT_MAX_AGE_SECONDS) def call total = redis.dbsize - Rails.logger.info "Total number of keys: #{total}, will delete keys with TTL less than #{bid_max_ttl.seconds.in_hours} hours" + logger.info "Total number of keys: #{total}, will delete BID-* keys with TTL less than #{bid_max_ttl.seconds.in_hours} hours" scan_enum = System.redis.scan_each(match: 'BID-*', type: 'hash', count: MAX_FETCH_COUNT) @@ -50,5 +50,15 @@ def each_with_progress_counter(enumerable, count) progress.call end end + + # This logger just prints out a message to STDOUT, with new line before and after. + # New line before is to make progress log look better + def logger + @logger ||= begin + log = ActiveSupport::Logger.new($stdout) + log.formatter = ->(_, _, _, msg) { "\n#{msg.is_a?(String) ? msg : msg.inspect}\n" } + log + end + end end end