Skip to content

Commit

Permalink
Fix environment variable not accepting lists (#11722)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Pickett <[email protected]>
  • Loading branch information
clefelhocz2 and bunchesofdonald authored Jan 26, 2024
1 parent 74c0e0e commit 7b1611f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/prefect/server/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def create_app(
and services will be disabled.
"""
settings = settings or prefect.settings.get_current_settings()
cache_key = (settings, ephemeral)
cache_key = (settings.hash_key(), ephemeral)

if cache_key in APP_CACHE and not ignore_cache:
return APP_CACHE[cache_key]
Expand Down
8 changes: 8 additions & 0 deletions src/prefect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,14 @@ def with_obfuscated_secrets(self):
settings.__fields_set__.intersection_update(self.__fields_set__)
return settings

def hash_key(self) -> str:
"""
Return a hash key for the settings object. This is needed since some
settings may be unhashable. An example is lists.
"""
env_variables = self.to_environment_variables()
return str(hash(tuple((key, value) for key, value in env_variables.items())))

def to_environment_variables(
self, include: Iterable[Setting] = None, exclude_unset: bool = False
) -> Dict[str, str]:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ async def test_global_task_retry_delay_seconds(
self, mock_anyio_sleep, prefect_client, flow_run, result_factory
):
with temporary_settings(
updates={PREFECT_TASK_DEFAULT_RETRY_DELAY_SECONDS: "43"}
updates={PREFECT_TASK_DEFAULT_RETRY_DELAY_SECONDS: [3, 5, 9]}
):
# the flow run must be running prior to running tasks
await prefect_client.set_flow_run_state(
Expand All @@ -1739,14 +1739,14 @@ async def test_global_task_retry_delay_seconds(
# Define a task that fails once and then succeeds
mock = MagicMock()

@task(retries=1)
@task(retries=3)
def flaky_function():
mock()

if mock.call_count == 2:
if mock.call_count == 4:
return 1

raise ValueError("try again, but only once")
raise ValueError("try again")

# Create a task run to test
task_run = await prefect_client.create_task_run(
Expand All @@ -1757,7 +1757,7 @@ def flaky_function():
)

# Actually run the task
with mock_anyio_sleep.assert_sleeps_for(43):
with mock_anyio_sleep.assert_sleeps_for(17):
await orchestrate_task_run(
task=flaky_function,
task_run=task_run,
Expand All @@ -1769,6 +1769,8 @@ def flaky_function():
log_prints=False,
)

assert mock_anyio_sleep.await_count == 3

async def test_retry_condition_fn_retries_after_failure(
self, mock_anyio_sleep, prefect_client, flow_run, result_factory
):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ def test_settings_to_environment_does_not_use_value_callback(self):
# generating the environment variables
assert "PREFECT_UI_API_URL" not in settings.to_environment_variables()

def test_settings_hash_key(self):
settings = Settings(PREFECT_TEST_MODE=True)
diff_settings = Settings(PREFECT_TEST_MODE=False)

assert settings.hash_key() == settings.hash_key()

assert settings.hash_key() != diff_settings.hash_key()

@pytest.mark.parametrize(
"log_level_setting",
[
Expand Down

0 comments on commit 7b1611f

Please sign in to comment.