Skip to content

Commit

Permalink
Updated the "Basic - Exit handler" sample (#1109)
Browse files Browse the repository at this point in the history
Modernized the sample pipeline code.
  • Loading branch information
Ark-kun authored and k8s-ci-robot committed Apr 12, 2019
1 parent ce80715 commit 2d10492
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions samples/basic/exit_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Copyright 2018 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,36 +14,44 @@
# limitations under the License.


import kfp.dsl as dsl
import kfp
from kfp import dsl


def gcs_download_op(url):
return dsl.ContainerOp(
name='GCS - Download',
image='google/cloud-sdk:216.0.0',
command=['sh', '-c'],
arguments=['gsutil cat $0 | tee $1', url, '/tmp/results.txt'],
file_outputs={
'data': '/tmp/results.txt',
}
)


def echo_op(text):
return dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo "$0"', text]
)


@dsl.pipeline(
name='Exit Handler',
description='Download a message and print it out. Exit Handler will run at the end.'
name='Exit Handler',
description='Downloads a message and prints it. The exit handler will run after the pipeline finishes (successfully or not).'
)
def download_and_print(url='gs://ml-pipeline-playground/shakespeare1.txt'):
"""A sample pipeline showing exit handler."""

exit_op = dsl.ContainerOp(
name='finally',
image='library/bash:4.4.23',
command=['echo', 'exit!'])
"""A sample pipeline showing exit handler."""

with dsl.ExitHandler(exit_op):
exit_task = echo_op('exit!')

op1 = dsl.ContainerOp(
name='download',
image='google/cloud-sdk:216.0.0',
command=['sh', '-c'],
arguments=['gsutil cat %s | tee /tmp/results.txt' % url],
file_outputs={'downloaded': '/tmp/results.txt'})
with dsl.ExitHandler(exit_task):
download_task = gcs_download_op(url)
echo_task = echo_op(download_task.output)

op2 = dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo %s' % op1.output])

if __name__ == '__main__':
import kfp.compiler as compiler
compiler.Compiler().compile(download_and_print, __file__ + '.zip')
kfp.compiler.Compiler().compile(download_and_print, __file__ + '.zip')

0 comments on commit 2d10492

Please sign in to comment.