2020-07-06 05:09:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Groups
|
|
|
|
class UpdateSharedRunnersService < Groups::BaseService
|
|
|
|
def execute
|
|
|
|
return error('Operation not allowed', 403) unless can?(current_user, :admin_group, group)
|
|
|
|
|
|
|
|
validate_params
|
|
|
|
|
2020-09-30 11:09:46 -04:00
|
|
|
update_shared_runners
|
2021-12-09 19:13:05 -05:00
|
|
|
update_pending_builds_async
|
2020-07-06 05:09:20 -04:00
|
|
|
|
|
|
|
success
|
|
|
|
|
2020-09-30 11:09:46 -04:00
|
|
|
rescue ActiveRecord::RecordInvalid, ArgumentError => error
|
2020-07-06 05:09:20 -04:00
|
|
|
error(error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_params
|
2020-09-30 11:09:46 -04:00
|
|
|
unless Namespace::SHARED_RUNNERS_SETTINGS.include?(params[:shared_runners_setting])
|
|
|
|
raise ArgumentError, "state must be one of: #{Namespace::SHARED_RUNNERS_SETTINGS.join(', ')}"
|
2020-07-06 05:09:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-30 11:09:46 -04:00
|
|
|
def update_shared_runners
|
|
|
|
group.update_shared_runners_setting!(params[:shared_runners_setting])
|
2020-07-06 05:09:20 -04:00
|
|
|
end
|
2021-09-02 20:09:01 -04:00
|
|
|
|
2021-12-09 19:13:05 -05:00
|
|
|
def update_pending_builds?
|
|
|
|
group.previous_changes.include?('shared_runners_enabled')
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_pending_builds_async
|
|
|
|
return unless update_pending_builds?
|
2021-09-02 20:09:01 -04:00
|
|
|
|
2021-12-09 19:13:05 -05:00
|
|
|
group.run_after_commit_or_now do |group|
|
|
|
|
pending_builds_params = { instance_runners_enabled: group.shared_runners_enabled }
|
2021-09-02 20:09:01 -04:00
|
|
|
|
2021-12-09 19:13:05 -05:00
|
|
|
::Ci::UpdatePendingBuildService.new(group, pending_builds_params).execute
|
|
|
|
end
|
2021-09-02 20:09:01 -04:00
|
|
|
end
|
2020-07-06 05:09:20 -04:00
|
|
|
end
|
|
|
|
end
|