summaryrefslogtreecommitdiff
path: root/app/workers/concerns/gitlab/github_import/rescheduling_methods.rb
blob: eb1af0869bd933d9920623ae71933c8407f2d343 (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
# frozen_string_literal: true

module Gitlab
  module GithubImport
    # Module that provides methods shared by the various workers used for
    # importing GitHub projects.
    module ReschedulingMethods
      # project_id - The ID of the GitLab project to import the note into.
      # hash - A Hash containing the details of the GitHub object to import.
      # notify_key - The Redis key to notify upon completion, if any.
      # rubocop: disable CodeReuse/ActiveRecord
      def perform(project_id, hash, notify_key = nil)
        project = Project.find_by(id: project_id)

        return notify_waiter(notify_key) unless project

        client = GithubImport.new_client_for(project, parallel: true)

        if try_import(project, client, hash)
          notify_waiter(notify_key)
        else
          # In the event of hitting the rate limit we want to reschedule the job
          # so its retried after our rate limit has been reset.
          self.class
            .perform_in(client.rate_limit_resets_in, project.id, hash, notify_key)
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord

      def try_import(*args)
        import(*args)
        true
      rescue RateLimitError
        false
      end

      def notify_waiter(key = nil)
        JobWaiter.notify(key, jid) if key
      end
    end
  end
end