bulk_create_example.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. """
  18. This example show you how to create workflows in batch mode.
  19. After this example run, we will create 10 workflows named `workflow:<workflow_num>`, and with 3 tasks
  20. named `task:<task_num>-workflow:<workflow_num>` in each workflow. Task shape as below
  21. task:1-workflow:1 -> task:2-workflow:1 -> task:3-workflow:1
  22. Each workflow is linear since we set `IS_CHAIN=True`, you could change task to parallel by set it to `False`.
  23. """
  24. from pydolphinscheduler.core.process_definition import ProcessDefinition
  25. from pydolphinscheduler.tasks.shell import Shell
  26. NUM_WORKFLOWS = 10
  27. NUM_TASKS = 5
  28. # Make sure your tenant exists in your operator system
  29. TENANT = "exists_tenant"
  30. # Whether task should dependent on pre one or not
  31. # False will create workflow with independent task, while True task will dependent on pre-task and dependence
  32. # link like `pre_task -> current_task -> next_task`, default True
  33. IS_CHAIN = True
  34. for wf in range(0, NUM_WORKFLOWS):
  35. workflow_name = f"workflow:{wf}"
  36. with ProcessDefinition(name=workflow_name, tenant=TENANT) as pd:
  37. for t in range(0, NUM_TASKS):
  38. task_name = f"task:{t}-{workflow_name}"
  39. command = f"echo This is task {task_name}"
  40. task = Shell(name=task_name, command=command)
  41. if IS_CHAIN and t > 0:
  42. pre_task_name = f"task:{t-1}-{workflow_name}"
  43. pd.get_one_task_by_name(pre_task_name) >> task
  44. # We just submit workflow and task definition without set schedule time or run it manually
  45. pd.submit()