diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-11-25 01:13:57 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-11-25 01:13:57 +0900 |
commit | 3886ee2f3d5e1764a3508c115793bef5d57566ef (patch) | |
tree | be3b3117970d400e7b84b2ebcd0a2fce3c1ed08c | |
parent | cf631ddbdceec49579a658698f11679a29cea579 (diff) | |
download | gitlab-ce-14879-cluster-workers-refactoring.tar.gz |
Cluster workers refactoring14879-cluster-workers-refactoring
29 files changed, 100 insertions, 88 deletions
diff --git a/app/services/clusters/applications/check_installation_progress_service.rb b/app/services/clusters/applications/check_installation_progress_service.rb index bde090eaeec..fd957e29369 100644 --- a/app/services/clusters/applications/check_installation_progress_service.rb +++ b/app/services/clusters/applications/check_installation_progress_service.rb @@ -38,13 +38,13 @@ module Clusters remove_installation_pod end else - ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) + Clusters::ClusterWaitForAppInstallationWorker.perform_in( + Clusters::ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) end end def timeouted? - Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT + Time.now.utc - app.updated_at.to_time.utc > Clusters::ClusterWaitForAppInstallationWorker::TIMEOUT end def remove_installation_pod diff --git a/app/services/clusters/applications/install_service.rb b/app/services/clusters/applications/install_service.rb index 8ceeec687cd..f81a625979f 100644 --- a/app/services/clusters/applications/install_service.rb +++ b/app/services/clusters/applications/install_service.rb @@ -8,8 +8,8 @@ module Clusters app.make_installing! helm_api.install(install_command) - ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) + Clusters::ClusterWaitForAppInstallationWorker.perform_in( + Clusters::ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) rescue KubeException => ke app.make_errored!("Kubernetes error: #{ke.message}") rescue StandardError diff --git a/app/services/clusters/applications/schedule_installation_service.rb b/app/services/clusters/applications/schedule_installation_service.rb index eb8caa68ef7..2c455b14534 100644 --- a/app/services/clusters/applications/schedule_installation_service.rb +++ b/app/services/clusters/applications/schedule_installation_service.rb @@ -4,7 +4,7 @@ module Clusters def execute application_class.find_or_create_by!(cluster: cluster).try do |application| application.make_scheduled! - ClusterInstallAppWorker.perform_async(application.name, application.id) + Clusters::ClusterInstallAppWorker.perform_async(application.name, application.id) end end diff --git a/app/services/clusters/create_service.rb b/app/services/clusters/create_service.rb index 1d407739b21..c1c5772878e 100644 --- a/app/services/clusters/create_service.rb +++ b/app/services/clusters/create_service.rb @@ -6,7 +6,7 @@ module Clusters @access_token = access_token create_cluster.tap do |cluster| - ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted? + Clusters::ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted? end end diff --git a/app/services/clusters/gcp/provision_service.rb b/app/services/clusters/gcp/provision_service.rb index 8beea5a8cfb..2aa1e3b0e72 100644 --- a/app/services/clusters/gcp/provision_service.rb +++ b/app/services/clusters/gcp/provision_service.rb @@ -8,7 +8,7 @@ module Clusters get_operation_id do |operation_id| if provider.make_creating(operation_id) - WaitForClusterCreationWorker.perform_in( + Clusters::WaitForClusterCreationWorker.perform_in( Clusters::Gcp::VerifyProvisionStatusService::INITIAL_INTERVAL, provider.cluster_id) else diff --git a/app/services/clusters/gcp/verify_provision_status_service.rb b/app/services/clusters/gcp/verify_provision_status_service.rb index bc33756f27c..833ad56529e 100644 --- a/app/services/clusters/gcp/verify_provision_status_service.rb +++ b/app/services/clusters/gcp/verify_provision_status_service.rb @@ -26,7 +26,7 @@ module Clusters def continue_creation(operation) if elapsed_time_from_creation(operation) < TIMEOUT - WaitForClusterCreationWorker.perform_in(EAGER_INTERVAL, provider.cluster_id) + Clusters::WaitForClusterCreationWorker.perform_in(EAGER_INTERVAL, provider.cluster_id) else provider.make_errored!("Cluster creation time exceeds timeout; #{TIMEOUT}") end diff --git a/app/workers/cluster_install_app_worker.rb b/app/workers/cluster_install_app_worker.rb deleted file mode 100644 index 899aed904e4..00000000000 --- a/app/workers/cluster_install_app_worker.rb +++ /dev/null @@ -1,11 +0,0 @@ -class ClusterInstallAppWorker - include Sidekiq::Worker - include ClusterQueue - include ClusterApplications - - def perform(app_name, app_id) - find_application(app_name, app_id) do |app| - Clusters::Applications::InstallService.new(app).execute - end - end -end diff --git a/app/workers/cluster_provision_worker.rb b/app/workers/cluster_provision_worker.rb deleted file mode 100644 index b01f9708424..00000000000 --- a/app/workers/cluster_provision_worker.rb +++ /dev/null @@ -1,12 +0,0 @@ -class ClusterProvisionWorker - include Sidekiq::Worker - include ClusterQueue - - def perform(cluster_id) - Clusters::Cluster.find_by_id(cluster_id).try do |cluster| - cluster.provider.try do |provider| - Clusters::Gcp::ProvisionService.new.execute(provider) if cluster.gcp? - end - end - end -end diff --git a/app/workers/cluster_wait_for_app_installation_worker.rb b/app/workers/cluster_wait_for_app_installation_worker.rb deleted file mode 100644 index 4bb8c293e5d..00000000000 --- a/app/workers/cluster_wait_for_app_installation_worker.rb +++ /dev/null @@ -1,14 +0,0 @@ -class ClusterWaitForAppInstallationWorker - include Sidekiq::Worker - include ClusterQueue - include ClusterApplications - - INTERVAL = 10.seconds - TIMEOUT = 20.minutes - - def perform(app_name, app_id) - find_application(app_name, app_id) do |app| - Clusters::Applications::CheckInstallationProgressService.new(app).execute - end - end -end diff --git a/app/workers/clusters/cluster_install_app_worker.rb b/app/workers/clusters/cluster_install_app_worker.rb new file mode 100644 index 00000000000..6a26aa418e7 --- /dev/null +++ b/app/workers/clusters/cluster_install_app_worker.rb @@ -0,0 +1,13 @@ +module Clusters + class ClusterInstallAppWorker + include Sidekiq::Worker + include ClusterQueue + include Concerns::ClusterApplications + + def perform(app_name, app_id) + find_application(app_name, app_id) do |app| + Clusters::Applications::InstallService.new(app).execute + end + end + end +end diff --git a/app/workers/clusters/cluster_provision_worker.rb b/app/workers/clusters/cluster_provision_worker.rb new file mode 100644 index 00000000000..b5dd41ba42e --- /dev/null +++ b/app/workers/clusters/cluster_provision_worker.rb @@ -0,0 +1,14 @@ +module Clusters + class ClusterProvisionWorker + include Sidekiq::Worker + include ClusterQueue + + def perform(cluster_id) + Clusters::Cluster.find_by_id(cluster_id).try do |cluster| + cluster.provider.try do |provider| + Clusters::Gcp::ProvisionService.new.execute(provider) if cluster.gcp? + end + end + end + end +end diff --git a/app/workers/clusters/cluster_wait_for_app_installation_worker.rb b/app/workers/clusters/cluster_wait_for_app_installation_worker.rb new file mode 100644 index 00000000000..bdbd7178558 --- /dev/null +++ b/app/workers/clusters/cluster_wait_for_app_installation_worker.rb @@ -0,0 +1,16 @@ +module Clusters + class ClusterWaitForAppInstallationWorker + include Sidekiq::Worker + include ClusterQueue + include Concerns::ClusterApplications + + INTERVAL = 10.seconds + TIMEOUT = 20.minutes + + def perform(app_name, app_id) + find_application(app_name, app_id) do |app| + Clusters::Applications::CheckInstallationProgressService.new(app).execute + end + end + end +end diff --git a/app/workers/clusters/concerns/cluster_applications.rb b/app/workers/clusters/concerns/cluster_applications.rb new file mode 100644 index 00000000000..56beb4fb0a4 --- /dev/null +++ b/app/workers/clusters/concerns/cluster_applications.rb @@ -0,0 +1,13 @@ +module Clusters + module Concerns + module ClusterApplications + extend ActiveSupport::Concern + + included do + def find_application(app_name, id, &blk) + Clusters::Cluster::APPLICATIONS[app_name].find(id).try(&blk) + end + end + end + end +end diff --git a/app/workers/clusters/wait_for_cluster_creation_worker.rb b/app/workers/clusters/wait_for_cluster_creation_worker.rb new file mode 100644 index 00000000000..712250ecb4c --- /dev/null +++ b/app/workers/clusters/wait_for_cluster_creation_worker.rb @@ -0,0 +1,14 @@ +module Clusters + class Clusters::WaitForClusterCreationWorker + include Sidekiq::Worker + include ClusterQueue + + def perform(cluster_id) + Clusters::Cluster.find_by_id(cluster_id).try do |cluster| + cluster.provider.try do |provider| + Clusters::Gcp::VerifyProvisionStatusService.new.execute(provider) if cluster.gcp? + end + end + end + end +end diff --git a/app/workers/concerns/cluster_applications.rb b/app/workers/concerns/cluster_applications.rb deleted file mode 100644 index 24ecaa0b52f..00000000000 --- a/app/workers/concerns/cluster_applications.rb +++ /dev/null @@ -1,9 +0,0 @@ -module ClusterApplications - extend ActiveSupport::Concern - - included do - def find_application(app_name, id, &blk) - Clusters::Cluster::APPLICATIONS[app_name].find(id).try(&blk) - end - end -end diff --git a/app/workers/wait_for_cluster_creation_worker.rb b/app/workers/wait_for_cluster_creation_worker.rb deleted file mode 100644 index 241ed3901dc..00000000000 --- a/app/workers/wait_for_cluster_creation_worker.rb +++ /dev/null @@ -1,12 +0,0 @@ -class WaitForClusterCreationWorker - include Sidekiq::Worker - include ClusterQueue - - def perform(cluster_id) - Clusters::Cluster.find_by_id(cluster_id).try do |cluster| - cluster.provider.try do |provider| - Clusters::Gcp::VerifyProvisionStatusService.new.execute(provider) if cluster.gcp? - end - end - end -end diff --git a/spec/controllers/projects/clusters/applications_controller_spec.rb b/spec/controllers/projects/clusters/applications_controller_spec.rb index 8b460646059..659c664b893 100644 --- a/spec/controllers/projects/clusters/applications_controller_spec.rb +++ b/spec/controllers/projects/clusters/applications_controller_spec.rb @@ -22,7 +22,7 @@ describe Projects::Clusters::ApplicationsController do end it 'schedule an application installation' do - expect(ClusterInstallAppWorker).to receive(:perform_async).with(application, anything).once + expect(Clusters::ClusterInstallAppWorker).to receive(:perform_async).with(application, anything).once expect { go }.to change { current_application.count } expect(response).to have_http_status(:no_content) @@ -65,7 +65,7 @@ describe Projects::Clusters::ApplicationsController do describe 'security' do before do - allow(ClusterInstallAppWorker).to receive(:perform_async) + allow(Clusters::ClusterInstallAppWorker).to receive(:perform_async) end it { expect { go }.to be_allowed_for(:admin) } diff --git a/spec/controllers/projects/clusters_controller_spec.rb b/spec/controllers/projects/clusters_controller_spec.rb index ca2bcb2b5ae..c35eda72713 100644 --- a/spec/controllers/projects/clusters_controller_spec.rb +++ b/spec/controllers/projects/clusters_controller_spec.rb @@ -195,7 +195,7 @@ describe Projects::ClustersController do context 'when creates a cluster on gke' do it 'creates a new cluster' do - expect(ClusterProvisionWorker).to receive(:perform_async) + expect(Clusters::ClusterProvisionWorker).to receive(:perform_async) expect { go }.to change { Clusters::Cluster.count } expect(response).to redirect_to(project_cluster_path(project, project.cluster)) end diff --git a/spec/factories/clusters/applications/helm.rb b/spec/factories/clusters/applications/helm.rb index fab37195113..032bf542e9e 100644 --- a/spec/factories/clusters/applications/helm.rb +++ b/spec/factories/clusters/applications/helm.rb @@ -29,7 +29,7 @@ FactoryGirl.define do trait :timeouted do installing - updated_at ClusterWaitForAppInstallationWorker::TIMEOUT.ago + updated_at Clusters::ClusterWaitForAppInstallationWorker::TIMEOUT.ago end end end diff --git a/spec/factories/clusters/applications/ingress.rb b/spec/factories/clusters/applications/ingress.rb index b103a980655..85db97dd2a2 100644 --- a/spec/factories/clusters/applications/ingress.rb +++ b/spec/factories/clusters/applications/ingress.rb @@ -29,7 +29,7 @@ FactoryGirl.define do trait :timeouted do installing - updated_at ClusterWaitForAppInstallationWorker::TIMEOUT.ago + updated_at Clusters::ClusterWaitForAppInstallationWorker::TIMEOUT.ago end end end diff --git a/spec/features/projects/clusters_spec.rb b/spec/features/projects/clusters_spec.rb index 197e6df4997..e25766b33a6 100644 --- a/spec/features/projects/clusters_spec.rb +++ b/spec/features/projects/clusters_spec.rb @@ -40,7 +40,7 @@ feature 'Clusters', :js do .to receive(:projects_zones_clusters_create).and_return(dbl) end - allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil) + allow(Clusters::WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil) fill_in 'cluster_provider_gcp_attributes_gcp_project_id', with: 'gcp-project-123' fill_in 'cluster_name', with: 'dev-cluster' @@ -102,7 +102,7 @@ feature 'Clusters', :js do context 'when user installs application: Helm Tiller' do before do - allow(ClusterInstallAppWorker).to receive(:perform_async).and_return(nil) + allow(Clusters::ClusterInstallAppWorker).to receive(:perform_async).and_return(nil) page.within('.js-cluster-application-row-helm') do page.find(:css, '.js-cluster-application-install-button').click @@ -133,7 +133,7 @@ feature 'Clusters', :js do context 'when user installs application: Ingress' do before do - allow(ClusterInstallAppWorker).to receive(:perform_async).and_return(nil) + allow(Clusters::ClusterInstallAppWorker).to receive(:perform_async).and_return(nil) # Helm Tiller needs to be installed before you can install Ingress create(:cluster_applications_helm, :installed, cluster: cluster) diff --git a/spec/services/clusters/applications/check_installation_progress_service_spec.rb b/spec/services/clusters/applications/check_installation_progress_service_spec.rb index 75fc05d36e9..cbde9e78a8e 100644 --- a/spec/services/clusters/applications/check_installation_progress_service_spec.rb +++ b/spec/services/clusters/applications/check_installation_progress_service_spec.rb @@ -22,7 +22,7 @@ describe Clusters::Applications::CheckInstallationProgressService do context "when phase is #{a_phase}" do context 'when not timeouted' do it 'reschedule a new check' do - expect(ClusterWaitForAppInstallationWorker).to receive(:perform_in).once + expect(Clusters::ClusterWaitForAppInstallationWorker).to receive(:perform_in).once expect(service).not_to receive(:remove_installation_pod) service.execute @@ -38,7 +38,7 @@ describe Clusters::Applications::CheckInstallationProgressService do it_behaves_like 'a terminated installation' it 'make the application errored' do - expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in) + expect(Clusters::ClusterWaitForAppInstallationWorker).not_to receive(:perform_in) service.execute @@ -63,7 +63,7 @@ describe Clusters::Applications::CheckInstallationProgressService do it_behaves_like 'a terminated installation' it 'make the application installed' do - expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in) + expect(Clusters::ClusterWaitForAppInstallationWorker).not_to receive(:perform_in) service.execute diff --git a/spec/services/clusters/applications/install_service_spec.rb b/spec/services/clusters/applications/install_service_spec.rb index 054a49ffedf..b8f001fc2a1 100644 --- a/spec/services/clusters/applications/install_service_spec.rb +++ b/spec/services/clusters/applications/install_service_spec.rb @@ -13,7 +13,7 @@ describe Clusters::Applications::InstallService do context 'when there are no errors' do before do expect(helm_client).to receive(:install).with(application.install_command) - allow(ClusterWaitForAppInstallationWorker).to receive(:perform_in).and_return(nil) + allow(Clusters::ClusterWaitForAppInstallationWorker).to receive(:perform_in).and_return(nil) end it 'make the application installing' do @@ -24,7 +24,7 @@ describe Clusters::Applications::InstallService do end it 'schedule async installation status check' do - expect(ClusterWaitForAppInstallationWorker).to receive(:perform_in).once + expect(Clusters::ClusterWaitForAppInstallationWorker).to receive(:perform_in).once service.execute end diff --git a/spec/services/clusters/applications/schedule_installation_service_spec.rb b/spec/services/clusters/applications/schedule_installation_service_spec.rb index cf95361c935..685223dae3f 100644 --- a/spec/services/clusters/applications/schedule_installation_service_spec.rb +++ b/spec/services/clusters/applications/schedule_installation_service_spec.rb @@ -7,7 +7,7 @@ describe Clusters::Applications::ScheduleInstallationService do shared_examples 'a failing service' do it 'raise an exception' do - expect(ClusterInstallAppWorker).not_to receive(:perform_async) + expect(Clusters::ClusterInstallAppWorker).not_to receive(:perform_async) count_before = count_scheduled expect { service.execute }.to raise_error(StandardError) @@ -26,7 +26,7 @@ describe Clusters::Applications::ScheduleInstallationService do end it 'make the application scheduled' do - expect(ClusterInstallAppWorker).to receive(:perform_async).with(application_class.application_name, kind_of(Numeric)).once + expect(Clusters::ClusterInstallAppWorker).to receive(:perform_async).with(application_class.application_name, kind_of(Numeric)).once expect { service.execute }.to change { application_class.with_status(:scheduled).count }.by(1) end diff --git a/spec/services/clusters/create_service_spec.rb b/spec/services/clusters/create_service_spec.rb index 5b6edb73beb..09ebe051116 100644 --- a/spec/services/clusters/create_service_spec.rb +++ b/spec/services/clusters/create_service_spec.rb @@ -22,7 +22,7 @@ describe Clusters::CreateService do end it 'creates a cluster object and performs a worker' do - expect(ClusterProvisionWorker).to receive(:perform_async) + expect(Clusters::ClusterProvisionWorker).to receive(:perform_async) expect { result } .to change { Clusters::Cluster.count }.by(1) @@ -55,7 +55,7 @@ describe Clusters::CreateService do end it 'returns an error' do - expect(ClusterProvisionWorker).not_to receive(:perform_async) + expect(Clusters::ClusterProvisionWorker).not_to receive(:perform_async) expect { result }.to change { Clusters::Cluster.count }.by(0) expect(result.errors[:"provider_gcp.gcp_project_id"]).to be_present end diff --git a/spec/services/clusters/gcp/provision_service_spec.rb b/spec/services/clusters/gcp/provision_service_spec.rb index f48afdc83b2..9c88efb650b 100644 --- a/spec/services/clusters/gcp/provision_service_spec.rb +++ b/spec/services/clusters/gcp/provision_service_spec.rb @@ -10,7 +10,7 @@ describe Clusters::Gcp::ProvisionService do shared_examples 'success' do it 'schedules a worker for status minitoring' do - expect(WaitForClusterCreationWorker).to receive(:perform_in) + expect(Clusters::WaitForClusterCreationWorker).to receive(:perform_in) described_class.new.execute(provider) diff --git a/spec/services/clusters/gcp/verify_provision_status_service_spec.rb b/spec/services/clusters/gcp/verify_provision_status_service_spec.rb index 2ee2fa51f63..902b2876f15 100644 --- a/spec/services/clusters/gcp/verify_provision_status_service_spec.rb +++ b/spec/services/clusters/gcp/verify_provision_status_service_spec.rb @@ -11,7 +11,7 @@ describe Clusters::Gcp::VerifyProvisionStatusService do shared_examples 'continue_creation' do it 'schedules a worker for status minitoring' do - expect(WaitForClusterCreationWorker).to receive(:perform_in) + expect(Clusters::WaitForClusterCreationWorker).to receive(:perform_in) described_class.new.execute(provider) end diff --git a/spec/workers/cluster_provision_worker_spec.rb b/spec/workers/cluster_provision_worker_spec.rb index 8054ec11a48..3e1b914def6 100644 --- a/spec/workers/cluster_provision_worker_spec.rb +++ b/spec/workers/cluster_provision_worker_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ClusterProvisionWorker do +describe Clusters::ClusterProvisionWorker do describe '#perform' do context 'when provider type is gcp' do let(:cluster) { create(:cluster, provider_type: :gcp, provider_gcp: provider) } diff --git a/spec/workers/wait_for_cluster_creation_worker_spec.rb b/spec/workers/wait_for_cluster_creation_worker_spec.rb index 0e92b298178..5835f43f20f 100644 --- a/spec/workers/wait_for_cluster_creation_worker_spec.rb +++ b/spec/workers/wait_for_cluster_creation_worker_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe WaitForClusterCreationWorker do +describe Clusters::WaitForClusterCreationWorker do describe '#perform' do context 'when provider type is gcp' do let(:cluster) { create(:cluster, provider_type: :gcp, provider_gcp: provider) } |