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.



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

def initialize(namespace)
  @namespace = namespace
end

Instance Method Details

#clean_upObject



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

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



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

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

#register(jid, max_jids) ⇒ Object



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

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

#remove(jid) ⇒ Object



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

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

#running_jidsObject



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

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