Documentation request: How do I handle a Run from Python?
adamryczkowski opened this issue · comments
I know how to start a Hatchet worker. Now I am trying to use it.
I am trying unsuccessfully get the answer for the following problems relating to scheduling workflow runs using Hatchet's Python SDK:
- How do I check if the Workflow is defined on the server in the first place?
- How do I schedule a workflow run (with passing input data)?
- How do I check, that my run (identified by the messageID I presume) is done? (I know that hatchet supports progress updates via its streaming API, but that I will postpone once I figure out this)
- How do I pull the results of the run?
And thank you for doing this very promising project. :-)
I understood, that this streaming syntax is actually the way to do:
from hatchet_sdk import StepRunEventType
event:StepRunEventType
for event in self._hatchet.client.listener.stream(self._messageID):
print(event.payload)
The problem is, that if the event does not seem to be caught if the run has already been finished before the for loop starts executing.
How do I check if the run has already been computed and get its payload? The payload is there, because it is accessible via the Web UI.
One would hope to have the following code do the trick:
from hatchet_sdk import WorkflowRun
workflow_run:WorkflowRun = self._hatchet.client.rest_client.workflow_run_get(self._messageID)
print(workflow_run.status)
Except, that it does not. The workflow_run_get
errors with
Traceback (most recent call last):
File "/snap/pycharm-professional/384/plugins/python/helpers-pro/pydevd_asyncio/pydevd_asyncio_utils.py", line 117, in _exec_async_code
result = func()
^^^^^^
File "<input>", line 1, in <module>
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/hatchet_sdk/clients/rest_client.py", line 58, in workflow_run_get
return self.workflow_api.workflow_run_get(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/pydantic/validate_call_decorator.py", line 58, in wrapper_function
return validate_call_wrapper(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/pydantic/_internal/_validate_call.py", line 81, in __call__
res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/hatchet_sdk/clients/rest/api/workflow_api.py", line 1476, in workflow_run_get
response_data = self.api_client.call_api(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/hatchet_sdk/clients/rest/api_client.py", line 269, in call_api
response_data = self.rest_client.request(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/hatchet_sdk/clients/rest/rest.py", line 243, in request
r = self.pool_manager.request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/urllib3/_request_methods.py", line 136, in request
return self.request_encode_url(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/urllib3/_request_methods.py", line 183, in request_encode_url
return self.urlopen(method, url, **extra_kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/urllib3/poolmanager.py", line 433, in urlopen
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adam/.cache/pypoetry/virtualenvs/stan-runner-1qcHgIo7-py3.12/lib/python3.12/site-packages/urllib3/poolmanager.py", line 295, in connection_from_host
raise LocationValueError("No host specified.")
urllib3.exceptions.LocationValueError: No host specified.
Upon inspection, the url of the underlying REST api call indeed lacks the hostname. It consists purely of the path, such as /api/v1/tenants/707d0855-80ab-4e1f-a156-f1c4546cbf52/workflow-runs/e5e3eadd-e91f-43af-a399-67c54d1aeb31
.
Hey @adamryczkowski, the empty hostname likely means that you haven't set the HATCHET_CLIENT_TOKEN
environment variable. And could you share the version of the Python SDK you're using? I believe we patched the py SDK recently to fix the rest_client
.