Commit e2c3cc8f authored by Sean Arnold's avatar Sean Arnold

Move apply incident label into worker

- Add worker & update queues file
parent c82d8413
......@@ -4,5 +4,5 @@ class IssuableSla < ApplicationRecord
belongs_to :issue, optional: false
validates :due_at, presence: true
scope :exceeded_for_issues, -> { joins(:issue).merge(Issue.opened).where('due_at < ?', Time.current) }
scope :exceeded_for_issues, -> { includes(:issue).merge(Issue.opened).where('due_at < ?', Time.current) }
end
......@@ -661,6 +661,14 @@
:weight: 1
:idempotent:
:tags: []
- :name: incident_management_apply_incident_sla_exceeded_label
:feature_category: :incident_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: ldap_group_sync
:feature_category: :authentication_and_authorization
:has_external_dependencies: true
......
# frozen_string_literal: true
module IncidentManagement
class ApplyIncidentSlaExceededLabelService < BaseService
def initialize(incident)
super(incident.project)
class ApplyIncidentSlaExceededLabelWorker
include ApplicationWorker
@incident = incident
@label = incident_exceeded_sla_label
end
idempotent!
feature_category :incident_management
def perform(incident_id)
@incident = Issue.find_by(id: incident_id)
@project = incident&.project
def execute
return unless incident && project
@label = incident_exceeded_sla_label
return if incident.label_ids.include?(label.id)
incident.labels << label
......@@ -20,7 +24,7 @@ module IncidentManagement
private
attr_reader :incident, :label
attr_reader :incident, :project, :label
def add_resource_event
ResourceEvents::ChangeLabelsService
......
# frozen_string_literal: true
module IncidentManagement
class IncidentSlaExceededCheckWorker # rubocop:disable Scalability/IdempotentWorker
class IncidentSlaExceededCheckWorker
include ApplicationWorker
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
......@@ -11,7 +11,7 @@ module IncidentManagement
def perform
IssuableSla.exceeded_for_issues.find_in_batches do |incident_slas|
incident_slas.each do |incident_sla|
ApplyIncidentSlaExceededLabelService.new(incident_sla.issue).execute
ApplyIncidentSlaExceededLabelWorker.perform_async(incident_sla.issue.id)
rescue StandardError => e
Gitlab::AppLogger.error("Error encountered in #{self.class.name}: #{e}")
......
......@@ -2,7 +2,9 @@
require 'spec_helper'
RSpec.describe IncidentManagement::ApplyIncidentSlaExceededLabelService do
RSpec.describe IncidentManagement::ApplyIncidentSlaExceededLabelWorker do
let(:worker) { described_class.new }
let_it_be_with_refind(:incident) { create(:incident) }
let_it_be(:project) { incident.project }
let_it_be(:label) do
......@@ -12,7 +14,7 @@ RSpec.describe IncidentManagement::ApplyIncidentSlaExceededLabelService do
.payload[:label]
end
subject(:apply_label) { described_class.new(incident).execute }
subject(:perform) { worker.perform(incident.id) }
context 'label exists already' do
before do
......@@ -25,11 +27,11 @@ RSpec.describe IncidentManagement::ApplyIncidentSlaExceededLabelService do
end
it 'adds a label to the incident' do
expect { apply_label }.to change { incident.labels.reload.count }.by(1)
expect { perform }.to change { incident.labels.reload.count }.by(1)
expected_props = IncidentManagement::CreateIncidentSlaExceededLabelService::LABEL_PROPERTIES
expect(apply_label).to have_attributes(expected_props)
expect(perform).to have_attributes(expected_props)
end
it 'adds a note that the label was added' do
......
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