Class: Gitlab::SidekiqMigrateJobs

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/sidekiq_migrate_jobs.rb

Constant Summary collapse

LOG_FREQUENCY =
1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sidekiq_set, logger: nil) ⇒ SidekiqMigrateJobs

Returns a new instance of SidekiqMigrateJobs.


9
10
11
12
# File 'lib/gitlab/sidekiq_migrate_jobs.rb', line 9

def initialize(sidekiq_set, logger: nil)
  @sidekiq_set = sidekiq_set
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.


7
8
9
# File 'lib/gitlab/sidekiq_migrate_jobs.rb', line 7

def logger
  @logger
end

#sidekiq_setObject (readonly)

Returns the value of attribute sidekiq_set.


7
8
9
# File 'lib/gitlab/sidekiq_migrate_jobs.rb', line 7

def sidekiq_set
  @sidekiq_set
end

Instance Method Details

#execute(mappings) ⇒ Object

mappings is a hash of WorkerClassName => target_queue_name


15
16
17
18
19
20
21
22
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
# File 'lib/gitlab/sidekiq_migrate_jobs.rb', line 15

def execute(mappings)
  source_queues_regex = Regexp.union(mappings.keys)
  cursor = 0
  scanned = 0
  migrated = 0

  estimated_size = Sidekiq.redis { |c| c.zcard(sidekiq_set) }
  logger&.info("Processing #{sidekiq_set} set. Estimated size: #{estimated_size}.")

  begin
    cursor, jobs = Sidekiq.redis { |c| c.zscan(sidekiq_set, cursor) }

    jobs.each do |(job, score)|
      if scanned > 0 && scanned % LOG_FREQUENCY == 0
        logger&.info("In progress. Scanned records: #{scanned}. Migrated records: #{migrated}.")
      end

      scanned += 1

      next unless job.match?(source_queues_regex)

      job_hash = Sidekiq.load_json(job)
      destination_queue = mappings[job_hash['class']]

      next unless mappings.has_key?(job_hash['class'])
      next if job_hash['queue'] == destination_queue

      job_hash['queue'] = destination_queue

      migrated += migrate_job(job, score, job_hash)
    end
  end while cursor.to_i != 0

  logger&.info("Done. Scanned records: #{scanned}. Migrated records: #{migrated}.")

  {
    scanned: scanned,
    migrated: migrated
  }
end