-
Notifications
You must be signed in to change notification settings - Fork 8
⚙️ FEATURE-#201: Separate Engine from Execution Strategy #202
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f675f09
⚙️ FEATURE-#201: Add TaskEngine with context manager lifecycle and co…
FernandoCelmer 5219e1a
⚙️ FEATURE-#201: Refactor Execution as backward-compatible wrapper ar…
FernandoCelmer 72d7cb7
⚙️ FEATURE-#201: Extract checkpoint to Flow base class and adapt stra…
FernandoCelmer 4097c81
❤️ TEST-#201: Add unit tests for TaskEngine lifecycle, retry, timeout…
FernandoCelmer 2e293b7
🪲 BUG-#201: Fix checkpoint example to use valid UUID instead of string
FernandoCelmer f110ebb
📝 PEP8-#201: Fix SIM117 nested with statements in test_engine
FernandoCelmer 6805e5e
📝 PEP8-#201: Fix ruff format with CI config (.code_quality/ruff.toml)
FernandoCelmer 5bb3f03
⚙️ FEATURE-#201: Move retry, timeout and backoff from Action to TaskE…
FernandoCelmer 5e418e2
⚙️ FEATURE-#201: Update strategies and Execution wrapper to use execu…
FernandoCelmer 30b03b4
❤️ TEST-#201: Update tests for retry/timeout in engine and simplify a…
FernandoCelmer 9f92ca1
📘 DOCS-#201: Update docs to reference TaskEngine instead of Execution
FernandoCelmer 18f0d59
📘 DOCS-#201: Add Task Engine concept documentation
FernandoCelmer 9ae25e1
📘 DOCS-#201: Add mermaid diagrams to Task Engine documentation
FernandoCelmer ca82155
📌 ISSUE-#201: Remove unused checkpoint_context and update docs with s…
FernandoCelmer 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Task Engine | ||
|
|
||
| The `TaskEngine` manages the **lifecycle** of a single task — status transitions, duration tracking, retry, timeout, backoff, error handling, and tracer integration. Execution strategies (`Sequential`, `Parallel`, etc.) are responsible only for **ordering and parallelism**. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| participant S as Strategy | ||
| participant E as TaskEngine | ||
| participant A as @action | ||
|
|
||
| S->>E: TaskEngine(task, workflow_id, previous_context) | ||
| S->>E: with engine.start() | ||
| E->>E: status = IN_PROGRESS | ||
| E->>E: tracer.start_task() | ||
| S->>E: engine.execute_with_retry() | ||
|
|
||
| loop retry loop (max_attempts) | ||
| alt timeout > 0 | ||
| E->>E: _execute_with_timeout() | ||
| else no timeout | ||
| E->>E: _execute_single() | ||
| end | ||
| E->>A: task.step() | ||
| alt success | ||
| A-->>E: Context | ||
| E-->>S: result | ||
| else failure & attempts remaining | ||
| E->>E: status = RETRY | ||
| E->>E: sleep(delay) | ||
| end | ||
| end | ||
|
|
||
| E->>E: status = COMPLETED / FAILED | ||
| E->>E: tracer.end_task() | ||
| S->>S: task.callback() | ||
| S->>S: _flow_callback() | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| The engine uses a **context manager** pattern to separate lifecycle from execution: | ||
|
|
||
| ```python | ||
| engine = TaskEngine(task=task, workflow_id=workflow_id, previous_context=previous_context) | ||
|
|
||
| with engine.start(): | ||
| engine.execute_with_retry() | ||
| ``` | ||
|
|
||
| ### `start()` — lifecycle context manager | ||
|
|
||
| Manages everything that happens **around** the execution: | ||
|
|
||
| ```mermaid | ||
| stateDiagram-v2 | ||
| [*] --> IN_PROGRESS: start() | ||
| IN_PROGRESS --> COMPLETED: success | ||
| IN_PROGRESS --> RETRY: retry attempt | ||
| RETRY --> COMPLETED: success after retry | ||
| RETRY --> FAILED: max attempts reached | ||
| IN_PROGRESS --> FAILED: exception | ||
| COMPLETED --> [*]: end_task tracer | ||
| FAILED --> [*]: end_task tracer | ||
| ``` | ||
|
|
||
| 1. Sets `status = IN_PROGRESS` and starts the timer | ||
| 2. Starts the tracer span | ||
| 3. **Yields** — the execution block runs here | ||
| 4. On success: sets `duration` and `status = COMPLETED` | ||
| 5. On error: sets `errors` and `status = FAILED` | ||
| 6. Always: ends the tracer span | ||
|
|
||
| ### `execute_with_retry()` — retry, timeout, and backoff | ||
|
|
||
| Reads `retry`, `timeout`, `retry_delay`, and `backoff` from the `@action` decorator and manages the full retry loop: | ||
|
|
||
| ```mermaid | ||
| flowchart TD | ||
| A["execute_with_retry()"] --> B{"timeout > 0?"} | ||
| B -->|yes| C["_execute_with_timeout()"] | ||
| B -->|no| D["_execute_single()"] | ||
| C --> E{"success?"} | ||
| D --> E | ||
| E -->|yes| F["return result"] | ||
| E -->|no| G{"attempt < max?"} | ||
| G -->|yes| H["status = RETRY\nsleep(delay)"] | ||
| H -->|"backoff?"| I["delay *= 2"] | ||
| I --> B | ||
| H -->|no backoff| B | ||
| G -->|no| J["raise exception"] | ||
| ``` | ||
|
|
||
| - If `timeout > 0`: uses `ThreadPoolExecutor` with a real deadline | ||
| - If execution fails and `attempt < max_attempts`: sets `status = RETRY`, waits, and retries | ||
| - If `backoff = True`: doubles the delay after each failed attempt | ||
|
|
||
| ### `execute()` — single execution | ||
|
|
||
| Calls the task function once without retry. Used internally by `execute_with_retry()` and available for cases where retry is not needed. | ||
|
|
||
| ## References | ||
|
|
||
| - [Task lifecycle and status](concept-task-lifecycle.md) | ||
| - [`@action` decorator](../reference/action.md) | ||
| - [`TypeStatus`](../reference/type-status.md) |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.