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