diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-06 09:09:19 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-06 09:09:19 +0300 |
commit | cce8cf03d3bebe8b05375e4db0004328f84b28a2 (patch) | |
tree | c4fe6a257e894b6ce226a36f275f35675025c299 /app/models/jira_import_state.rb | |
parent | f098e6d3d2c8eaaec0a228c8a3ae01f770e15dd2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/jira_import_state.rb')
-rw-r--r-- | app/models/jira_import_state.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/app/models/jira_import_state.rb b/app/models/jira_import_state.rb new file mode 100644 index 00000000000..0c138d674ea --- /dev/null +++ b/app/models/jira_import_state.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +class JiraImportState < ApplicationRecord + include AfterCommitQueue + + self.table_name = 'jira_imports' + + STATUSES = { initial: 0, scheduled: 1, started: 2, failed: 3, finished: 4 }.freeze + + belongs_to :project + belongs_to :user + belongs_to :label + + validates :project, presence: true + validates :jira_project_key, presence: true + validates :jira_project_name, presence: true + validates :jira_project_xid, presence: true + + validates :project, uniqueness: { + conditions: -> { where.not(status: STATUSES.values_at(:failed, :finished)) }, + message: _('Cannot have multiple Jira imports running at the same time') + } + + state_machine :status, initial: :initial do + event :schedule do + transition initial: :scheduled + end + + event :start do + transition scheduled: :started + end + + event :finish do + transition started: :finished + end + + event :do_fail do + transition [:initial, :scheduled, :started] => :failed + end + + after_transition initial: :scheduled do |state, _| + state.run_after_commit do + job_id = Gitlab::JiraImport::Stage::StartImportWorker.perform_async(project.id) + state.update(jid: job_id) if job_id + end + end + + after_transition any => :finished do |state, _| + if state.jid.present? + Gitlab::SidekiqStatus.unset(state.jid) + + state.update_column(:jid, nil) + end + end + + # Supress warning: + # both JiraImportState and its :status machine have defined a different default for "status". + # although both have same value but represented in 2 ways: integer(0) and symbol(:initial) + def owner_class_attribute_default + 'initial' + end + end + + enum status: STATUSES + + def in_progress? + scheduled? || started? + end + + def refresh_jid_expiration + return unless jid + + Gitlab::SidekiqStatus.set(jid, StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION) + end + + def self.jid_by(project_id:, status:) + select(:jid).with_status(status).find_by(project_id: project_id) + end +end |