diff --git a/app/models/environment.rb b/app/models/environment.rb index 0bbb2b394c0ffc35d93ded599c7ebf358e218cde..446d458fa0d6b974f2130af6f0157159717aa696 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 0000000000000000000000000000000000000000..e5058a6df38fdd1d050f33f8d5c7e07aada40276 --- /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 0000000000000000000000000000000000000000..060654b8185393c82e3f55e51da680a9ffdebc10 --- /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 134d297502be6431faf46923d3741c5bbe5c5056..c39f9dc61f65848b5518174ffce017e8d7b9abaf 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 1242a139394345d004bd3f991f966ce03d3c7da4..89ecab0205b6a2ab20a3ae2643165d23f92929fc 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 2c6ac42d26bc4873000df5396047587dd0d2b963..832fdbd597e14b4d0e019fb6f231857a1cab1dad 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