Commit c4796bc6 authored by Matija Čupić's avatar Matija Čupić Committed by Douglas Barbosa Alexandre

Move pipeline retry to worker

Makes the Projects::PipelinesController#retry endpoint perform the retry
asynchronously via a worker.

Changelog: changed
parent 23853502
......@@ -176,7 +176,11 @@ class Projects::PipelinesController < Projects::ApplicationController
end
def retry
pipeline.retry_failed(current_user)
if Feature.enabled?(:background_pipeline_retry_endpoint, @project)
::Ci::RetryPipelineWorker.perform_async(pipeline.id, current_user.id) # rubocop:disable CodeReuse/Worker
else
pipeline.retry_failed(current_user)
end
respond_to do |format|
format.html do
......
......@@ -1515,6 +1515,15 @@
:weight: 3
:idempotent:
:tags: []
- :name: pipeline_default:ci_retry_pipeline
:worker_name: Ci::RetryPipelineWorker
:feature_category: :continuous_integration
:has_external_dependencies:
:urgency: :high
:resource_boundary: :cpu
:weight: 3
:idempotent:
:tags: []
- :name: pipeline_default:pipeline_metrics
:worker_name: PipelineMetricsWorker
:feature_category: :continuous_integration
......
# frozen_string_literal: true
module Ci
class RetryPipelineWorker # rubocop:disable Scalability/IdempotentWorker
include ::ApplicationWorker
include ::PipelineQueue
urgency :high
worker_resource_boundary :cpu
def perform(pipeline_id, user_id)
::Ci::Pipeline.find_by_id(pipeline_id).try do |pipeline|
::User.find_by_id(user_id).try do |user|
pipeline.retry_failed(user)
end
end
end
end
end
......@@ -856,7 +856,11 @@ RSpec.describe Projects::PipelinesController do
let!(:pipeline) { create(:ci_pipeline, :failed, project: project) }
let!(:build) { create(:ci_build, :failed, pipeline: pipeline) }
let(:worker_spy) { class_spy(::Ci::RetryPipelineWorker) }
before do
stub_const('::Ci::RetryPipelineWorker', worker_spy)
post :retry, params: {
namespace_id: project.namespace,
project_id: project,
......@@ -865,9 +869,9 @@ RSpec.describe Projects::PipelinesController do
format: :json
end
it 'retries a pipeline without returning any content' do
it 'retries a pipeline in the background without returning any content' do
expect(response).to have_gitlab_http_status(:no_content)
expect(build.reload).to be_retried
expect(::Ci::RetryPipelineWorker).to have_received(:perform_async).with(pipeline.id, user.id)
end
context 'when builds are disabled' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::RetryPipelineWorker do
describe '#perform' do
subject(:perform) { described_class.new.perform(pipeline_id, user_id) }
let(:pipeline) { create(:ci_pipeline) }
context 'when pipeline exists' do
let(:pipeline_id) { pipeline.id }
context 'when user exists' do
let(:user) { create(:user) }
let(:user_id) { user.id }
before do
pipeline.project.add_maintainer(user)
end
it 'retries the pipeline' do
expect(::Ci::Pipeline).to receive(:find_by_id).with(pipeline.id).and_return(pipeline)
expect(pipeline).to receive(:retry_failed).with(having_attributes(id: user_id))
perform
end
end
context 'when user does not exist' do
let(:user_id) { 1234 }
it 'does not retry the pipeline' do
expect(::Ci::Pipeline).to receive(:find_by_id).with(pipeline_id).and_return(pipeline)
expect(pipeline).not_to receive(:retry_failed).with(having_attributes(id: user_id))
perform
end
end
end
context 'when pipeline does not exist' do
let(:pipeline_id) { 1234 }
let(:user_id) { 1234 }
it 'returns false' do
expect(perform).to eq(nil)
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