Module: Gitlab::SidekiqStatus
- Defined in:
- lib/gitlab/sidekiq_status.rb,
lib/gitlab/sidekiq_status/client_middleware.rb,
lib/gitlab/sidekiq_status/server_middleware.rb
Overview
The SidekiqStatus module and its child classes can be used for checking if a Sidekiq job has been processed or not.
To check if a job has been completed, simply pass the job ID to the ‘completed?` method:
job_id = SomeWorker.with_status.perform_async(...)
if Gitlab::SidekiqStatus.completed?(job_id)
...
end
If you do not use ‘with_status`, and the worker class does not declare `status_expiration` in its `sidekiq_options`, then this status will not be stored.
For each job ID registered a separate key is stored in Redis, making lookups much faster than using Sidekiq’s built-in job finding/status API. These keys expire after a certain period of time to prevent storing too many keys in Redis.
Defined Under Namespace
Classes: ClientMiddleware, ServerMiddleware
Constant Summary collapse
- STATUS_KEY =
'gitlab-sidekiq-status:%s'
- DEFAULT_EXPIRATION =
The default time (in seconds) after which a status key is expired automatically. The default of 30 minutes should be more than sufficient for most jobs.
30.minutes.to_i
Class Method Summary collapse
-
.all_completed?(job_ids) ⇒ Boolean
Returns true if all the given job have been completed.
-
.completed_jids(job_ids) ⇒ Object
Returns the JIDs that are completed.
-
.job_status(job_ids) ⇒ Object
Returns the job status for each of the given job IDs.
- .key_for(jid) ⇒ Object
-
.num_completed(job_ids) ⇒ Object
Returns the number of jobs that have completed.
-
.num_running(job_ids) ⇒ Object
Returns the number of jobs that are running or enqueued.
-
.running?(job_id) ⇒ Boolean
Returns true if the given job is running or enqueued.
-
.set(jid, expire = DEFAULT_EXPIRATION) ⇒ Object
Starts tracking of the given job.
-
.unset(jid) ⇒ Object
Stops the tracking of the given job.
Class Method Details
.all_completed?(job_ids) ⇒ Boolean
Returns true if all the given job have been completed.
job_ids - The Sidekiq job IDs to check.
Returns true or false.
58 59 60 |
# File 'lib/gitlab/sidekiq_status.rb', line 58 def self.all_completed?(job_ids) self.num_running(job_ids) == 0 end |
.completed_jids(job_ids) ⇒ Object
Returns the JIDs that are completed
job_ids - The Sidekiq job IDs to check.
Returns an array of completed JIDs
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gitlab/sidekiq_status.rb', line 106 def self.completed_jids(job_ids) statuses = job_status(job_ids) completed = [] job_ids.zip(statuses).each do |job_id, status| completed << job_id unless status end completed end |
.job_status(job_ids) ⇒ Object
Returns the job status for each of the given job IDs.
job_ids - The Sidekiq job IDs to check.
Returns an array of true or false indicating job completion. true = job is still running or enqueued false = job completed
92 93 94 95 96 97 98 99 |
# File 'lib/gitlab/sidekiq_status.rb', line 92 def self.job_status(job_ids) return [] if job_ids.empty? keys = job_ids.map { |jid| key_for(jid) } with_redis { |redis| redis.mget(*keys) } .map { |result| !result.nil? } end |
.key_for(jid) ⇒ Object
117 118 119 |
# File 'lib/gitlab/sidekiq_status.rb', line 117 def self.key_for(jid) STATUS_KEY % jid end |
.num_completed(job_ids) ⇒ Object
Returns the number of jobs that have completed.
job_ids - The Sidekiq job IDs to check.
81 82 83 |
# File 'lib/gitlab/sidekiq_status.rb', line 81 def self.num_completed(job_ids) job_ids.size - self.num_running(job_ids) end |
.num_running(job_ids) ⇒ Object
Returns the number of jobs that are running or enqueued.
job_ids - The Sidekiq job IDs to check.
72 73 74 75 76 |
# File 'lib/gitlab/sidekiq_status.rb', line 72 def self.num_running(job_ids) responses = self.job_status(job_ids) responses.count(&:present?) end |
.running?(job_id) ⇒ Boolean
Returns true if the given job is running or enqueued.
job_id - The Sidekiq job ID to check.
65 66 67 |
# File 'lib/gitlab/sidekiq_status.rb', line 65 def self.running?(job_id) num_running([job_id]) > 0 end |
.set(jid, expire = DEFAULT_EXPIRATION) ⇒ Object
Starts tracking of the given job.
jid - The Sidekiq job ID expire - The expiration time of the Redis key.
36 37 38 39 40 41 42 |
# File 'lib/gitlab/sidekiq_status.rb', line 36 def self.set(jid, expire = DEFAULT_EXPIRATION) return unless expire with_redis do |redis| redis.set(key_for(jid), 1, ex: expire) end end |
.unset(jid) ⇒ Object
Stops the tracking of the given job.
jid - The Sidekiq job ID to remove.
47 48 49 50 51 |
# File 'lib/gitlab/sidekiq_status.rb', line 47 def self.unset(jid) with_redis do |redis| redis.del(key_for(jid)) end end |