summaryrefslogtreecommitdiff
path: root/app/services/environments/stop_stale_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/environments/stop_stale_service.rb')
-rw-r--r--app/services/environments/stop_stale_service.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/app/services/environments/stop_stale_service.rb b/app/services/environments/stop_stale_service.rb
new file mode 100644
index 00000000000..7b7032cb670
--- /dev/null
+++ b/app/services/environments/stop_stale_service.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module Environments
+ class StopStaleService < BaseService
+ def execute
+ return ServiceResponse.error(message: 'Before date must be provided') unless params[:before].present?
+
+ return ServiceResponse.error(message: 'Unauthorized') unless can?(current_user, :stop_environment, project)
+
+ Environment.available
+ .deployed_and_updated_before(project.id, params[:before])
+ .without_protected(project)
+ .in_batches(of: 100) do |env_batch| # rubocop:disable Cop/InBatches
+ Environments::AutoStopWorker.bulk_perform_async_with_contexts(
+ env_batch,
+ arguments_proc: ->(environment) { environment.id },
+ context_proc: ->(environment) { { project: project } }
+ )
+ end
+
+ ServiceResponse.success(message: 'Successfully requested stop for all stale environments')
+ end
+ end
+end