diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
commit | 585826cb22ecea5998a2c2a4675735c94bdeedac (patch) | |
tree | 5b05f0b30d33cef48963609e8a18a4dff260eab3 /db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb | |
parent | df221d036e5d0c6c0ee4d55b9c97f481ee05dee8 (diff) |
Add latest changes from gitlab-org/gitlab@16-6-stable-eev16.6.0-rc42
Diffstat (limited to 'db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb')
-rw-r--r-- | db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb b/db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb new file mode 100644 index 00000000000..7d4d6876848 --- /dev/null +++ b/db/post_migrate/20231003045342_migrate_sidekiq_namespaced_jobs.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +class MigrateSidekiqNamespacedJobs < Gitlab::Database::Migration[2.1] + BATCH_SIZE = 1000 + SORTED_SET_NAMES = %w[schedule retry dead] + + def up + SORTED_SET_NAMES.each do |set_name| + sorted_set_migrate("resque:gitlab:#{set_name}", set_name) + end + + Sidekiq::Queue.all.each do |queue| + name = queue.name + sidekiq_queue_migrate("resque:gitlab:queue:#{name}", to: "queue:#{name}") + end + end + + def down + SORTED_SET_NAMES.each do |set_name| + sorted_set_migrate(set_name, "resque:gitlab:#{set_name}") + end + + Sidekiq::Queue.all.each do |queue| + name = queue.name + sidekiq_queue_migrate("queue:#{name}", to: "resque:gitlab:queue:#{name}") + end + end + + private + + def sidekiq_queue_migrate(queue_from, to:) + Gitlab::Redis::Queues.with do |conn| # rubocop:disable Cop/RedisQueueUsage + conn.rpoplpush(queue_from, to) while conn.llen(queue_from) > 0 + end + end + + def sorted_set_migrate(from, to) + cursor = '0' + + loop do + result = [] + + Gitlab::Redis::Queues.with do |redis| # rubocop:disable Cop/RedisQueueUsage + cursor, result = redis.zscan(from, cursor, count: BATCH_SIZE) + + next if result.empty? + + redis.multi do |multi| + multi.zadd(to, result.map { |k, v| [v, k] }) + multi.zrem(from, result.map { |k, _v| k }) + end + end + + sleep(0.01) + + break if cursor == '0' + end + end +end |