https://prefect.io logo
#prefect-community
Title
# prefect-community
j

Joshua Greenhalgh

05/23/2022, 2:36 PM
Hi is there a way to tell if my flow is running because it was triggered on a schedule as opposed to not? I tried this check;
Copy code
try:
            scheduled_start_time: DateTime = prefect.context.scheduled_start_time
        except AttributeError:
            raise Exception("No start/end time params and no schedule")
k

Kevin Kho

05/23/2022, 2:37 PM
There is something like
auto_scheduled
in the context
j

Joshua Greenhalgh

05/23/2022, 2:39 PM
Ahh ok will take a look - docs here -> https://docs.prefect.io/api/latest/utilities/context.html#context-2 dont mention it
Only keys that mention "schedule" are
'run_on_schedule': True
(this is from a run kicked off from the UI) and
'scheduled_start_time': DateTime(2022, 5, 23, 15, 4, 13, 497479, tzinfo=Timezone('+00:00'))
which is when I kicked it off?
k

Kevin Kho

05/23/2022, 3:15 PM
Let me check
🙏 1
Yeah I’ll need to ask other team members if I am misremembering then get back to you
j

Joshua Greenhalgh

05/23/2022, 3:36 PM
Its weird but this may be what I want... have a param_default on the clock;
Copy code
clock = clocks.IntervalClock(
    timedelta(minutes=5), parameter_defaults={"is_scheduled_param": True}
)
set the param to false by default
Copy code
is_scheduled_param = Parameter(name="is_scheduled_param", default=False)
    start_time_param = Parameter(name="start_time_param", default=None)
    end_time_param = Parameter(name="end_time_param", default=None)

    start_time, end_time = get_ingestion_period(
        start_time_param, end_time_param, is_scheduled_param
    )
pass that into a function that needs to know if its running on a schedule...messy - should really be a flag but cannot find it 😞
Although haven't tried it yet - maybe having two defaults one takes precedence and its not the clocks one?
k

Kevin Kho

05/23/2022, 3:42 PM
Yeah that looks like it would work. Clock parameter would take precedent over Parameter default
j

Joshua Greenhalgh

05/23/2022, 4:13 PM
It does indeed work
it is not particularly elegant - the flow must somewhere know if it has been scheduled!?
k

Kevin Kho

05/23/2022, 8:28 PM
So
auto_scheduled
is in the GraphQL backend. Not in context. So you can query for it with something like:
Copy code
query = """ 
query($id: uuid!) {
  flow_run(where: {id: {_eq: $id} }) {
    id
    auto_scheduled
  }
}
"""
client.graphql(query=query, variables=dict(id=prefect.context["flow_run_id"]))
👍 1
3 Views