-
Notifications
You must be signed in to change notification settings - Fork 3
Integrate decision handler into worker #31
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 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
324de45
integrate into worker
timl3136 aa34615
Merge branch 'main' into handler-integrate
timl3136 428f0ac
Add workflow engine integration and fix test compatibility
timl3136 f9989a9
lint
timl3136 073dc34
minor fix
timl3136 3b385cb
respond to comments
timl3136 0e03c79
Merge branch 'main' into handler-integrate
timl3136 4dbed67
Merge remote-tracking branch 'origin/main' into handler-integrate
timl3136 a8432b6
integrate with event iterator
timl3136 c6f1165
minor rename
timl3136 17a7316
fix test
timl3136 01fa7a3
respond to comment 1
timl3136 c6b1b93
respond to comment 2
timl3136 de55832
fix comment
timl3136 1ac57c5
fix test
timl3136 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
File renamed without changes.
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,4 +1,6 @@ | ||
import logging | ||
import threading | ||
from typing import Dict, Tuple | ||
|
||
from cadence.api.v1.common_pb2 import Payload | ||
from cadence.api.v1.service_worker_pb2 import ( | ||
|
@@ -19,7 +21,8 @@ class DecisionTaskHandler(BaseTaskHandler[PollForDecisionTaskResponse]): | |
""" | ||
Task handler for processing decision tasks. | ||
|
||
This handler processes decision tasks and generates decisions using the workflow engine. | ||
This handler processes decision tasks and generates decisions using workflow engines. | ||
Uses a thread-safe cache to hold workflow engines for concurrent decision task handling. | ||
""" | ||
|
||
def __init__(self, client: Client, task_list: str, registry: Registry, identity: str = "unknown", **options): | ||
|
@@ -35,7 +38,9 @@ def __init__(self, client: Client, task_list: str, registry: Registry, identity: | |
""" | ||
super().__init__(client, task_list, identity, **options) | ||
self._registry = registry | ||
self._workflow_engine: WorkflowEngine | ||
# Thread-safe cache to hold workflow engines keyed by (workflow_id, run_id) | ||
self._workflow_engines: Dict[Tuple[str, str], WorkflowEngine] = {} | ||
self._cache_lock = threading.RLock() | ||
|
||
|
||
async def _handle_task_implementation(self, task: PollForDecisionTaskResponse) -> None: | ||
|
@@ -84,21 +89,41 @@ async def _handle_task_implementation(self, task: PollForDecisionTaskResponse) - | |
) | ||
raise KeyError(f"Workflow type '{workflow_type_name}' not found") | ||
|
||
# Create workflow info and engine | ||
# Create workflow info and get or create workflow engine from cache | ||
workflow_info = WorkflowInfo( | ||
workflow_type=workflow_type_name, | ||
workflow_domain=self._client.domain, | ||
workflow_id=workflow_id, | ||
workflow_run_id=run_id | ||
) | ||
|
||
self._workflow_engine = WorkflowEngine( | ||
info=workflow_info, | ||
client=self._client, | ||
workflow_func=workflow_func | ||
) | ||
# Use thread-safe cache to get or create workflow engine | ||
cache_key = (workflow_id, run_id) | ||
with self._cache_lock: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. current cache is unbounded. We need a LFU cache here. Maybe add a TODO for next PR |
||
workflow_engine = self._workflow_engines.get(cache_key) | ||
if workflow_engine is None: | ||
workflow_engine = WorkflowEngine( | ||
info=workflow_info, | ||
client=self._client, | ||
workflow_func=workflow_func | ||
) | ||
self._workflow_engines[cache_key] = workflow_engine | ||
|
||
decision_result = await self._workflow_engine.process_decision(task) | ||
decision_result = await workflow_engine.process_decision(task) | ||
|
||
# Clean up completed workflows from cache to prevent memory leaks | ||
# Use getattr with default False to handle mocked engines in tests | ||
if getattr(workflow_engine, '_is_workflow_complete', False): | ||
timl3136 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
with self._cache_lock: | ||
self._workflow_engines.pop(cache_key, None) | ||
logger.debug( | ||
"Removed completed workflow from cache", | ||
extra={ | ||
"workflow_id": workflow_id, | ||
"run_id": run_id, | ||
"cache_size": len(self._workflow_engines) | ||
} | ||
) | ||
|
||
# Respond with the decisions | ||
await self._respond_decision_task_completed(task, decision_result) | ||
|
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
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.