skrawczyk
07/07/2023, 5:52 PMhello_world
with a deployment called 1.0
that is scheduled to run everyday.
I already have some code in place that can automatically create a new version of hello_world
as a deployment named 1.1
that would also run everyday.
I want to retroactively go back and turn off the schedule for hello_world:1.0
so there aren't 2 versions running at the same time.
I see that the Deployment class has a load()
and update()
function, which would take care of turning off the schedule if I knew which deployment name I needed to turn off.
Does anyone know how I can list all deployments a flow has stored and select the most recent or the ones that have active schedules?Read Deployment By Name
won't work because it requires both flow_name
and deployment_name
. I'm only able to get the flow_name, deployment_name
is the tricky part that I'm trying to work around as there's no way to tell what it would be in our use case.
That's why I'm looking for some sort of function that you could provide flow_name
to and it gives back all of the current deployment_name
valuesSet Schedule Inactive
probably won't work either as I assume the required deployment_id
is unique to a flow_name/deployment_name combo, which takes us back to the same issueChristopher Boyd
07/07/2023, 6:38 PMskrawczyk
07/07/2023, 6:40 PMChristopher Boyd
07/07/2023, 6:40 PMcurl '<https://api.prefect.cloud/api/accounts//workspaces//deployments/filter>' \
-H 'authority: api.prefect.cloud' \
-H 'accept: application/json, text/plain, */*' \
-H 'accept-language: en-US,en;q=0.9' \
-H 'authorization: bearer ' \
-H 'content-type: application/json' \
-H 'origin: <https://app.prefect.cloud>' \
-H 'sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'sec-fetch-dest: empty' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-site: same-site' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' \
-H 'x-prefect-ui: true' \
--data-raw '{"flows":{"id":{"any_":["5f35f85c-a5d1-471b-8da3-b7b22dd29978"]}},"sort":"NAME_ASC"}' \
--compressed
skrawczyk
07/07/2023, 6:49 PMChristopher Boyd
07/07/2023, 6:50 PMprefect flow ls
<https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/flows/name/{name}>
skrawczyk
07/07/2023, 6:58 PMNico Neumann
07/07/2023, 7:18 PMskrawczyk
07/07/2023, 7:28 PMFlowFilter
object to prefect.client.read_deployments()
?
tried dicts and strings, both threw errors.
https://docs.prefect.io/2.10.15/api-ref/prefect/client/orchestration/#prefect.client.orchestration.PrefectClient.read_deploymentsChristopher Boyd
07/07/2023, 7:29 PMfrom prefect.client.schemas.filters import DeploymentFilter, DeploymentFilterName, FlowRunFilter, FlowRunFilterState, FlowRunFilterStateType
import asyncio
async def get_client_response() -> asyncio.coroutine:
async with get_client() as client:
response = await client.read_flow_runs(
deployment_filter=DeploymentFilter(name=DeploymentFilterName
(like_="default")),
flow_run_filter=FlowRunFilter(
state=FlowRunFilterState(
type=FlowRunFilterStateType(
any_=["FAILED", "COMPLETED"])),
)
)
print(response)
print(len(response))
asyncio.run(get_client_response())
skrawczyk
07/07/2023, 7:30 PMChristopher Boyd
07/07/2023, 7:30 PMskrawczyk
07/07/2023, 7:31 PMasync def turn_off_old_schedules(flow_name:str):
async with get_client() as client:
# get flow_id using the name
flow_result:flow_api = await client.read_flow_by_name(flow_name)
flow_id = flow_result.id
# get any deployment where the schedule is active
active_flow_deploys = await client.read_deployments(
flow_filter=FlowFilter(id=FlowFilterId(any_=[flow_id])),
deployment_filter=DeploymentFilter(is_schedule_active=DeploymentFilterIsScheduleActive(eq_=True)))
print(f"\n Number of active deploys: {len(active_flow_deploys)}\n")
# checking if there are deploys to turn off
if bool(active_flow_deploys):
print(f"""\nTurning off old active schedules for the "{flow_name}" flow\n\n""")
for deploy in active_flow_deploys:
flow = Deployment(flow_name=flow_name, name=deploy.name)
await flow.load()
flow.is_schedule_active = False
await flow.apply()
Christopher Boyd
07/07/2023, 8:26 PM