|
| 1 | +# Temporal TypeScript Example |
| 2 | + |
| 3 | +This example demonstrates a complete Temporal workflow setup including: |
| 4 | +- **Workflow Definition**: A multi-step workflow with activities, signals, and queries |
| 5 | +- **Worker**: A worker that executes workflows and activities |
| 6 | +- **Workflow Starter**: A client that starts and interacts with workflows |
| 7 | +- **Replayer**: Functionality to replay workflow history for testing and debugging |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +1. **Temporal Server**: Make sure you have Temporal server running locally |
| 12 | + ```bash |
| 13 | + # Using Temporal CLI (recommended) |
| 14 | + temporal server start-dev |
| 15 | + |
| 16 | + # Or using Docker |
| 17 | + git clone https://github.com/temporalio/docker-compose.git |
| 18 | + cd docker-compose |
| 19 | + docker-compose up |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Node.js**: Version 16 or higher |
| 23 | + |
| 24 | +## Setup |
| 25 | + |
| 26 | +1. Install dependencies: |
| 27 | + ```bash |
| 28 | + npm install |
| 29 | + ``` |
| 30 | + |
| 31 | +2. Build the TypeScript code: |
| 32 | + ```bash |
| 33 | + npm run build |
| 34 | + ``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### 1. Start the Worker |
| 39 | + |
| 40 | +In one terminal, start the worker to listen for workflow tasks: |
| 41 | + |
| 42 | +```bash |
| 43 | +npm run worker |
| 44 | +``` |
| 45 | + |
| 46 | +You should see: |
| 47 | +``` |
| 48 | +Starting worker... |
| 49 | +Worker started. Listening for workflows... |
| 50 | +``` |
| 51 | + |
| 52 | +### 2. Start a Workflow |
| 53 | + |
| 54 | +In another terminal, start a workflow: |
| 55 | + |
| 56 | +```bash |
| 57 | +npm run start |
| 58 | +``` |
| 59 | + |
| 60 | +This will: |
| 61 | +- Start a new workflow with a unique ID |
| 62 | +- Send signals to update the workflow state |
| 63 | +- Query the workflow status |
| 64 | +- Wait for the workflow to complete |
| 65 | +- Display the final result |
| 66 | + |
| 67 | +Expected output: |
| 68 | +``` |
| 69 | +Starting workflow... |
| 70 | +Started workflow with ID: example-workflow-1234567890 |
| 71 | +Sending signal to update name... |
| 72 | +Current status: name updated to Updated Developer |
| 73 | +Sending completion signal... |
| 74 | +Workflow result: Workflow completed successfully! Greeting: Hello, Updated Developer!, Result: 42, Data: Processed: SAMPLE WORKFLOW DATA |
| 75 | +``` |
| 76 | + |
| 77 | +### 3. Replay a Workflow |
| 78 | + |
| 79 | +To replay a completed workflow for testing/debugging: |
| 80 | + |
| 81 | +```bash |
| 82 | +npm run replay <workflow-id> |
| 83 | +``` |
| 84 | + |
| 85 | +For example: |
| 86 | +```bash |
| 87 | +npm run replay example-workflow-1234567890 |
| 88 | +``` |
| 89 | + |
| 90 | +## Workflow Features |
| 91 | + |
| 92 | +### Activities |
| 93 | +- **greetActivity**: Simple greeting with a name |
| 94 | +- **calculateActivity**: Performs a calculation with retry logic |
| 95 | +- **processDataActivity**: Processes and transforms data |
| 96 | + |
| 97 | +### Signals |
| 98 | +- **updateNameSignal**: Updates the name used in the workflow |
| 99 | +- **completeWorkflowSignal**: Triggers workflow completion |
| 100 | + |
| 101 | +### Queries |
| 102 | +- **getCurrentStatusQuery**: Returns the current workflow status |
| 103 | + |
| 104 | +### Workflow Flow |
| 105 | +1. **Greeting**: Calls the greet activity with the initial name |
| 106 | +2. **Calculation**: Performs a mathematical operation |
| 107 | +3. **Data Processing**: Transforms input data |
| 108 | +4. **Wait for Completion**: Waits for a signal or timeout (30 seconds) |
| 109 | +5. **Return Result**: Combines all results into a final message |
| 110 | + |
| 111 | +## Advanced Usage |
| 112 | + |
| 113 | +### Direct TypeScript Execution |
| 114 | + |
| 115 | +You can also run the commands directly with ts-node: |
| 116 | + |
| 117 | +```bash |
| 118 | +# Start worker |
| 119 | +npx ts-node main.ts worker |
| 120 | + |
| 121 | +# Start workflow |
| 122 | +npx ts-node main.ts start |
| 123 | + |
| 124 | +# Replay workflow |
| 125 | +npx ts-node main.ts replay <workflow-id> |
| 126 | +``` |
| 127 | + |
| 128 | +### Customizing the Example |
| 129 | + |
| 130 | +The workflow accepts input parameters that you can modify in the `startWorkflow()` function: |
| 131 | + |
| 132 | +```typescript |
| 133 | +const workflowInput: WorkflowInput = { |
| 134 | + initialName: 'Your Name Here', |
| 135 | + numbers: { a: 5, b: 10 }, |
| 136 | + data: 'your custom data', |
| 137 | +}; |
| 138 | +``` |
| 139 | + |
| 140 | +### Integration with Replayer Adapter |
| 141 | + |
| 142 | +This example is designed to work with the `replayer-adapter-nodejs` package for enhanced replay capabilities. You can integrate it by: |
| 143 | + |
| 144 | +1. Installing the replayer adapter: |
| 145 | + ```bash |
| 146 | + npm install ../../../replayer-adapter-nodejs |
| 147 | + ``` |
| 148 | + |
| 149 | +2. Using the replayer adapter in your workflow code for advanced debugging features. |
| 150 | + |
| 151 | +## Project Structure |
| 152 | + |
| 153 | +``` |
| 154 | +example/js/ |
| 155 | +├── main.ts # Complete workflow example |
| 156 | +├── package.json # Dependencies and scripts |
| 157 | +├── tsconfig.json # TypeScript configuration |
| 158 | +├── README.md # This file |
| 159 | +└── dist/ # Compiled JavaScript (after build) |
| 160 | +``` |
| 161 | + |
| 162 | +## Troubleshooting |
| 163 | + |
| 164 | +1. **Connection Errors**: Make sure Temporal server is running on `localhost:7233` |
| 165 | +2. **Build Errors**: Ensure you have TypeScript installed and run `npm run build` |
| 166 | +3. **Worker Not Receiving Tasks**: Check that both worker and client use the same task queue name |
| 167 | +4. **Signal/Query Errors**: Ensure the workflow is running before sending signals or queries |
| 168 | + |
| 169 | +## Next Steps |
| 170 | + |
| 171 | +- Explore the [Temporal TypeScript SDK documentation](https://docs.temporal.io/typescript) |
| 172 | +- Check out more [Temporal samples](https://github.com/temporalio/samples-typescript) |
| 173 | +- Learn about [Temporal best practices](https://docs.temporal.io/typescript/best-practices) |
0 commit comments