task_conditions_example.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. r"""
  18. A example workflow for task condition.
  19. This example will create five task in single workflow, with four shell task and one condition task. Task
  20. condition have one upstream which we declare explicit with syntax `parent >> condition`, and three downstream
  21. automatically set dependence by condition task by passing parameter `condition`. The graph of this workflow
  22. like:
  23. pre_task_1 -> -> success_branch
  24. \ /
  25. pre_task_2 -> -> conditions ->
  26. / \
  27. pre_task_3 -> -> fail_branch
  28. .
  29. """
  30. from pydolphinscheduler.core.process_definition import ProcessDefinition
  31. from pydolphinscheduler.tasks.condition import FAILURE, SUCCESS, And, Conditions
  32. from pydolphinscheduler.tasks.shell import Shell
  33. with ProcessDefinition(name="task_conditions_example", tenant="tenant_exists") as pd:
  34. pre_task_1 = Shell(name="pre_task_1", command="echo pre_task_1")
  35. pre_task_2 = Shell(name="pre_task_2", command="echo pre_task_2")
  36. pre_task_3 = Shell(name="pre_task_3", command="echo pre_task_3")
  37. cond_operator = And(
  38. And(
  39. SUCCESS(pre_task_1, pre_task_2),
  40. FAILURE(pre_task_3),
  41. ),
  42. )
  43. success_branch = Shell(name="success_branch", command="echo success_branch")
  44. fail_branch = Shell(name="fail_branch", command="echo fail_branch")
  45. condition = Conditions(
  46. name="conditions",
  47. condition=cond_operator,
  48. success_task=success_branch,
  49. failed_task=fail_branch,
  50. )
  51. pd.submit()