BigW Consortium Gitlab

20161019190736_migrate_sidekiq_queues_from_default.rb 4.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
require 'json'

# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class MigrateSidekiqQueuesFromDefault < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = true

11
  DOWNTIME_REASON = <<-EOF
12 13 14 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  Moving Sidekiq jobs from queues requires Sidekiq to be stopped. Not stopping
  Sidekiq will result in the loss of jobs that are scheduled after this
  migration completes.
  EOF

  disable_ddl_transaction!

  # Jobs for which the queue names have been changed (e.g. multiple workers
  # using the same non-default queue).
  #
  # The keys are the old queue names, the values the jobs to move and their new
  # queue names.
  RENAMED_QUEUES = {
    gitlab_shell: {
      'GitGarbageCollectorWorker' => :git_garbage_collector,
      'ProjectExportWorker'       => :project_export,
      'RepositoryForkWorker'      => :repository_fork,
      'RepositoryImportWorker'    => :repository_import
    },
    project_web_hook: {
      'ProjectServiceWorker' => :project_service
    },
    incoming_email: {
      'EmailReceiverWorker' => :email_receiver
    },
    mailers: {
      'EmailsOnPushWorker' => :emails_on_push
    },
    default: {
      'AdminEmailWorker'                        => :cronjob,
      'BuildCoverageWorker'                     => :build,
      'BuildEmailWorker'                        => :build,
      'BuildFinishedWorker'                     => :build,
      'BuildHooksWorker'                        => :build,
      'BuildSuccessWorker'                      => :build,
      'ClearDatabaseCacheWorker'                => :clear_database_cache,
      'DeleteUserWorker'                        => :delete_user,
      'ExpireBuildArtifactsWorker'              => :cronjob,
      'ExpireBuildInstanceArtifactsWorker'      => :expire_build_instance_artifacts,
      'GroupDestroyWorker'                      => :group_destroy,
      'ImportExportProjectCleanupWorker'        => :cronjob,
      'IrkerWorker'                             => :irker,
      'MergeWorker'                             => :merge,
      'NewNoteWorker'                           => :new_note,
      'PipelineHooksWorker'                     => :pipeline,
      'PipelineMetricsWorker'                   => :pipeline,
      'PipelineProcessWorker'                   => :pipeline,
      'PipelineSuccessWorker'                   => :pipeline,
      'PipelineUpdateWorker'                    => :pipeline,
      'ProjectCacheWorker'                      => :project_cache,
      'ProjectDestroyWorker'                    => :project_destroy,
      'PruneOldEventsWorker'                    => :cronjob,
      'RemoveExpiredGroupLinksWorker'           => :cronjob,
      'RemoveExpiredMembersWorker'              => :cronjob,
      'RepositoryArchiveCacheWorker'            => :cronjob,
      'RepositoryCheck::BatchWorker'            => :cronjob,
      'RepositoryCheck::ClearWorker'            => :repository_check,
      'RepositoryCheck::SingleRepositoryWorker' => :repository_check,
      'RequestsProfilesWorker'                  => :cronjob,
      'StuckCiBuildsWorker'                     => :cronjob,
      'UpdateMergeRequestsWorker'               => :update_merge_requests
    }
74
  }.freeze
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  def up
    Sidekiq.redis do |redis|
      RENAMED_QUEUES.each do |queue, jobs|
        migrate_from_queue(redis, queue, jobs)
      end
    end
  end

  def down
    Sidekiq.redis do |redis|
      RENAMED_QUEUES.each do |dest_queue, jobs|
        jobs.each do |worker, from_queue|
          migrate_from_queue(redis, from_queue, worker => dest_queue)
        end
      end
    end
  end

  def migrate_from_queue(redis, queue, job_mapping)
    while job = redis.lpop("queue:#{queue}")
Douwe Maan committed
96
      payload = JSON.parse(job)
97 98 99 100 101 102 103 104 105 106 107 108 109
      new_queue = job_mapping[payload['class']]

      # If we have no target queue to migrate to we're probably dealing with
      # some ancient job for which the worker no longer exists. In that case
      # there's no sane option we can take, other than just dropping the job.
      next unless new_queue

      payload['queue'] = new_queue

      redis.lpush("queue:#{new_queue}", JSON.dump(payload))
    end
  end
end