Commit a946dc15 authored by Peter Leitzen's avatar Peter Leitzen

Add worker to publish incidents to status page

This worker starts the process of publishing incidents to the status
page.
parent 35eb3fa2
......@@ -232,6 +232,8 @@
- 2
- - service_desk_email_receiver
- 1
- - status_page_publish_incident
- 1
- - sync_seat_link_request
- 1
- - system_hook_push
......
......@@ -570,6 +570,13 @@
:resource_boundary: :unknown
:weight: 1
:idempotent:
- :name: status_page_publish_incident
:feature_category: :status_page
:has_external_dependencies: true
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
- :name: sync_seat_link_request
:feature_category: :analysis
:has_external_dependencies: true
......
# frozen_string_literal: true
module StatusPage
class PublishIncidentWorker
include ApplicationWorker
include Gitlab::Utils::StrongMemoize
sidekiq_options retry: 5
feature_category :status_page
worker_has_external_dependencies!
idempotent!
def perform(project_id, issue_id)
@project_id = project_id
@issue_id = issue_id
@project = Project.find_by_id(project_id)
return if project.nil?
publish
end
private
attr_reader :project_id, :issue_id, :project
def publish
result = PublishIncidentService
.new(project: project, issue_id: issue_id)
.execute
log_error(result.message) if result.error?
rescue => e
log_error(e.message)
raise
end
def log_error(message)
preamble = "Failed to publish incident for project_id=#{project_id}, issue_id=#{issue_id}"
logger.error("#{preamble}: #{message}")
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe StatusPage::PublishIncidentWorker do
include ExclusiveLeaseHelpers
let_it_be(:project) { create(:project) }
let_it_be(:issue) { create(:issue, project: project) }
let(:worker) { described_class.new }
let(:logger) { worker.send(:logger) }
let(:service) { instance_double(StatusPage::PublishIncidentService) }
let(:service_result) { ServiceResponse.success }
before do
allow(StatusPage::PublishIncidentService)
.to receive(:new).with(project: project, issue_id: issue.id)
.and_return(service)
allow(service).to receive(:execute)
.and_return(service_result)
end
describe '#perform' do
subject { worker.perform(project.id, issue.id) }
it_behaves_like 'an idempotent worker' do
let(:job_args) { [project.id, issue.id] }
context 'when service succeeds' do
it 'execute the service' do
expect(service).to receive(:execute)
subject
end
end
context 'with unknown project' do
let(:project) { build(:project) }
it 'does not execute the service' do
expect(StatusPage::PublishIncidentService).not_to receive(:execute)
subject
end
end
context 'when service returns an error' do
let(:error_message) { 'some message' }
let(:service_result) { ServiceResponse.error(message: error_message) }
it 'succeeds and logs the errors' do
expect(logger)
.to receive(:error).with(/#{error_message}/)
.exactly(worker_exec_times).times
subject
end
end
end
context 'when service raises an exception' do
let(:error_message) { 'some exception' }
let(:exception) { StandardError.new(error_message) }
it 'logs and re-raises exception' do
allow(service).to receive(:execute).and_raise(exception)
expect(logger).to receive(:error).with(/#{error_message}/)
expect { subject }.to raise_error(exception)
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