What is the lightest-weight way to check in a func...
# ask-community
j
What is the lightest-weight way to check in a function whether or not the caller is part of a flow or task? I have a failing Jenkins test where I think the problem is that
prefect.runtime.flow_run.id is not None
is trying to create a sqlite database in a directory that it's not allowed to write to when trying to access the prefect.runtime object.
w
prefect.runtime
is the way to do it. But since this is a test, you could use the prefect_test_harness() which would avoid trying to hit an actual database: https://docs.prefect.io/3.0/develop/test-workflows
j
Thank you! I'll try that!
No joy. • I was able to get my tests to fail in my dev environment, not just in jenkins, by doing "prefect cloud logout" • I failed immediately in my prefect_test_fixture (implemented as shown in docs above) with
RuntimeError: Timed out while attempting to connect to ephemeral Prefect API server.
before I even got to my test body • There are screenfuls of sqlalchemy exceptions before the final failure message
n
easiest way is to use the
get_run_context
Copy code
In [1]: from prefect import flow

In [2]: from prefect.context import get_run_context

In [3]: @flow
   ...: def f(): print(get_run_context())

In [4]: f()
12:34:23.116 | INFO    | prefect.engine - Created flow run 'attentive-jackdaw' for flow 'f'
12:34:23.119 | INFO    | prefect.engine - View at ...
12:34:23.565 | INFO    | Flow run 'attentive-jackdaw' - Finished in state Completed()

In [5]: f.fn()
---------------------------------------------------------------------------
MissingContextError                       Traceback (most recent call last)
Cell In[5], line 1
----> 1 f.fn()

Cell In[3], line 2, in f()
      1 @flow
----> 2 def f(): print(get_run_context())

File ~/github.com/prefecthq/prefect/src/prefect/context.py:499, in get_run_context()
    496 if flow_run_ctx:
    497     return flow_run_ctx
--> 499 raise MissingContextError(
    500     "No run context available. You are not in a flow or task run context."
    501 )

MissingContextError: No run context available. You are not in a flow or task run context.
j
Yeah, the whole ephemeral database thing is just a mess in my isolated build environment so I'm just carrying around a variable and checking whether to call my_task or my_task.fn all over the place for now so I can make progress on other things. I may swing back to this.
n
the whole ephemeral database thing is just a mess in my isolated build environment so I'm just carrying around a variable and checking whether to call my_task or my_task.fn
im interested to hear more about this if you don't mind explaining!
j
I failed instantly in the
with prefect_test_harness()
fixture as suggested in the docs ( https://docs.prefect.io/3.0/develop/test-workflows) before I even got to the beginning of my test either in the build environment, and that also happened on my local dev machine if I did
prefect cloud logout
before trying to run the tests.
Not a battle I wanted to fight right now
👍 1
n
when/if you have time, I'd be super appreciative if you could share exactly what error you received