Updated the "Basic - Exit handler" sample (#1109)
Modernized the sample pipeline code.
This commit is contained in:
parent
ce8071566f
commit
2d1049251b
|
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# Copyright 2018 Google LLC
|
# Copyright 2019 Google LLC
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
|
|
@ -14,36 +14,44 @@
|
||||||
# limitations under the License.
|
# 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(
|
@dsl.pipeline(
|
||||||
name='Exit Handler',
|
name='Exit Handler',
|
||||||
description='Download a message and print it out. Exit Handler will run at the end.'
|
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'):
|
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(
|
exit_task = echo_op('exit!')
|
||||||
name='finally',
|
|
||||||
image='library/bash:4.4.23',
|
|
||||||
command=['echo', '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__':
|
if __name__ == '__main__':
|
||||||
import kfp.compiler as compiler
|
kfp.compiler.Compiler().compile(download_and_print, __file__ + '.zip')
|
||||||
compiler.Compiler().compile(download_and_print, __file__ + '.zip')
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue