Module: Gitlab::Database::Migrations::SidekiqHelpers

Included in:
Gitlab::Database::MigrationHelpers
Defined in:
lib/gitlab/database/migrations/sidekiq_helpers.rb

Overview

rubocop:disable Cop/SidekiqApiUsage rubocop:disable Cop/SidekiqRedisCall

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =

Constants for default sidekiq_remove_jobs values

5
DEFAULT_TIMES_IN_A_ROW =
2

Instance Method Summary collapse

Instance Method Details

#sidekiq_queue_length(queue_name) ⇒ Object



70
71
72
73
74
# File 'lib/gitlab/database/migrations/sidekiq_helpers.rb', line 70

def sidekiq_queue_length(queue_name)
  Sidekiq.redis do |conn|
    conn.llen("queue:#{queue_name}")
  end
end

#sidekiq_queue_migrate(queue_from, to:) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/gitlab/database/migrations/sidekiq_helpers.rb', line 62

def sidekiq_queue_migrate(queue_from, to:)
  while sidekiq_queue_length(queue_from) > 0
    Sidekiq.redis do |conn|
      conn.rpoplpush "queue:#{queue_from}", "queue:#{to}"
    end
  end
end

#sidekiq_remove_jobs(job_klasses:, times_in_a_row: DEFAULT_TIMES_IN_A_ROW, max_attempts: DEFAULT_MAX_ATTEMPTS) ⇒ Object

Probabilistically removes job_klasses from their specific queues, the retry set and the scheduled set.

If jobs are still being processed at the same time, then there is a small chance it will not remove all instances of job_klass. To minimize this risk, it repeatedly removes matching jobs from each until nothing is removed twice in a row.

Before calling this method, you should make sure that job_klass is no longer being scheduled within the running application.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/database/migrations/sidekiq_helpers.rb', line 23

def sidekiq_remove_jobs(
  job_klasses:,
  times_in_a_row: DEFAULT_TIMES_IN_A_ROW,
  max_attempts: DEFAULT_MAX_ATTEMPTS
)
  kwargs = { times_in_a_row: times_in_a_row, max_attempts: max_attempts }

  if transaction_open?
    raise 'sidekiq_remove_jobs can not be run inside a transaction, ' \
          'you can disable transactions by calling disable_ddl_transaction! ' \
          'in the body of your migration class'
  end

  job_klasses_queues = job_klasses
    .select { |job_klass| job_klass.to_s.safe_constantize.present? }
    .map { |job_klass| job_klass.safe_constantize.queue }
    .uniq

  job_klasses_queues.each do |queue|
    delete_jobs_for(
      set: Sidekiq::Queue.new(queue),
      job_klasses: job_klasses,
      kwargs: kwargs
    )
  end

  delete_jobs_for(
    set: Sidekiq::RetrySet.new,
    kwargs: kwargs,
    job_klasses: job_klasses
  )

  delete_jobs_for(
    set: Sidekiq::ScheduledSet.new,
    kwargs: kwargs,
    job_klasses: job_klasses
  )
end