Thread
#prefect-community
    Joshua Greenhalgh

    Joshua Greenhalgh

    4 months ago
    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;
    try:
                scheduled_start_time: DateTime = prefect.context.scheduled_start_time
            except AttributeError:
                raise Exception("No start/end time params and no schedule")
    Kevin Kho

    Kevin Kho

    4 months ago
    There is something like
    auto_scheduled
    in the context
    Joshua Greenhalgh

    Joshua Greenhalgh

    4 months ago
    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?
    Kevin Kho

    Kevin Kho

    4 months ago
    Let me check
    Yeah I’ll need to ask other team members if I am misremembering then get back to you
    Joshua Greenhalgh

    Joshua Greenhalgh

    4 months ago
    Its weird but this may be what I want... have a param_default on the clock;
    clock = clocks.IntervalClock(
        timedelta(minutes=5), parameter_defaults={"is_scheduled_param": True}
    )
    set the param to false by default
    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?
    Kevin Kho

    Kevin Kho

    4 months ago
    Yeah that looks like it would work. Clock parameter would take precedent over Parameter default
    Joshua Greenhalgh

    Joshua Greenhalgh

    4 months ago
    It does indeed work
    it is not particularly elegant - the flow must somewhere know if it has been scheduled!?
    Kevin Kho

    Kevin Kho

    4 months ago
    So
    auto_scheduled
    is in the GraphQL backend. Not in context. So you can query for it with something like:
    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"]))