Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle set schedule active intelligently #37

Merged
merged 2 commits into from
Aug 12, 2020
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
2 changes: 2 additions & 0 deletions changes/pr37.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
enhancement:
- "Set schedule to inactive if neither Flow nor Flow Group have a schedule - [#37](https://github.com/PrefectHQ/server/pull/37)"
9 changes: 7 additions & 2 deletions src/prefect_server/api/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async def create_flow(
config.core_version_cutoff
):
raise ValueError(
"Prefect Serve requires new flows to be built with Prefect "
"Prefect Server requires new flows to be built with Prefect "
f"{config.core_version_cutoff}+, but this flow was built with "
f"Prefect {core_version}."
)
Expand Down Expand Up @@ -234,7 +234,7 @@ async def create_flow(
{"name": {"_eq": version_group_id}},
]
}
).first()
).first({"id", "schedule"})
if flow_group is None:
flow_group_id = await models.FlowGroup(
tenant_id=tenant_id,
Expand All @@ -250,6 +250,11 @@ async def create_flow(

version = (await models.Flow.where(version_where).max({"version"}))["version"] or 0

# if there is no referenceable schedule for this Flow,
# we should set its "schedule" to inactive to avoid confusion
if flow.schedule is None and getattr(flow_group, "schedule", None) is None:
set_schedule_active = False

# precompute task ids to make edges easy to add to database
flow_id = await models.Flow(
tenant_id=tenant_id,
Expand Down
36 changes: 36 additions & 0 deletions tests/api/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ async def test_create_flow(self, project_id, flow):
)
assert await models.Flow.exists(flow_id)

async def test_create_flow_with_no_schedule_sets_schedule_inactive(
self, project_id, flow
):
assert flow.schedule is None

flow_id = await api.flows.create_flow(
project_id=project_id, serialized_flow=flow.serialize()
)

flow = await models.Flow.where(id=flow_id).first({"is_schedule_active"})
assert flow.is_schedule_active is False

async def test_create_flow_with_only_flow_group_schedule_keeps_schedule_active(
self, project_id, flow_group_id
):
success = await api.flow_groups.set_flow_group_schedule(
flow_group_id=flow_group_id,
clocks=[{"type": "CronClock", "cron": "42 0 0 * * *"}],
)
assert success is True

flow_group = await models.FlowGroup.where(id=flow_group_id).first(
{"schedule", "name"}
)
assert flow_group.schedule is not None

flow = prefect.Flow("empty Flow")
flow_id = await api.flows.create_flow(
project_id=project_id,
serialized_flow=flow.serialize(),
version_group_id=flow_group.name,
)

flow = await models.Flow.where(id=flow_id).first({"is_schedule_active"})
assert flow.is_schedule_active is True

async def test_create_old_and_valid_flow(self, project_id, flow):
serialized_flow = flow.serialize()
serialized_flow["environment"]["__version__"] = "0.0.42"
Expand Down