Skip to content

Commit 51ba9a8

Browse files
committed
svc2svc - fix deadlock error when making multiple requests, update callback function API
Signed-off-by: Lance Drane <dranelt@ornl.gov>
1 parent 31f0ce1 commit 51ba9a8

File tree

12 files changed

+217
-147
lines changed

12 files changed

+217
-147
lines changed

examples/4_service_to_service/example_1_service.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,27 @@ def pass_text_to_service_2(self, text: str) -> None:
4040
self.intersect_sdk_call_service(msg_to_send, self.service_2_handler)
4141

4242
@intersect_event(events={'response_event': IntersectEventDefinition(event_type=str)})
43-
def service_2_handler(self, msg: str) -> None:
44-
"""Handles response from service 2 and emits the response as an event for the client."""
43+
def service_2_handler(self, _source: str, _operation: str, _has_error: bool, msg: str) -> None:
44+
"""Handles first response from service 2, emits the response as an event for the client, and sends a hardcoded message to service 2."""
4545
self.intersect_sdk_emit_event('response_event', f'Received Response from Service 2: {msg}')
4646

47+
# verify that we can call the service multiple
48+
msg_to_send = IntersectDirectMessageParams(
49+
destination='example-organization.example-facility.example-system.example-subsystem.service-two',
50+
operation='ServiceTwo.test_service',
51+
payload='Final Verification',
52+
)
53+
self.intersect_sdk_call_service(msg_to_send, self.additional_service_handler)
54+
55+
@intersect_event(events={'response_event': IntersectEventDefinition(event_type=str)})
56+
def additional_service_handler(
57+
self, _source: str, _operation: str, _has_error: bool, msg: str
58+
) -> None:
59+
"""Handles second response from service 2 and emits the response as an event for the client."""
60+
self.intersect_sdk_emit_event(
61+
'response_event', f'Received Second Response from Service 2: {msg}'
62+
)
63+
4764

4865
if __name__ == '__main__':
4966
from_config_file = {

examples/4_service_to_service/example_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
class SampleOrchestrator:
2424
"""Simply contains an event callback for events from Service 1."""
2525

26+
def __init__(self) -> None:
27+
"""Straightforward constructor, just initializes global variable which counts events."""
28+
self.got_first_event = False
29+
2630
def event_callback(
2731
self, _source: str, _operation: str, _event_name: str, payload: INTERSECT_JSON_VALUE
2832
) -> None:
@@ -35,8 +39,11 @@ def event_callback(
3539
payload: Value of the response from the Service.
3640
"""
3741
print(payload)
38-
# break out of pubsub loop
39-
raise Exception
42+
if self.got_first_event:
43+
# break out of pubsub loop
44+
raise Exception
45+
self.got_first_event = True
46+
# empty return, don't send any additional messages or modify the events listened to
4047

4148

4249
if __name__ == '__main__':

pdm.lock

Lines changed: 52 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ docs = [
5454
[tool.pdm.dev-dependencies]
5555
lint = [
5656
"pre-commit>=3.3.1",
57-
"ruff>=0.4.2",
57+
"ruff>=0.5.7",
5858
"mypy>=1.10.0",
5959
"types-paho-mqtt>=1.6.0.20240106",
6060
]

src/intersect_sdk/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .config.service import IntersectServiceConfig
2020
from .config.shared import (
2121
ControlPlaneConfig,
22+
ControlProvider,
2223
DataStoreConfig,
2324
DataStoreConfigMap,
2425
HierarchyConfig,
@@ -63,6 +64,7 @@
6364
'IntersectServiceConfig',
6465
'HierarchyConfig',
6566
'ControlPlaneConfig',
67+
'ControlProvider',
6668
'DataStoreConfig',
6769
'DataStoreConfigMap',
6870
'__version__',

src/intersect_sdk/_internal/interfaces.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ def create_external_request(
3636
self,
3737
request: IntersectDirectMessageParams,
3838
response_handler: INTERSECT_SERVICE_RESPONSE_CALLBACK_TYPE | None = None,
39+
timeout: float = 300.0,
3940
) -> UUID:
4041
"""Observed entity (capabilitiy) tells observer (i.e. service) to send an external request.
4142
4243
Params:
4344
- request: the request we want to send out, encapsulated as an IntersectClientMessageParams object
4445
- response_handler: optional callback for how we want to handle the response from this request.
46+
- timeout: optional value for how long we should wait on the request, in seconds (default: 300 seconds)
4547
4648
Returns:
4749
- generated RequestID associated with your request

src/intersect_sdk/capability/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ def intersect_sdk_call_service(
133133
self,
134134
request: IntersectDirectMessageParams,
135135
response_handler: INTERSECT_SERVICE_RESPONSE_CALLBACK_TYPE | None = None,
136+
timeout: float = 300,
136137
) -> list[UUID]:
137138
"""Create an external request that we'll send to a different Service.
138139
139140
Params:
140141
- request: the request we want to send out, encapsulated as an IntersectClientMessageParams object
141142
- response_handler: optional callback for how we want to handle the response from this request.
143+
- timeout: optional value for how long we should wait on the request, in seconds (default: 300 seconds)
142144
143145
Returns:
144146
- list of generated RequestIDs associated with your request. Note that for almost all use cases,
@@ -148,6 +150,6 @@ def intersect_sdk_call_service(
148150
- pydantic.ValidationError - if the request parameter isn't valid
149151
"""
150152
return [
151-
observer.create_external_request(request, response_handler)
153+
observer.create_external_request(request, response_handler, timeout)
152154
for observer in self.__intersect_sdk_observers__
153155
]

0 commit comments

Comments
 (0)