Commit 07e0b72b authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '32351-delete-api' into 'master'

Sidekiq worker to delete self-monitoring project

See merge request gitlab-org/gitlab!21402
parents 67cf4845 8a6ddca0
...@@ -188,3 +188,4 @@ ...@@ -188,3 +188,4 @@
- create_evidence - create_evidence
- group_export - group_export
- self_monitoring_project_create - self_monitoring_project_create
- self_monitoring_project_delete
# frozen_string_literal: true
module SelfMonitoringProjectWorker
extend ActiveSupport::Concern
included do
# This worker falls under Self-monitoring with Monitor::APM group. However,
# self-monitoring is not classified as a feature category but rather as
# Other Functionality. Metrics seems to be the closest feature_category for
# this worker.
feature_category :metrics
end
LEASE_TIMEOUT = 15.minutes.to_i
EXCLUSIVE_LEASE_KEY = 'self_monitoring_service_creation_deletion'
class_methods do
# @param job_id [String]
# Job ID that is used to construct the cache keys.
# @return [Hash]
# Returns true if the job is enqueued or in progress and false otherwise.
def in_progress?(job_id)
Gitlab::SidekiqStatus.job_status(Array.wrap(job_id)).first
end
end
private
def lease_key
EXCLUSIVE_LEASE_KEY
end
def lease_timeout
self.class::LEASE_TIMEOUT
end
end
...@@ -3,38 +3,11 @@ ...@@ -3,38 +3,11 @@
class SelfMonitoringProjectCreateWorker class SelfMonitoringProjectCreateWorker
include ApplicationWorker include ApplicationWorker
include ExclusiveLeaseGuard include ExclusiveLeaseGuard
include SelfMonitoringProjectWorker
# This worker falls under Self-monitoring with Monitor::APM group. However,
# self-monitoring is not classified as a feature category but rather as
# Other Functionality. Metrics seems to be the closest feature_category for
# this worker.
feature_category :metrics
LEASE_TIMEOUT = 15.minutes.to_i
EXCLUSIVE_LEASE_KEY = 'self_monitoring_service_creation_deletion'
def perform def perform
try_obtain_lease do try_obtain_lease do
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService.new.execute Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService.new.execute
end end
end end
# @param job_id [String]
# Job ID that is used to construct the cache keys.
# @return [Hash]
# Returns true if the job is enqueued or in progress and false otherwise.
def self.in_progress?(job_id)
Gitlab::SidekiqStatus.job_status(Array.wrap(job_id)).first
end
private
def lease_key
EXCLUSIVE_LEASE_KEY
end
def lease_timeout
LEASE_TIMEOUT
end
end end
# frozen_string_literal: true
class SelfMonitoringProjectDeleteWorker
include ApplicationWorker
include ExclusiveLeaseGuard
include SelfMonitoringProjectWorker
def perform
try_obtain_lease do
Gitlab::DatabaseImporters::SelfMonitoring::Project::DeleteService.new.execute
end
end
end
...@@ -100,6 +100,7 @@ ...@@ -100,6 +100,7 @@
- [create_evidence, 2] - [create_evidence, 2]
- [group_export, 1] - [group_export, 1]
- [self_monitoring_project_create, 2] - [self_monitoring_project_create, 2]
- [self_monitoring_project_delete, 2]
# EE-specific queues # EE-specific queues
- [analytics, 1] - [analytics, 1]
......
# frozen_string_literal: true
# This shared_example requires the following variables:
# let(:service_class) { Gitlab::DatabaseImporters::SelfMonitoring::Project::DeleteService }
# let(:service) { instance_double(service_class) }
RSpec.shared_examples 'executes service' do
before do
allow(service_class).to receive(:new) { service }
end
it 'runs the service' do
expect(service).to receive(:execute)
subject.perform
end
end
RSpec.shared_examples 'returns in_progress based on Sidekiq::Status' do
it 'returns true when job is enqueued' do
jid = described_class.perform_async
expect(described_class.in_progress?(jid)).to eq(true)
end
it 'returns false when job does not exist' do
expect(described_class.in_progress?('fake_jid')).to eq(false)
end
end
...@@ -7,22 +7,10 @@ describe SelfMonitoringProjectCreateWorker do ...@@ -7,22 +7,10 @@ describe SelfMonitoringProjectCreateWorker do
let(:service_class) { Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService } let(:service_class) { Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService }
let(:service) { instance_double(service_class) } let(:service) { instance_double(service_class) }
before do it_behaves_like 'executes service'
allow(service_class).to receive(:new) { service }
end
it 'runs the SelfMonitoring::Project::CreateService' do
expect(service).to receive(:execute)
subject.perform
end
end end
describe '.in_progress?', :clean_gitlab_redis_shared_state do describe '.in_progress?', :clean_gitlab_redis_shared_state do
it 'returns in_progress when job is enqueued' do it_behaves_like 'returns in_progress based on Sidekiq::Status'
jid = described_class.perform_async
expect(described_class.in_progress?(jid)).to eq(true)
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe SelfMonitoringProjectDeleteWorker do
let_it_be(:jid) { 'b5b28910d97563e58c2fe55f' }
let_it_be(:data_key) { "self_monitoring_delete_result:#{jid}" }
describe '#perform' do
let(:service_class) { Gitlab::DatabaseImporters::SelfMonitoring::Project::DeleteService }
let(:service) { instance_double(service_class) }
it_behaves_like 'executes service'
end
describe '.status', :clean_gitlab_redis_shared_state do
it_behaves_like 'returns in_progress based on Sidekiq::Status'
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