Class: Gitlab::PhabricatorImport::BaseWorker

Inherits:
Object
  • Object
show all
Includes:
ExclusiveLeaseHelpers, WorkerAttributes
Defined in:
app/workers/gitlab/phabricator_import/base_worker.rb

Overview

rubocop:disable Scalability/IdempotentWorker

Direct Known Subclasses

ImportTasksWorker

Constant Summary

Constants included from ExclusiveLeaseHelpers

ExclusiveLeaseHelpers::FailedToObtainLockError

Constants included from WorkerAttributes

WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExclusiveLeaseHelpers

#in_lock

Class Method Details

.add_job(project_id) ⇒ Object


33
34
35
# File 'app/workers/gitlab/phabricator_import/base_worker.rb', line 33

def add_job(project_id)
  worker_state(project_id).add_job
end

.remove_job(project_id) ⇒ Object


37
38
39
# File 'app/workers/gitlab/phabricator_import/base_worker.rb', line 37

def remove_job(project_id)
  worker_state(project_id).remove_job
end

.schedule(project_id, *args) ⇒ Object


28
29
30
31
# File 'app/workers/gitlab/phabricator_import/base_worker.rb', line 28

def schedule(project_id, *args)
  perform_async(project_id, *args)
  add_job(project_id)
end

.worker_state(project_id) ⇒ Object


41
42
43
# File 'app/workers/gitlab/phabricator_import/base_worker.rb', line 41

def worker_state(project_id)
  Gitlab::PhabricatorImport::WorkerState.new(project_id)
end

Instance Method Details

#perform(project_id, *args) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/workers/gitlab/phabricator_import/base_worker.rb', line 46

def perform(project_id, *args)
  in_lock("#{self.class.name.underscore}/#{project_id}/#{args}", ttl: 2.hours, sleep_sec: 5.seconds) do
    project = Project.find_by_id(project_id)
    next unless project

    # Bail if the import job already failed
    next unless project.import_state&.in_progress?

    project.import_state.refresh_jid_expiration

    import(project, *args)

    # If this is the last running job, finish the import
    project.after_import if self.class.worker_state(project_id).running_count < 2

    self.class.remove_job(project_id)
  end
rescue Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
  # Reschedule a job if there was already a running one
  # Running them at the same time could cause a deadlock updating the same
  # resource
  self.class.perform_in(30.seconds, project_id, *args)
end