Class: Sidekiq::Ultimate::EmptyQueues

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sidekiq/ultimate/empty_queues.rb,
lib/sidekiq/ultimate/empty_queues/refresh_timer_task.rb

Overview

Maintains a cache of empty queues. It has a global cache and a local cache. The global cache is stored in redis and updated periodically. The local cache is updated either by using the fresh cache fetched after global cache update or by using existing global cache. Only one process can update the global cache at a time.

Defined Under Namespace

Classes: RefreshTimerTask

Constant Summary collapse

LOCK_KEY =
"ultimate:empty_queues_updater:lock"
LAST_RUN_KEY =
"ultimate:empty_queues_updater:last_run"
KEY =
"ultimate:empty_queues"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmptyQueues

Returns a new instance of EmptyQueues.



25
26
27
28
29
30
# File 'lib/sidekiq/ultimate/empty_queues.rb', line 25

def initialize
  @local_lock = Mutex.new
  @queues = []

  super
end

Instance Attribute Details

#local_lockObject (readonly)

Returns the value of attribute local_lock.



23
24
25
# File 'lib/sidekiq/ultimate/empty_queues.rb', line 23

def local_lock
  @local_lock
end

#queuesObject (readonly)

Returns the value of attribute queues.



23
24
25
# File 'lib/sidekiq/ultimate/empty_queues.rb', line 23

def queues
  @queues
end

Class Method Details

.setup!Object

Sets up automatic empty queues cache updater. It will call #refresh! every ‘Sidekiq::Ultimate::Configuration.instance.empty_queues_cache_refresh_interval_sec` seconds



35
36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq/ultimate/empty_queues.rb', line 35

def self.setup!
  refresher_timer_task = nil

  Sidekiq.on(:startup) do
    refresher_timer_task&.shutdown
    refresher_timer_task = RefreshTimerTask.setup!(self)
  end

  Sidekiq.on(:shutdown) { refresher_timer_task&.shutdown }
end

Instance Method Details

#refresh!Boolean

Attempts to update the global cache of empty queues by first acquiring a global lock If the lock is acquired, it brute force generates an accurate list of currently empty queues and then writes the updated list to the global cache The local queue cache is always updated as a result of this operation, either by using the recently generated list or fetching the most recent list from the global cache

Returns:

  • (Boolean)

    true if local cache was updated



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sidekiq/ultimate/empty_queues.rb', line 53

def refresh!
  return false unless local_lock.try_lock

  begin
    refresh_global_cache! || refresh_local_cache
  ensure
    local_lock.unlock
  end
rescue => e
  Sidekiq.logger.error { "Empty queues cache update failed: #{e}" }
  raise
end