From d5792a14823eaf5da88bc84d9ba380a7d5d1db83 Mon Sep 17 00:00:00 2001 From: Tiger Date: Tue, 26 Nov 2024 13:12:44 +1300 Subject: [PATCH] Add auto stop setting to environments Currently environments are stopped when a merge request is closed or branch is deleted, even if there is no associated stop job. This setting will be used to allow users to switch between this behaviour and only stopping when a stop job is present. Changelog: added --- app/models/environment.rb | 9 ++++++++- ...25222645_add_stop_setting_to_environments.rb | 9 +++++++++ db/schema_migrations/20241125222645 | 1 + db/structure.sql | 1 + spec/factories/environments.rb | 8 ++++++++ spec/models/environment_spec.rb | 17 +++++++++++++---- 6 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20241125222645_add_stop_setting_to_environments.rb create mode 100644 db/schema_migrations/20241125222645 diff --git a/app/models/environment.rb b/app/models/environment.rb index 0bbb2b394c0ffc..446d458fa0d6b9 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -201,6 +201,11 @@ class Environment < ApplicationRecord other: 4 } + enum auto_stop_setting: { + always: 0, + with_action: 1 + }, _prefix: true + state_machine :state, initial: :available do event :start do transition stopped: :available @@ -372,7 +377,9 @@ def wait_for_stop? def stop_with_actions! return unless available? - stop! + if stop_actions.any? || auto_stop_setting_always? + stop! + end actions = [] diff --git a/db/migrate/20241125222645_add_stop_setting_to_environments.rb b/db/migrate/20241125222645_add_stop_setting_to_environments.rb new file mode 100644 index 00000000000000..e5058a6df38fdd --- /dev/null +++ b/db/migrate/20241125222645_add_stop_setting_to_environments.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddStopSettingToEnvironments < Gitlab::Database::Migration[2.2] + milestone '17.7' + + def change + add_column :environments, :auto_stop_setting, :smallint, null: false, default: 0 + end +end diff --git a/db/schema_migrations/20241125222645 b/db/schema_migrations/20241125222645 new file mode 100644 index 00000000000000..060654b8185393 --- /dev/null +++ b/db/schema_migrations/20241125222645 @@ -0,0 +1 @@ +0d0ab0290506bdb66433a7e6e5314423a00e30793d69ad26b155d7c6755c2033 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 134d297502be64..c39f9dc61f6584 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -12032,6 +12032,7 @@ CREATE TABLE environments ( description text, description_html text, cached_markdown_version integer, + auto_stop_setting smallint DEFAULT 0 NOT NULL, CONSTRAINT check_23b1eb18a2 CHECK ((char_length(flux_resource_path) <= 255)), CONSTRAINT check_ad5e1ed5e1 CHECK ((char_length(description) <= 10000)), CONSTRAINT check_b5373a1804 CHECK ((char_length(kubernetes_namespace) <= 63)), diff --git a/spec/factories/environments.rb b/spec/factories/environments.rb index 1242a139394345..89ecab0205b6a2 100644 --- a/spec/factories/environments.rb +++ b/spec/factories/environments.rb @@ -108,5 +108,13 @@ trait :will_auto_stop do auto_stop_at { 1.day.from_now } end + + trait :auto_stop_always do + auto_stop_setting { :always } + end + + trait :auto_stop_with_action do + auto_stop_setting { :with_action } + end end end diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 2c6ac42d26bc48..832fdbd597e14b 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -804,10 +804,6 @@ subject { environment.stop_with_actions! } shared_examples_for 'stop with playing a teardown job' do - before do - expect(environment).to receive(:available?).and_call_original - end - context 'when no other actions' do context 'environment is available' do before do @@ -820,6 +816,19 @@ expect(environment).to be_stopped expect(actions).to match_array([]) end + + context 'when the auto stop setting is set to :with_action' do + before do + environment.update!(auto_stop_setting: :with_action) + end + + it 'does not stop the environment' do + actions = subject + + expect(environment).to be_available + expect(actions).to match_array([]) + end + end end context 'environment is already stopped' do -- GitLab