Skip to content

Commit 4062911

Browse files
authored
Allow combining on/off idle_duration between runs and fleets (#3756)
1 parent 5b1233c commit 4062911

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/dstack/_internal/server/services/requirements/combine.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ def _combine_spot_policy_optional(
128128

129129

130130
def _combine_idle_duration(value1: int, value2: int) -> int:
131-
if value1 < 0 and value2 >= 0 or value2 < 0 and value1 >= 0:
132-
raise CombineError(f"idle_duration values {value1} and {value2} cannot be combined")
131+
if value1 < 0:
132+
if value2 < 0:
133+
return min(value1, value2)
134+
return value2
135+
if value2 < 0:
136+
return value1
133137
return min(value1, value2)
134138

135139

src/tests/_internal/server/services/requirements/test_combine.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ def test_returns_the_same_profile_if_profiles_identical(self):
4343
)
4444
assert combine_fleet_and_run_profiles(profile, profile) == profile
4545

46+
def test_prefers_finite_idle_duration_over_off(self):
47+
combined_profile = combine_fleet_and_run_profiles(
48+
Profile(idle_duration=300),
49+
Profile(idle_duration=-1),
50+
)
51+
52+
assert combined_profile is not None
53+
assert combined_profile.idle_duration == 300
54+
4655
@pytest.mark.parametrize(
4756
argnames=["fleet_profile", "run_profile", "expected_profile"],
4857
argvalues=[
@@ -218,27 +227,19 @@ def test_both_negative_returns_minimum(self):
218227
def test_both_zero_returns_zero(self):
219228
assert _combine_idle_duration_optional(0, 0) == 0
220229

221-
def test_positive_and_negative_raises_error(self):
222-
with pytest.raises(
223-
CombineError, match="idle_duration values 3600 and -1 cannot be combined"
224-
):
225-
_combine_idle_duration_optional(3600, -1)
230+
def test_positive_and_negative_returns_positive(self):
231+
assert _combine_idle_duration_optional(3600, -1) == 3600
226232

227-
def test_negative_and_positive_raises_error(self):
228-
with pytest.raises(
229-
CombineError, match="idle_duration values -1 and 3600 cannot be combined"
230-
):
231-
_combine_idle_duration_optional(-1, 3600)
233+
def test_negative_and_positive_returns_positive(self):
234+
assert _combine_idle_duration_optional(-1, 3600) == 3600
232235

233236
def test_zero_and_positive_returns_zero(self):
234237
assert _combine_idle_duration_optional(0, 3600) == 0
235238
assert _combine_idle_duration_optional(3600, 0) == 0
236239

237-
def test_zero_and_negative_raises_error(self):
238-
with pytest.raises(CombineError, match="idle_duration values 0 and -1 cannot be combined"):
239-
_combine_idle_duration_optional(0, -1)
240-
with pytest.raises(CombineError, match="idle_duration values -1 and 0 cannot be combined"):
241-
_combine_idle_duration_optional(-1, 0)
240+
def test_zero_and_negative_returns_zero(self):
241+
assert _combine_idle_duration_optional(0, -1) == 0
242+
assert _combine_idle_duration_optional(-1, 0) == 0
242243

243244

244245
class TestCombineSpotPolicy:

0 commit comments

Comments
 (0)