|
| 1 | +:_mod-docs-content-type: CONCEPT |
| 2 | + |
| 3 | +[id="ref-best-practices-for-creating-workflows.adoc_{context}"] |
| 4 | += Best practices when creating serverless workflows |
| 5 | + |
| 6 | +Create effective serverless workflows using thoughtful approaches to design, handle data, and manage error by following these best practices based on the Serverless Workflow Domain Specific Language (DSL) principles. These principles help you to build robust workflows. |
| 7 | + |
| 8 | +Workflow design principles:: |
| 9 | ++ |
| 10 | +The Serverless Workflow DSL prioritizes clarity and ease of use when writing workflows. |
| 11 | + |
| 12 | +Priority of constituencies::: |
| 13 | ++ |
| 14 | +When developing workflows or APIs, ensure the needs of the author (workflow writer) come first. The constituencies are prioritized in the following order: Authors > Operators > Implementors > Specifications writers. |
| 15 | + |
| 16 | +Linguistic fluency and clarity::: |
| 17 | ++ |
| 18 | +** Use imperative verbs such as `Call`, `Emit`, `For`, `Fork`, `Raise`, `Run`, `Set`, `Switch`, and `Wait`. These simple, universally understood terms make your workflow simple to read and understand. |
| 19 | + |
| 20 | +Structure and extensibility::: |
| 21 | ++ |
| 22 | +** Use implicit default behaviors to reduce redundancy. |
| 23 | +** Declare components inline if they are not reusable to keep the definition self-contained. |
| 24 | +** Use external references to import and reuse shared components, which promotes a modular design. |
| 25 | +** Prioritize flexibility over strict enumerations to ensure extensibility and adaptability across different runtime environments. |
| 26 | + |
| 27 | +Data flow and runtime management:: |
| 28 | ++ |
| 29 | +Controlling data flow is critical for efficient workflows. Tasks are the fundamental computing units of a workflow. The Domain Specific Language (DSL) defines several default task types that runtimes must do. These include `Do`, `Listen`, `Raise`, `Run`, `Try`, and `Wait`. |
| 30 | + |
| 31 | +Security and error handling:: |
| 32 | ++ |
| 33 | +Secrets::: Use Secrets with caution. Avoid passing them directly in call inputs as this might expose sensitive information. |
| 34 | + |
| 35 | +Fault tolerance and error handling::: Serverless Workflow is designed with resilience in mind to recover from failures. |
| 36 | + |
| 37 | +Orchestrator UI integration best practices:: |
| 38 | ++ |
| 39 | +For your workflow results to be effectively displayed in the Orchestrator UI and to facilitate chaining of workflows, you must structure the output data according to the `WorkflowResult` schema. Additionally, include any error information as part of the workflow output so the UI and subsequent workflows can handle them accordingly. |
| 40 | ++ |
| 41 | +Workflow output schema::: |
| 42 | ++ |
| 43 | +Results placement:::: The primary output intended for subsequent processing must be placed under the `data.result` property. |
| 44 | + |
| 45 | +Schema reference:::: Your output schema file (`schemas/workflow-output-schema.json`) must reference the `WorkflowResult` schema. |
| 46 | + |
| 47 | +Outputs definition:::: Include an `outputs` section in your workflow definition. This section contains human-readable key/value pairs that the UI will display. |
| 48 | ++ |
| 49 | +Structure of workflow: |
| 50 | ++ |
| 51 | +[source,yaml] |
| 52 | +---- |
| 53 | +id: my-workflow |
| 54 | +version: "0.8" |
| 55 | +specVersion: "0.8" |
| 56 | +name: My Workflow |
| 57 | +start: ImmediatelyEnd |
| 58 | +dataInputSchema: schemas/basic__main-schema.json |
| 59 | +extensions: |
| 60 | + - extensionid: workflow-output-schema |
| 61 | + outputSchema: schemas/workflow-output-schema.json |
| 62 | +functions: |
| 63 | + - name: print |
| 64 | + type: custom |
| 65 | + operation: sysout |
| 66 | + - name: successResult |
| 67 | + type: expression |
| 68 | + operation: '{ |
| 69 | + "result": { |
| 70 | + "message": "Project " + .projectName + " active", |
| 71 | + "outputs":[] |
| 72 | + } |
| 73 | + }' |
| 74 | +start: "successResult" |
| 75 | +states: |
| 76 | + - name: successResult |
| 77 | + type: operation |
| 78 | + actions: |
| 79 | + - name: setOutput |
| 80 | + functionRef: |
| 81 | + refName: successResult |
| 82 | + end: true |
| 83 | +---- |
0 commit comments