Skip to content

Commit

Permalink
Tidy up environments that could not even be scheduled with the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sersorrel committed Feb 1, 2024
1 parent c6687ce commit 26e7aed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 4 additions & 6 deletions softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore
if input_err is not None:
return input_err

name = env.name
versionless_name = env.name
version = 1

while True:
env.name = name + "-" + str(version)
env.name = versionless_name + "-" + str(version)
response = cls.create_new_env(
env, Artifacts.built_by_softpack_file
)
Expand All @@ -267,16 +267,14 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore

version += 1

env.name = name

# Send build request
try:
host = app.settings.builder.host
port = app.settings.builder.port
r = httpx.post(
f"http://{host}:{port}/environments/build",
json={
"name": f"{env.path}/{env.name}",
"name": f"{env.path}/{versionless_name}",
"version": str(version),
"model": {
"description": env.description,
Expand All @@ -292,7 +290,7 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore
)
r.raise_for_status()
except Exception as e:
# TODO: clean up scheduled build in repo
cls.delete(env.name, env.path)
return BuilderError(
message="Connection to builder failed: "
+ "".join(format_exception_only(type(e), e))
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
State,
UpdateEnvironmentSuccess,
WriteArtifactSuccess,
BuilderError,
)
from tests.integration.utils import file_in_remote

Expand Down Expand Up @@ -126,6 +127,22 @@ def test_create_path_invalid_disallowed(httpx_post, testable_env_input):
assert isinstance(result, InvalidInputError)


def test_create_cleans_up_after_builder_failure(httpx_post, testable_env_input):
httpx_post.side_effect = Exception('could not contact builder')
result = Environment.create(testable_env_input)
assert isinstance(result, BuilderError)

dir = Path(
Environment.artifacts.environments_root,
testable_env_input.path,
testable_env_input.name + "-1",
)
builtPath = dir / Environment.artifacts.built_by_softpack_file
ymlPath = dir / Environment.artifacts.environments_file
assert not file_in_remote(builtPath)
assert not file_in_remote(ymlPath)


def builder_called_correctly(
post_mock, testable_env_input: EnvironmentInput
) -> None:
Expand Down

0 comments on commit 26e7aed

Please sign in to comment.