summaryrefslogtreecommitdiff
path: root/app/workers/gitlab/github_gists_import/finish_import_worker.rb
blob: 284e5833f0c7b6aadad527324f395a870659aa96 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
# frozen_string_literal: true

module Gitlab
  module GithubGistsImport
    class FinishImportWorker
      include ApplicationWorker

      data_consistency :always
      queue_namespace :github_gists_importer
      feature_category :importers
      idempotent!

      sidekiq_options dead: false, retry: 5

      sidekiq_retries_exhausted do |msg, _|
        Gitlab::GithubGistsImport::Status.new(msg['args'][0]).fail!
      end

      INTERVAL = 30.seconds.to_i
      BLOCKING_WAIT_TIME = 5
      GISTS_ERRORS_BY_ID = 'gitlab:github-gists-import:%{user_id}:errors'

      def perform(user_id, waiter_key, remaining)
        waiter = wait_for_jobs(waiter_key, remaining)

        if waiter.nil?
          Gitlab::GithubGistsImport::Status.new(user_id).finish!
          Gitlab::GithubImport::Logger.info(user_id: user_id, message: 'GitHub Gists import finished')
          send_email_if_errors(user_id)
        else
          self.class.perform_in(INTERVAL, user_id, waiter.key, waiter.jobs_remaining)
        end
      end

      private

      def wait_for_jobs(key, remaining)
        waiter = JobWaiter.new(remaining, key)
        waiter.wait(BLOCKING_WAIT_TIME)

        return if waiter.jobs_remaining == 0

        waiter
      end

      def send_email_if_errors(user_id)
        key = format(GISTS_ERRORS_BY_ID, user_id: user_id)
        errors = ::Gitlab::Cache::Import::Caching.values_from_hash(key)

        return if errors.blank?

        Notify.github_gists_import_errors_email(user_id, errors).deliver_now
      ensure
        ::Gitlab::Cache::Import::Caching.expire(key, ::Gitlab::Cache::Import::Caching::SHORTER_TIMEOUT)
      end
    end
  end
end