Skip to content

Commit

Permalink
Merge pull request #72 from state-alchemists/release/0.3.1
Browse files Browse the repository at this point in the history
Release 0.3.1
  • Loading branch information
goFrendiAsgard authored Dec 23, 2023
2 parents ea214b7 + a004c4c commit 5824458
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
30 changes: 28 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,37 @@ Parallel(set_xcom_cmd, set_xcom_py) >> Parallel(get_xcom_cmd, get_xcom_py) >> te
runner.register(test_xcom)
```

The example shows that `set-xcom-cmd` and `set-xcom-py` set XCom values `one` and `two` respectively.
The example shows that `set-xcom-cmd` and `set-xcom-py` set XCom values `one` and `two`, respectively.

On the other hand, `get-xcom-cmd` and `get-xcom-py` fetch the values and print them.

Furthermore, every Zrb Task has their return values saved as `__xcom['execution-id']['task-name']`.
Furthermore, every Zrb Task has its return values saved as `__xcom['execution-id']['task-name']`. Let's see the following example:

```python
from zrb import runner, Parallel, CmdTask, python_task

hello_cmd = CmdTask(
name='hello-cmd',
cmd='echo hello-cmd',
)

@python_task(
name='hello-py'
)
def hello_py(*args, **kwargs):
return 'hello-py'

hello = CmdTask(
name='hello',
cmd=[
'echo {{task.get_xcom("hello-cmd")}}',
'echo {{task.get_xcom("hello-py")}}',
],
)

Parallel(hello_cmd, hello_py) >> hello
runner.register(hello)
```


## Basic Example
Expand Down
3 changes: 2 additions & 1 deletion src/zrb/task/base_task/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ async def _run_and_check_all(
]
results = await asyncio.gather(*coroutines)
result = results[-1]
self.set_xcom(self.get_name(), f'{result}')
self._print_result(result)
return result
except Exception as e:
Expand Down Expand Up @@ -379,6 +378,8 @@ async def _cached_run(self, *args: Any, **kwargs: Any) -> Any:
self._increase_attempt()
await asyncio.sleep(self._retry_interval)
await self.on_retry()
self.set_xcom(self.get_name(), f'{result}')
self.log_debug(f'XCom: {self.__xcom}')
await self._mark_done()
return result

Expand Down
26 changes: 25 additions & 1 deletion test/task/test_task_xcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from zrb.task.task import Task


def test_task_xcom():
def test_set_xcom():
set_xcom_cmd = CmdTask(
name='set-xcom-cmd',
cmd='echo "hi{{task.set_xcom("one", "ichi")}}"'
Expand Down Expand Up @@ -45,3 +45,27 @@ def get_xcom_py(*args, **kwargs):
assert result[0].output == 'ichi\nni'
assert result[1] == 'ichi\nni'


def test_get_return_value_as_xcom():
hello_cmd = CmdTask(
name='hello-cmd',
cmd='echo hello-cmd',
)

@python_task(
name='hello-py'
)
def hello_py(*args, **kwargs):
return 'hello-py'

hello = CmdTask(
name='hello',
upstreams=[hello_cmd, hello_py],
cmd=[
'echo {{task.get_xcom("hello-cmd")}}',
'echo {{task.get_xcom("hello-py")}}',
],
)
fn = hello.to_function()
result = fn()
assert result.output == 'hello-cmd\nhello-py'

0 comments on commit 5824458

Please sign in to comment.