Class: LimitedCapacity::JobTracker

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/workers/concerns/limited_capacity/job_tracker.rb

Overview

rubocop:disable Scalability/IdempotentWorker

Constant Summary collapse

LUA_REGISTER_SCRIPT =
<<~EOS
  local set_key, element, max_elements = KEYS[1], ARGV[1], ARGV[2]

  if redis.call("scard", set_key) < tonumber(max_elements) then
    redis.call("sadd", set_key, element)
    return true
  end

  return false
EOS

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ JobTracker

Returns a new instance of JobTracker.



18
19
20
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 18

def initialize(namespace)
  @namespace = namespace
end

Instance Method Details

#clean_upObject



34
35
36
37
38
39
40
41
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 34

def clean_up
  completed_jids = Gitlab::SidekiqStatus.completed_jids(running_jids)
  return unless completed_jids.any?

  with_redis do |redis|
    remove_job_keys(redis, completed_jids)
  end
end

#countObject



43
44
45
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 43

def count
  with_redis { |redis| redis.scard(counter_key) }
end

#register(jid, max_jids) ⇒ Object



22
23
24
25
26
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 22

def register(jid, max_jids)
  with_redis do |redis|
    redis.eval(LUA_REGISTER_SCRIPT, keys: [counter_key], argv: [jid.to_s, max_jids.to_i])
  end.present?
end

#remove(jid) ⇒ Object



28
29
30
31
32
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 28

def remove(jid)
  with_redis do |redis|
    remove_job_keys(redis, jid)
  end
end

#running_jidsObject



47
48
49
50
51
# File 'app/workers/concerns/limited_capacity/job_tracker.rb', line 47

def running_jids
  with_redis do |redis|
    redis.smembers(counter_key)
  end
end