Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion proxy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,11 @@ def set_deployment_schedule(deployment_id: str, status: str) -> None:
del schedule["updated"]
del schedule["deployment_id"]
schedule["active"] = status == "active"
prefect_patch(f"deployments/{deployment_id}", {"schedules": [schedule]})

prefect_patch(
f"deployments/{deployment_id}",
{"schedules": [schedule], "paused": status == "inactive"},
)
Comment on lines +1167 to +1171
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Always patch paused (even when there are no schedules) and update all schedules, not just the first

Current flow skips patching entirely if the deployment has zero schedules, so pause/resume never takes effect in that case. Also, by patching with only the first schedule, you’ll inadvertently drop any additional schedules on the deployment.

Refactor to:

  • Validate status ("active" | "inactive").
  • Always patch "paused", regardless of schedules presence.
  • Update the active flag on all schedules and preserve the entire schedules list.

Apply this diff:

 def set_deployment_schedule(deployment_id: str, status: str) -> None:
     """Set deployment schedule to active or inactive"""
     # both the apis return null below
     deployment = prefect_get(f"deployments/{deployment_id}")
-    if len(deployment["schedules"]) > 0:
-        schedule = deployment["schedules"][0]
-        del schedule["id"]
-        del schedule["created"]
-        del schedule["updated"]
-        del schedule["deployment_id"]
-        schedule["active"] = status == "active"
-
-        prefect_patch(
-            f"deployments/{deployment_id}",
-            {"schedules": [schedule], "paused": status == "inactive"},
-        )
+    allowed = {"active", "inactive"}
+    if status not in allowed:
+        raise ValueError(f"status must be one of {allowed}")
+
+    paused = status == "inactive"
+    payload = {"paused": paused}
+
+    if len(deployment["schedules"]) > 0:
+        new_schedules = []
+        for s in deployment["schedules"]:
+            # Drop server-managed fields before patching
+            s_clean = {
+                k: v
+                for k, v in s.items()
+                if k not in {"id", "created", "updated", "deployment_id"}
+            }
+            s_clean["active"] = not paused
+            new_schedules.append(s_clean)
+        payload["schedules"] = new_schedules
+
+    prefect_patch(f"deployments/{deployment_id}", payload)
 
     return None
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
prefect_patch(
f"deployments/{deployment_id}",
{"schedules": [schedule], "paused": status == "inactive"},
)
def set_deployment_schedule(deployment_id: str, status: str) -> None:
"""Set deployment schedule to active or inactive"""
# both the apis return null below
deployment = prefect_get(f"deployments/{deployment_id}")
allowed = {"active", "inactive"}
if status not in allowed:
raise ValueError(f"status must be one of {allowed}")
paused = status == "inactive"
payload = {"paused": paused}
if len(deployment["schedules"]) > 0:
new_schedules = []
for s in deployment["schedules"]:
# Drop server-managed fields before patching
s_clean = {
k: v
for k, v in s.items()
if k not in {"id", "created", "updated", "deployment_id"}
}
s_clean["active"] = not paused
new_schedules.append(s_clean)
payload["schedules"] = new_schedules
prefect_patch(f"deployments/{deployment_id}", payload)
return None


return None

Expand Down
4 changes: 2 additions & 2 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ def test_set_deployment_schedule_result_1():
set_deployment_schedule(deployment_id, "active")
mock_patch.assert_called_once_with(
f"deployments/{deployment_id}",
{"schedules": [{"schedule": {"cron": "0 0 * * *"}, "active": True}]},
{"schedules": [{"schedule": {"cron": "0 0 * * *"}, "active": True}], "paused": False},
)


Expand All @@ -1542,7 +1542,7 @@ def test_set_deployment_schedule_result_2():
set_deployment_schedule(deployment_id, "inactive")
mock_patch.assert_called_once_with(
f"deployments/{deployment_id}",
{"schedules": [{"schedule": {"cron": "0 0 * * *"}, "active": False}]},
{"schedules": [{"schedule": {"cron": "0 0 * * *"}, "active": False}], "paused": True},
)


Expand Down
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.