Class: Ci::UnlockPipelineRequest

Inherits:
Object
  • Object
show all
Defined in:
app/models/ci/unlock_pipeline_request.rb

Constant Summary collapse

QUEUE_REDIS_KEY =
'ci_unlock_pipeline_requests:queue'

Class Method Summary collapse

Class Method Details

.enqueue(pipeline_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/ci/unlock_pipeline_request.rb', line 7

def self.enqueue(pipeline_id)
  unix_timestamp = Time.current.utc.to_i
  pipeline_ids = Array(pipeline_id).uniq
  pipeline_ids_with_scores = pipeline_ids.map do |id|
    # The order of values per pair is `[score, key]`, so in this case, the unix timestamp is the score.
    # By default, the sort order of sorted sets is from lowest to highest, though this does not matter much
    # because we use `ZPOPMIN` to make sure to return the lowest/oldest request in terms of unix timestamp score.
    [unix_timestamp, id]
  end

  with_redis do |redis|
    added = redis.zadd(QUEUE_REDIS_KEY, pipeline_ids_with_scores, nx: true)
    log_event(:enqueued, pipeline_ids) if added > 0
    added
  end
end

.log_event(event, pipeline_id) ⇒ Object



46
47
48
49
50
51
# File 'app/models/ci/unlock_pipeline_request.rb', line 46

def self.log_event(event, pipeline_id)
  Gitlab::AppLogger.info(
    message: "Pipeline unlock - #{event}",
    pipeline_id: pipeline_id
  )
end

.next!Object



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/ci/unlock_pipeline_request.rb', line 24

def self.next!
  with_redis do |redis|
    pipeline_id, enqueue_timestamp = redis.zpopmin(QUEUE_REDIS_KEY)
    break unless pipeline_id

    pipeline_id = pipeline_id.to_i
    log_event(:picked_next, pipeline_id)

    [pipeline_id, enqueue_timestamp.to_i]
  end
end

.total_pendingObject



36
37
38
39
40
# File 'app/models/ci/unlock_pipeline_request.rb', line 36

def self.total_pending
  with_redis do |redis|
    redis.zcard(QUEUE_REDIS_KEY)
  end
end

.with_redis(&block) ⇒ Object



42
43
44
# File 'app/models/ci/unlock_pipeline_request.rb', line 42

def self.with_redis(&block)
  Gitlab::Redis::SharedState.with(&block)
end