Commit d093b1f7 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '210522-import-status-correlation-id' into 'master'

Store correlation ID with import state & expose via API

See merge request gitlab-org/gitlab!28662
parents 7626a927 4dd597e0
...@@ -10,6 +10,8 @@ class ProjectImportState < ApplicationRecord ...@@ -10,6 +10,8 @@ class ProjectImportState < ApplicationRecord
validates :project, presence: true validates :project, presence: true
alias_attribute :correlation_id, :correlation_id_value
state_machine :status, initial: :none do state_machine :status, initial: :none do
event :schedule do event :schedule do
transition [:none, :finished, :failed] => :scheduled transition [:none, :finished, :failed] => :scheduled
...@@ -39,7 +41,11 @@ class ProjectImportState < ApplicationRecord ...@@ -39,7 +41,11 @@ class ProjectImportState < ApplicationRecord
after_transition [:none, :finished, :failed] => :scheduled do |state, _| after_transition [:none, :finished, :failed] => :scheduled do |state, _|
state.run_after_commit do state.run_after_commit do
job_id = project.add_import_job job_id = project.add_import_job
update(jid: job_id) if job_id
if job_id
correlation_id = Labkit::Correlation::CorrelationId.current_or_new_id
update(jid: job_id, correlation_id_value: correlation_id)
end
end end
end end
......
---
title: Add correlation_id to project_mirror_data, expose in /import API endpoints
merge_request: 28662
author:
type: changed
# frozen_string_literal: true
class AddCorrelationIdToProjectImportState < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
add_column :project_mirror_data, :correlation_id_value, :string, limit: 128
end
end
def down
with_lock_retries do
remove_column :project_mirror_data, :correlation_id_value
end
end
end
...@@ -4932,7 +4932,8 @@ CREATE TABLE public.project_mirror_data ( ...@@ -4932,7 +4932,8 @@ CREATE TABLE public.project_mirror_data (
jid character varying, jid character varying,
last_error text, last_error text,
last_update_at timestamp with time zone, last_update_at timestamp with time zone,
last_successful_update_at timestamp with time zone last_successful_update_at timestamp with time zone,
correlation_id_value character varying(128)
); );
CREATE SEQUENCE public.project_mirror_data_id_seq CREATE SEQUENCE public.project_mirror_data_id_seq
...@@ -13062,6 +13063,7 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -13062,6 +13063,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200331220930 20200331220930
20200401211005 20200401211005
20200402123926 20200402123926
20200402124802
20200402135250 20200402135250
20200402185044 20200402185044
20200403184110 20200403184110
......
...@@ -172,7 +172,8 @@ requests.post(url, headers=headers, data=data, files=files) ...@@ -172,7 +172,8 @@ requests.post(url, headers=headers, data=data, files=files)
"path": "api-project", "path": "api-project",
"path_with_namespace": "root/api-project", "path_with_namespace": "root/api-project",
"created_at": "2018-02-13T09:05:58.023Z", "created_at": "2018-02-13T09:05:58.023Z",
"import_status": "scheduled" "import_status": "scheduled",
"correlation_id": "mezklWso3Za"
} }
``` ```
...@@ -211,6 +212,7 @@ If the status is `failed`, it will include the import error message under `impor ...@@ -211,6 +212,7 @@ If the status is `failed`, it will include the import error message under `impor
"path": "gitlab-test", "path": "gitlab-test",
"path_with_namespace": "gitlab-org/gitlab-test", "path_with_namespace": "gitlab-org/gitlab-test",
"created_at": "2017-08-29T04:36:44.383Z", "created_at": "2017-08-29T04:36:44.383Z",
"import_status": "started" "import_status": "started",
"correlation_id": "mezklWso3Za"
} }
``` ```
...@@ -4,6 +4,9 @@ module API ...@@ -4,6 +4,9 @@ module API
module Entities module Entities
class ProjectImportStatus < ProjectIdentity class ProjectImportStatus < ProjectIdentity
expose :import_status expose :import_status
expose :correlation_id do |project, _options|
project.import_state.correlation_id
end
# TODO: Use `expose_nil` once we upgrade the grape-entity gem # TODO: Use `expose_nil` once we upgrade the grape-entity gem
expose :import_error, if: lambda { |project, _ops| project.import_state&.last_error } do |project| expose :import_error, if: lambda { |project, _ops| project.import_state&.last_error } do |project|
......
...@@ -14,8 +14,8 @@ describe ProjectImportState, type: :model do ...@@ -14,8 +14,8 @@ describe ProjectImportState, type: :model do
end end
describe 'Project import job' do describe 'Project import job' do
let(:import_state) { create(:import_state, import_url: generate(:url)) } let_it_be(:import_state) { create(:import_state, import_url: generate(:url)) }
let(:project) { import_state.project } let_it_be(:project) { import_state.project }
before do before do
allow_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:import_repository) allow_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:import_repository)
...@@ -29,8 +29,16 @@ describe ProjectImportState, type: :model do ...@@ -29,8 +29,16 @@ describe ProjectImportState, type: :model do
it 'imports a project', :sidekiq_might_not_need_inline do it 'imports a project', :sidekiq_might_not_need_inline do
expect(RepositoryImportWorker).to receive(:perform_async).and_call_original expect(RepositoryImportWorker).to receive(:perform_async).and_call_original
expect { import_state.schedule }.to change { import_state.jid } expect { import_state.schedule }.to change { import_state.status }.from('none').to('finished')
expect(import_state.status).to eq('finished') end
it 'records job and correlation IDs', :sidekiq_might_not_need_inline do
allow(Labkit::Correlation::CorrelationId).to receive(:current_or_new_id).and_return('abc')
import_state.schedule
expect(import_state.jid).to be_an_instance_of(String)
expect(import_state.correlation_id).to eq('abc')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment