-
Notifications
You must be signed in to change notification settings - Fork 1
Update README and dependencies for hello world #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StephenBarlow
wants to merge
3
commits into
main
Choose a base branch
from
simplify-hello-world
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−113
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,114 @@ | ||
| # Hello World - Getting Started with Render Workflows | ||
| # Hello World - Render Workflows (TypeScript) | ||
|
|
||
| The simplest possible workflow example to help you understand the basics of Render Workflows. | ||
| This hello-world example demonstrates three foundational workflow patterns: | ||
|
|
||
| ## What you'll learn | ||
| - A minimal task definition (`calculateSquare`) | ||
| - A task that chains runs of another task (`sumSquares`) | ||
| - A task with custom retry behavior (`flipCoin`) | ||
|
|
||
| - **What is a task?** A function that can be executed as a workflow | ||
| - **What is a subtask?** A task called by another task using `await` | ||
| - **How to orchestrate:** Combining multiple tasks to create workflows | ||
| ## What You'll Learn | ||
|
|
||
| ## Workflow structure | ||
| - How to define tasks with `task(...)` | ||
| - How to chain task runs using `await` and `Promise.all` | ||
| - How to customize retry behavior with `retry` | ||
|
|
||
| ## Example Tasks | ||
|
|
||
| ### `calculateSquare(a: number): number` | ||
|
|
||
| The smallest possible task: takes one number and returns its square. | ||
|
|
||
| ### `sumSquares(a: number, b: number): Promise<number>` | ||
|
|
||
| Chains two runs of `calculateSquare` and sums the results. | ||
|
|
||
| It uses `Promise.all(...)` to chain the two runs in parallel: | ||
|
|
||
| ```ts | ||
| const [result1, result2] = await Promise.all([ | ||
| calculateSquare(a), | ||
| calculateSquare(b), | ||
| ]); | ||
| ``` | ||
| calculateAndProcess (multi-step orchestrator) | ||
| ├── addDoubledNumbers | ||
| │ ├── double (subtask #1) | ||
| │ └── double (subtask #2) | ||
| └── processNumbers | ||
| ├── double (subtask for item 1) | ||
| ├── double (subtask for item 2) | ||
| └── double (subtask for item N) | ||
| ``` | ||
|
|
||
| ## Run locally | ||
| ### `flipCoin(): string` | ||
|
|
||
| Simulates a coin flip: | ||
|
|
||
| - Heads: Returns success | ||
| - Tails: Raises an error to trigger retry | ||
|
|
||
| Retry policy in this example: | ||
|
|
||
| - max retries: `3` | ||
| - wait duration: `1000ms` | ||
| - backoff scaling: `1.5` | ||
|
|
||
| ## Local Development | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - Node.js 18+ | ||
|
|
||
| ### Run locally | ||
|
|
||
| ```bash | ||
| cd hello-world | ||
| npm install | ||
| npm run build | ||
| npm start | ||
| ``` | ||
|
|
||
| ## Deploy to Render | ||
| To run tasks locally from another terminal: | ||
|
|
||
| ```bash | ||
| render workflows tasks start calculateSquare --local --input='[5]' | ||
| render workflows tasks start sumSquares --local --input='[3,4]' | ||
| render workflows tasks start flipCoin --local --input='[]' | ||
| ``` | ||
|
|
||
| Expected behavior: | ||
|
|
||
| - `calculateSquare` with `5` returns `25` | ||
| - `sumSquares` with `3,4` returns `25` | ||
| - `flipCoin` may fail and retry before succeeding | ||
|
|
||
| ## Deploying to Render | ||
|
|
||
| Configure your Workflow service with: | ||
|
|
||
| | Option | Value | | ||
| | --- | --- | | ||
| | Build command | `npm install` | | ||
| | Start command | `npm start` | | ||
|
|
||
| ## Key Concepts | ||
|
|
||
| ### Task registration | ||
|
|
||
| Any call to `task({ name: ... }, handler)` registers a runnable workflow task. | ||
|
|
||
| ### Chaining runs | ||
|
|
||
| Inside an async task, calling `await anotherTask(...)` chains a run of that task. | ||
|
|
||
| ### Retries | ||
|
|
||
| Use the `retry` option in task config when transient failures should be retried automatically. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### "Task not found" | ||
|
|
||
| - Confirm the service is running | ||
| - Verify task names exactly match: `calculateSquare`, `sumSquares`, `flipCoin` | ||
|
|
||
| ### Import or dependency issues | ||
|
|
||
| - Confirm dependency install completed from `package.json` | ||
| - Confirm Node.js version is 18+ | ||
|
|
||
| Create a new **Workflow** service on Render: | ||
| ## Resources | ||
|
|
||
| - **Build command:** `npm install && npm run build` | ||
| - **Start command:** `npm start` | ||
| - [Render Workflows documentation](https://render.com/docs/workflows) | ||
| - [Workflows tutorial](https://render.com/docs/workflows-tutorial) | ||
| - [Local development guide](https://render.com/docs/workflows-local-development) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,15 @@ | ||
| { | ||
| "name": "hello-world-workflow", | ||
| "version": "1.0.0", | ||
| "description": "Hello World - Getting Started with Render Workflows (TypeScript)", | ||
| "description": "Hello World - Render Workflows (TypeScript)", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "start": "node dist/main.js", | ||
| "dev": "tsx src/main.ts" | ||
| "start": "tsx src/main.ts" | ||
| }, | ||
| "dependencies": { | ||
| "@renderinc/sdk": "latest", | ||
| "dotenv": "^16.4.7" | ||
| "@renderinc/sdk": "^0.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "tsx": "^4.19.0", | ||
| "typescript": "^5.7.0" | ||
| "tsx": "^4.20.2" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,36 @@ | ||
| import "dotenv/config"; | ||
| import { task } from "@renderinc/sdk/workflows"; | ||
|
|
||
| // Subtask: doubles a number | ||
| const double = task({ name: "double" }, function double(x: number): number { | ||
| console.log(`[TASK] Doubling ${x}`); | ||
| const result = x * 2; | ||
| console.log(`[TASK] Result: ${result}`); | ||
| return result; | ||
| }); | ||
|
|
||
| // Subtask (also callable as root): doubles two numbers and sums them | ||
| const addDoubledNumbers = task( | ||
| { name: "addDoubledNumbers" }, | ||
| async function addDoubledNumbers(a: number, b: number) { | ||
| console.log(`[WORKFLOW] Starting: addDoubledNumbers(${a}, ${b})`); | ||
|
|
||
| const doubledA = await double(a); | ||
| const doubledB = await double(b); | ||
| const total = doubledA + doubledB; | ||
|
|
||
| const result = { | ||
| original_numbers: [a, b], | ||
| doubled_numbers: [doubledA, doubledB], | ||
| sum_of_doubled: total, | ||
| explanation: `${a} doubled is ${doubledA}, ${b} doubled is ${doubledB}, sum is ${total}`, | ||
| }; | ||
|
|
||
| console.log("[WORKFLOW] Complete:", result); | ||
| return result; | ||
| const calculateSquare = task( | ||
| { name: "calculateSquare" }, | ||
| function calculateSquare(a: number): number { | ||
| return a * a; | ||
| }, | ||
| ); | ||
|
|
||
| // Subtask (also callable as root): doubles each number in a list | ||
| const processNumbers = task( | ||
| { name: "processNumbers" }, | ||
| async function processNumbers(...numbers: number[]) { | ||
| console.log(`[WORKFLOW] Starting: processNumbers(${numbers})`); | ||
|
|
||
| const doubledResults: number[] = []; | ||
|
|
||
| for (let i = 0; i < numbers.length; i++) { | ||
| console.log( | ||
| `[WORKFLOW] Processing item ${i + 1}/${numbers.length}: ${numbers[i]}`, | ||
| ); | ||
| const doubled = await double(numbers[i]); | ||
| doubledResults.push(doubled); | ||
| } | ||
|
|
||
| const result = { | ||
| original_numbers: numbers, | ||
| doubled_numbers: doubledResults, | ||
| count: numbers.length, | ||
| explanation: `Processed ${numbers.length} numbers through the double subtask`, | ||
| }; | ||
|
|
||
| console.log("[WORKFLOW] Complete:", result); | ||
| return result; | ||
| const sumSquares = task( | ||
| { name: "sumSquares" }, | ||
| async function sumSquares(a: number, b: number): Promise<number> { | ||
| const [result1, result2] = await Promise.all([ | ||
| calculateSquare(a), | ||
| calculateSquare(b), | ||
| ]); | ||
| return result1 + result2; | ||
| }, | ||
| ); | ||
|
|
||
| // Root task: chains addDoubledNumbers and processNumbers | ||
| task( | ||
| { name: "calculateAndProcess" }, | ||
| async function calculateAndProcess( | ||
| a: number, | ||
| b: number, | ||
| ...moreNumbers: number[] | ||
| ) { | ||
| console.log("[WORKFLOW] Starting multi-step workflow"); | ||
|
|
||
| console.log("[WORKFLOW] Step 1: Adding doubled numbers"); | ||
| const step1Result = await addDoubledNumbers(a, b); | ||
|
|
||
| console.log("[WORKFLOW] Step 2: Processing number list"); | ||
| const step2Result = await processNumbers(...moreNumbers); | ||
|
|
||
| console.log("[WORKFLOW] Step 3: Combining results"); | ||
| const finalResult = { | ||
| step1_sum: step1Result.sum_of_doubled, | ||
| step2_doubled: step2Result.doubled_numbers, | ||
| total_operations: 2 + moreNumbers.length, | ||
| summary: `Added doubled ${a} and ${b}, then doubled ${moreNumbers.length} more numbers`, | ||
| }; | ||
|
|
||
| console.log("[WORKFLOW] Multi-step workflow complete"); | ||
| return finalResult; | ||
| { | ||
| name: "flipCoin", | ||
| retry: { | ||
| maxRetries: 3, | ||
| waitDurationMs: 1000, | ||
| backoffScaling: 1.5, | ||
| }, | ||
| }, | ||
| function flipCoin(): string { | ||
| if (Math.random() < 0.5) { | ||
| throw new Error("Flipped tails! Retrying."); | ||
| } | ||
| return "Flipped heads!"; | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the comment on the python example: we should show
render workflows dev -- npm starthere.