Class: Environments::StopStaleService

Inherits:
BaseService show all
Defined in:
app/services/environments/stop_stale_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#executeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/environments/stop_stale_service.rb', line 5

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