Skip to content

feat: exit hook docs #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,68 @@ Metaflow sets the following metadata for the alert payload so that the consumpti
"run_id": "run-123"
}
}
```

## Custom Exit Hooks

Argo Workflows supports custom lifecycle hooks that can be executed on flow success or failure. You can define a custom function in your flow file and configure this to be executed with the `@exit_hook` flow decorator

### Features and Limitations

The defined function should either take no arguments, or a single `run=` kwarg which will be populated by Metaflow
with a `Run` object if a run exists, otherwise this will be `None`.



### Example hooks flow

The following example flow uses both success and error hooks

```python
from time import sleep
from typing import Optional
from metaflow import step, FlowSpec, Parameter, exit_hook, Run


def success_print():
print("Flow completed successfully. Performing some success related notifications.")


def failure_print(run: Optional[Run]):
print("Flow failed.")

if run is not None:
print("No run was registered.")
return

print("failed run id:", run.id)
print(
"Failed tasks:\n",
"\n".join(
[task.pathspec for step in run for task in step if not task.successful]
),
)


@exit_hook(on_success=[success_print])
@exit_hook(on_error=[failure_print])
class ExitHookFlow(FlowSpec):
should_fail = Parameter(name="should_fail", default=False)

@step
def start(self):
print("Starting 👋")
print("Should fail?", self.should_fail)
if self.should_fail:
sleep(60)
raise Exception("failing as expected")
self.next(self.end)

@step
def end(self):
print("Done! 🏁")


if __name__ == "__main__":
ExitHookFlow()
```
Loading