diff --git a/samples/basic/exit_handler.py b/samples/basic/exit_handler.py index 85806ffe77..25decd5d9e 100755 --- a/samples/basic/exit_handler.py +++ b/samples/basic/exit_handler.py @@ -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. @@ -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.""" + """A sample pipeline showing exit handler.""" - exit_op = dsl.ContainerOp( - name='finally', - image='library/bash:4.4.23', - command=['echo', 'exit!']) + exit_task = echo_op('exit!') - with dsl.ExitHandler(exit_op): + with dsl.ExitHandler(exit_task): + download_task = gcs_download_op(url) + echo_task = echo_op(download_task.output) - 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'}) - - 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')