forked from temporalio/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
849 lines (770 loc) · 29.9 KB
/
Copy pathservice.py
File metadata and controls
849 lines (770 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
"""Underlying gRPC services."""
from __future__ import annotations
import asyncio
import logging
import os
import socket
import warnings
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from datetime import timedelta
from enum import IntEnum
from typing import ClassVar, Generic, Mapping, Optional, Type, TypeVar, Union
import google.protobuf.empty_pb2
import google.protobuf.message
import temporalio.api.common.v1
import temporalio.api.operatorservice.v1
import temporalio.api.testservice.v1
import temporalio.api.workflowservice.v1
import temporalio.bridge.client
import temporalio.bridge.proto.health.v1
import temporalio.exceptions
import temporalio.runtime
__version__ = "1.5.1"
ServiceRequest = TypeVar("ServiceRequest", bound=google.protobuf.message.Message)
ServiceResponse = TypeVar("ServiceResponse", bound=google.protobuf.message.Message)
logger = logging.getLogger(__name__)
# Set to true to log all requests and responses
LOG_PROTOS = False
@dataclass
class TLSConfig:
"""TLS configuration for connecting to Temporal server."""
server_root_ca_cert: Optional[bytes] = None
"""Root CA to validate the server certificate against."""
domain: Optional[str] = None
"""TLS domain."""
client_cert: Optional[bytes] = None
"""Client certificate for mTLS.
This must be combined with :py:attr:`client_private_key`."""
client_private_key: Optional[bytes] = None
"""Client private key for mTLS.
This must be combined with :py:attr:`client_cert`."""
def _to_bridge_config(self) -> temporalio.bridge.client.ClientTlsConfig:
return temporalio.bridge.client.ClientTlsConfig(
server_root_ca_cert=self.server_root_ca_cert,
domain=self.domain,
client_cert=self.client_cert,
client_private_key=self.client_private_key,
)
@dataclass
class RetryConfig:
"""Retry configuration for server calls."""
initial_interval_millis: int = 100
"""Initial backoff interval."""
randomization_factor: float = 0.2
"""Randomization jitter to add."""
multiplier: float = 1.5
"""Backoff multiplier."""
max_interval_millis: int = 5000
"""Maximum backoff interval."""
max_elapsed_time_millis: Optional[int] = 10000
"""Maximum total time."""
max_retries: int = 10
"""Maximum number of retries."""
def _to_bridge_config(self) -> temporalio.bridge.client.ClientRetryConfig:
return temporalio.bridge.client.ClientRetryConfig(
initial_interval_millis=self.initial_interval_millis,
randomization_factor=self.randomization_factor,
multiplier=self.multiplier,
max_interval_millis=self.max_interval_millis,
max_elapsed_time_millis=self.max_elapsed_time_millis,
max_retries=self.max_retries,
)
@dataclass(frozen=True)
class KeepAliveConfig:
"""Keep-alive configuration for client connections."""
interval_millis: int = 30000
"""Interval to send HTTP2 keep alive pings."""
timeout_millis: int = 15000
"""Timeout that the keep alive must be responded to within or the connection
will be closed."""
default: ClassVar[KeepAliveConfig]
"""Default keep alive config."""
def _to_bridge_config(self) -> temporalio.bridge.client.ClientKeepAliveConfig:
return temporalio.bridge.client.ClientKeepAliveConfig(
interval_millis=self.interval_millis,
timeout_millis=self.timeout_millis,
)
KeepAliveConfig.default = KeepAliveConfig()
@dataclass
class ConnectConfig:
"""Config for connecting to the server."""
target_host: str
api_key: Optional[str] = None
tls: Union[bool, TLSConfig] = False
retry_config: Optional[RetryConfig] = None
keep_alive_config: Optional[KeepAliveConfig] = KeepAliveConfig.default
rpc_metadata: Mapping[str, str] = field(default_factory=dict)
identity: str = ""
lazy: bool = False
runtime: Optional[temporalio.runtime.Runtime] = None
def __post_init__(self) -> None:
"""Set extra defaults on unset properties."""
if not self.identity:
self.identity = f"{os.getpid()}@{socket.gethostname()}"
def _to_bridge_config(self) -> temporalio.bridge.client.ClientConfig:
# Need to create the URL from the host:port. We allowed scheme in the
# past so we'll leave it for only one more version with a warning.
# Otherwise we'll prepend the scheme.
target_url: str
tls_config: Optional[temporalio.bridge.client.ClientTlsConfig]
if "://" in self.target_host:
warnings.warn(
"Target host as URL with scheme no longer supported. This will be an error in future versions."
)
target_url = self.target_host
tls_config = (
self.tls._to_bridge_config()
if isinstance(self.tls, TLSConfig)
else None
)
elif isinstance(self.tls, TLSConfig):
target_url = f"https://{self.target_host}"
tls_config = self.tls._to_bridge_config()
elif self.tls:
target_url = f"https://{self.target_host}"
tls_config = TLSConfig()._to_bridge_config()
else:
target_url = f"http://{self.target_host}"
tls_config = None
return temporalio.bridge.client.ClientConfig(
target_url=target_url,
api_key=self.api_key,
tls_config=tls_config,
retry_config=self.retry_config._to_bridge_config()
if self.retry_config
else None,
keep_alive_config=self.keep_alive_config._to_bridge_config()
if self.keep_alive_config
else None,
metadata=self.rpc_metadata,
identity=self.identity,
client_name="temporal-python",
client_version=__version__,
)
class ServiceClient(ABC):
"""Direct client to Temporal services."""
@staticmethod
async def connect(config: ConnectConfig) -> ServiceClient:
"""Connect directly to Temporal services."""
return await _BridgeServiceClient.connect(config)
def __init__(self, config: ConnectConfig) -> None:
"""Initialize the base service client."""
super().__init__()
self.config = config
self.workflow_service = WorkflowService(self)
self.operator_service = OperatorService(self)
self.test_service = TestService(self)
self._check_health_call = self._new_call(
"check",
temporalio.bridge.proto.health.v1.HealthCheckRequest,
temporalio.bridge.proto.health.v1.HealthCheckResponse,
service="health",
)
async def check_health(
self,
*,
service: str = "temporal.api.workflowservice.v1.WorkflowService",
retry: bool = False,
metadata: Mapping[str, str] = {},
timeout: Optional[timedelta] = None,
) -> bool:
"""Check whether the WorkflowService is up.
In addition to accepting which service to check health on, this accepts
some of the same parameters as other RPC calls. See
:py:meth:`ServiceCall.__call__`.
Returns:
True when available, false if the server is running but the service
is unavailable (rare), or raises an error if server/service cannot
be reached.
"""
resp = await self._check_health_call(
temporalio.bridge.proto.health.v1.HealthCheckRequest(service=service),
retry=retry,
metadata=metadata,
timeout=timeout,
)
return (
resp.status
== temporalio.bridge.proto.health.v1.HealthCheckResponse.ServingStatus.SERVING
)
@property
@abstractmethod
def worker_service_client(self) -> _BridgeServiceClient:
"""Underlying service client."""
raise NotImplementedError
@abstractmethod
def update_rpc_metadata(self, metadata: Mapping[str, str]) -> None:
"""Update service client's RPC metadata."""
raise NotImplementedError
@abstractmethod
def update_api_key(self, api_key: Optional[str]) -> None:
"""Update service client's API key."""
raise NotImplementedError
@abstractmethod
async def _rpc_call(
self,
rpc: str,
req: google.protobuf.message.Message,
resp_type: Type[ServiceResponse],
*,
service: str,
retry: bool,
metadata: Mapping[str, str],
timeout: Optional[timedelta],
) -> ServiceResponse:
raise NotImplementedError
def _new_call(
self,
name: str,
req_type: Type[ServiceRequest],
resp_type: Type[ServiceResponse],
*,
service: str = "workflow",
) -> ServiceCall[ServiceRequest, ServiceResponse]:
return ServiceCall(self, name, req_type, resp_type, service)
class WorkflowService:
"""Client to the Temporal server's workflow service."""
def __init__(self, client: ServiceClient) -> None:
"""Initialize the workflow service."""
wsv1 = temporalio.api.workflowservice.v1
self.count_workflow_executions = client._new_call(
"count_workflow_executions",
wsv1.CountWorkflowExecutionsRequest,
wsv1.CountWorkflowExecutionsResponse,
)
self.create_schedule = client._new_call(
"create_schedule",
wsv1.CreateScheduleRequest,
wsv1.CreateScheduleResponse,
)
self.delete_schedule = client._new_call(
"delete_schedule",
wsv1.DeleteScheduleRequest,
wsv1.DeleteScheduleResponse,
)
self.delete_workflow_execution = client._new_call(
"delete_workflow_execution",
wsv1.DeleteWorkflowExecutionRequest,
wsv1.DeleteWorkflowExecutionResponse,
)
self.describe_batch_operation = client._new_call(
"describe_batch_operation",
wsv1.DescribeBatchOperationRequest,
wsv1.DescribeBatchOperationResponse,
)
self.deprecate_namespace = client._new_call(
"deprecate_namespace",
wsv1.DeprecateNamespaceRequest,
wsv1.DeprecateNamespaceResponse,
)
self.describe_namespace = client._new_call(
"describe_namespace",
wsv1.DescribeNamespaceRequest,
wsv1.DescribeNamespaceResponse,
)
self.describe_schedule = client._new_call(
"describe_schedule",
wsv1.DescribeScheduleRequest,
wsv1.DescribeScheduleResponse,
)
self.describe_task_queue = client._new_call(
"describe_task_queue",
wsv1.DescribeTaskQueueRequest,
wsv1.DescribeTaskQueueResponse,
)
self.describe_workflow_execution = client._new_call(
"describe_workflow_execution",
wsv1.DescribeWorkflowExecutionRequest,
wsv1.DescribeWorkflowExecutionResponse,
)
self.get_cluster_info = client._new_call(
"get_cluster_info",
wsv1.GetClusterInfoRequest,
wsv1.GetClusterInfoResponse,
)
self.get_search_attributes = client._new_call(
"get_search_attributes",
wsv1.GetSearchAttributesRequest,
wsv1.GetSearchAttributesResponse,
)
self.get_system_info = client._new_call(
"get_system_info",
wsv1.GetSystemInfoRequest,
wsv1.GetSystemInfoResponse,
)
self.get_worker_build_id_compatibility = client._new_call(
"get_worker_build_id_compatibility",
wsv1.GetWorkerBuildIdCompatibilityRequest,
wsv1.GetWorkerBuildIdCompatibilityResponse,
)
self.get_worker_task_reachability = client._new_call(
"get_worker_task_reachability",
wsv1.GetWorkerTaskReachabilityRequest,
wsv1.GetWorkerTaskReachabilityResponse,
)
self.get_workflow_execution_history = client._new_call(
"get_workflow_execution_history",
wsv1.GetWorkflowExecutionHistoryRequest,
wsv1.GetWorkflowExecutionHistoryResponse,
)
self.get_workflow_execution_history_reverse = client._new_call(
"get_workflow_execution_history_reverse",
wsv1.GetWorkflowExecutionHistoryReverseRequest,
wsv1.GetWorkflowExecutionHistoryReverseResponse,
)
self.list_archived_workflow_executions = client._new_call(
"list_archived_workflow_executions",
wsv1.ListArchivedWorkflowExecutionsRequest,
wsv1.ListArchivedWorkflowExecutionsResponse,
)
self.list_batch_operations = client._new_call(
"list_batch_operations",
wsv1.ListBatchOperationsRequest,
wsv1.ListBatchOperationsResponse,
)
self.list_closed_workflow_executions = client._new_call(
"list_closed_workflow_executions",
wsv1.ListClosedWorkflowExecutionsRequest,
wsv1.ListClosedWorkflowExecutionsResponse,
)
self.list_namespaces = client._new_call(
"list_namespaces",
wsv1.ListNamespacesRequest,
wsv1.ListNamespacesResponse,
)
self.list_open_workflow_executions = client._new_call(
"list_open_workflow_executions",
wsv1.ListOpenWorkflowExecutionsRequest,
wsv1.ListOpenWorkflowExecutionsResponse,
)
self.list_schedule_matching_times = client._new_call(
"list_schedule_matching_times",
wsv1.ListScheduleMatchingTimesRequest,
wsv1.ListScheduleMatchingTimesResponse,
)
self.list_schedules = client._new_call(
"list_schedules",
wsv1.ListSchedulesRequest,
wsv1.ListSchedulesResponse,
)
self.list_task_queue_partitions = client._new_call(
"list_task_queue_partitions",
wsv1.ListTaskQueuePartitionsRequest,
wsv1.ListTaskQueuePartitionsResponse,
)
self.list_workflow_executions = client._new_call(
"list_workflow_executions",
wsv1.ListWorkflowExecutionsRequest,
wsv1.ListWorkflowExecutionsResponse,
)
self.patch_schedule = client._new_call(
"patch_schedule",
wsv1.PatchScheduleRequest,
wsv1.PatchScheduleResponse,
)
self.poll_activity_task_queue = client._new_call(
"poll_activity_task_queue",
wsv1.PollActivityTaskQueueRequest,
wsv1.PollActivityTaskQueueResponse,
)
self.poll_workflow_execution_update = client._new_call(
"poll_workflow_execution_update",
wsv1.PollWorkflowExecutionUpdateRequest,
wsv1.PollWorkflowExecutionUpdateResponse,
)
self.poll_workflow_task_queue = client._new_call(
"poll_workflow_task_queue",
wsv1.PollWorkflowTaskQueueRequest,
wsv1.PollWorkflowTaskQueueResponse,
)
self.query_workflow = client._new_call(
"query_workflow",
wsv1.QueryWorkflowRequest,
wsv1.QueryWorkflowResponse,
)
self.record_activity_task_heartbeat = client._new_call(
"record_activity_task_heartbeat",
wsv1.RecordActivityTaskHeartbeatRequest,
wsv1.RecordActivityTaskHeartbeatResponse,
)
self.record_activity_task_heartbeat_by_id = client._new_call(
"record_activity_task_heartbeat_by_id",
wsv1.RecordActivityTaskHeartbeatByIdRequest,
wsv1.RecordActivityTaskHeartbeatByIdResponse,
)
self.register_namespace = client._new_call(
"register_namespace",
wsv1.RegisterNamespaceRequest,
wsv1.RegisterNamespaceResponse,
)
self.request_cancel_workflow_execution = client._new_call(
"request_cancel_workflow_execution",
wsv1.RequestCancelWorkflowExecutionRequest,
wsv1.RequestCancelWorkflowExecutionResponse,
)
self.reset_sticky_task_queue = client._new_call(
"reset_sticky_task_queue",
wsv1.ResetStickyTaskQueueRequest,
wsv1.ResetStickyTaskQueueResponse,
)
self.reset_workflow_execution = client._new_call(
"reset_workflow_execution",
wsv1.ResetWorkflowExecutionRequest,
wsv1.ResetWorkflowExecutionResponse,
)
self.respond_activity_task_canceled = client._new_call(
"respond_activity_task_canceled",
wsv1.RespondActivityTaskCanceledRequest,
wsv1.RespondActivityTaskCanceledResponse,
)
self.respond_activity_task_canceled_by_id = client._new_call(
"respond_activity_task_canceled_by_id",
wsv1.RespondActivityTaskCanceledByIdRequest,
wsv1.RespondActivityTaskCanceledByIdResponse,
)
self.respond_activity_task_completed = client._new_call(
"respond_activity_task_completed",
wsv1.RespondActivityTaskCompletedRequest,
wsv1.RespondActivityTaskCompletedResponse,
)
self.respond_activity_task_completed_by_id = client._new_call(
"respond_activity_task_completed_by_id",
wsv1.RespondActivityTaskCompletedByIdRequest,
wsv1.RespondActivityTaskCompletedByIdResponse,
)
self.respond_activity_task_failed = client._new_call(
"respond_activity_task_failed",
wsv1.RespondActivityTaskFailedRequest,
wsv1.RespondActivityTaskFailedResponse,
)
self.respond_activity_task_failed_by_id = client._new_call(
"respond_activity_task_failed_by_id",
wsv1.RespondActivityTaskFailedByIdRequest,
wsv1.RespondActivityTaskFailedByIdResponse,
)
self.respond_query_task_completed = client._new_call(
"respond_query_task_completed",
wsv1.RespondQueryTaskCompletedRequest,
wsv1.RespondQueryTaskCompletedResponse,
)
self.respond_workflow_task_completed = client._new_call(
"respond_workflow_task_completed",
wsv1.RespondWorkflowTaskCompletedRequest,
wsv1.RespondWorkflowTaskCompletedResponse,
)
self.respond_workflow_task_failed = client._new_call(
"respond_workflow_task_failed",
wsv1.RespondWorkflowTaskFailedRequest,
wsv1.RespondWorkflowTaskFailedResponse,
)
self.scan_workflow_executions = client._new_call(
"scan_workflow_executions",
wsv1.ScanWorkflowExecutionsRequest,
wsv1.ScanWorkflowExecutionsResponse,
)
self.signal_with_start_workflow_execution = client._new_call(
"signal_with_start_workflow_execution",
wsv1.SignalWithStartWorkflowExecutionRequest,
wsv1.SignalWithStartWorkflowExecutionResponse,
)
self.signal_workflow_execution = client._new_call(
"signal_workflow_execution",
wsv1.SignalWorkflowExecutionRequest,
wsv1.SignalWorkflowExecutionResponse,
)
self.start_batch_operation = client._new_call(
"start_batch_operation",
wsv1.StartBatchOperationRequest,
wsv1.StartBatchOperationResponse,
)
self.start_workflow_execution = client._new_call(
"start_workflow_execution",
wsv1.StartWorkflowExecutionRequest,
wsv1.StartWorkflowExecutionResponse,
)
self.stop_batch_operation = client._new_call(
"stop_batch_operation",
wsv1.StopBatchOperationRequest,
wsv1.StopBatchOperationResponse,
)
self.terminate_workflow_execution = client._new_call(
"terminate_workflow_execution",
wsv1.TerminateWorkflowExecutionRequest,
wsv1.TerminateWorkflowExecutionResponse,
)
self.update_namespace = client._new_call(
"update_namespace",
wsv1.UpdateNamespaceRequest,
wsv1.UpdateNamespaceResponse,
)
self.update_schedule = client._new_call(
"update_schedule",
wsv1.UpdateScheduleRequest,
wsv1.UpdateScheduleResponse,
)
self.update_workflow_execution = client._new_call(
"update_workflow_execution",
wsv1.UpdateWorkflowExecutionRequest,
wsv1.UpdateWorkflowExecutionResponse,
)
self.update_worker_build_id_compatibility = client._new_call(
"update_worker_build_id_compatibility",
wsv1.UpdateWorkerBuildIdCompatibilityRequest,
wsv1.UpdateWorkerBuildIdCompatibilityResponse,
)
class OperatorService:
"""Client to the Temporal server's operator service."""
def __init__(self, client: ServiceClient) -> None:
"""Initialize the operator service."""
osv1 = temporalio.api.operatorservice.v1
self.add_or_update_remote_cluster = client._new_call(
"add_or_update_remote_cluster",
osv1.AddOrUpdateRemoteClusterRequest,
osv1.AddOrUpdateRemoteClusterResponse,
service="operator",
)
self.add_search_attributes = client._new_call(
"add_search_attributes",
osv1.AddSearchAttributesRequest,
osv1.AddSearchAttributesResponse,
service="operator",
)
self.delete_namespace = client._new_call(
"delete_namespace",
osv1.DeleteNamespaceRequest,
osv1.DeleteNamespaceResponse,
service="operator",
)
self.list_clusters = client._new_call(
"list_clusters",
osv1.ListClustersRequest,
osv1.ListClustersResponse,
service="operator",
)
self.list_search_attributes = client._new_call(
"list_search_attributes",
osv1.ListSearchAttributesRequest,
osv1.ListSearchAttributesResponse,
service="operator",
)
self.remove_remote_cluster = client._new_call(
"remove_remote_cluster",
osv1.RemoveRemoteClusterRequest,
osv1.RemoveRemoteClusterResponse,
service="operator",
)
self.remove_search_attributes = client._new_call(
"remove_search_attributes",
osv1.RemoveSearchAttributesRequest,
osv1.RemoveSearchAttributesResponse,
service="operator",
)
class TestService:
"""Client to the Temporal test server's test service."""
def __init__(self, client: ServiceClient) -> None:
"""Initialize the test service."""
tsv1 = temporalio.api.testservice.v1
self.get_current_time = client._new_call(
"get_current_time",
google.protobuf.empty_pb2.Empty,
tsv1.GetCurrentTimeResponse,
service="test",
)
self.lock_time_skipping = client._new_call(
"lock_time_skipping",
tsv1.LockTimeSkippingRequest,
tsv1.LockTimeSkippingResponse,
service="test",
)
self.sleep_until = client._new_call(
"sleep_until",
tsv1.SleepUntilRequest,
tsv1.SleepResponse,
service="test",
)
self.sleep = client._new_call(
"sleep",
tsv1.SleepRequest,
tsv1.SleepResponse,
service="test",
)
self.unlock_time_skipping_with_sleep = client._new_call(
"unlock_time_skipping_with_sleep",
tsv1.SleepRequest,
tsv1.SleepResponse,
service="test",
)
self.unlock_time_skipping = client._new_call(
"unlock_time_skipping",
tsv1.UnlockTimeSkippingRequest,
tsv1.UnlockTimeSkippingResponse,
service="test",
)
class ServiceCall(Generic[ServiceRequest, ServiceResponse]):
"""Callable RPC method for services."""
def __init__(
self,
service_client: ServiceClient,
name: str,
req_type: Type[ServiceRequest],
resp_type: Type[ServiceResponse],
service: str,
) -> None:
"""Initialize the service call."""
self.service_client = service_client
self.name = name
self.req_type = req_type
self.resp_type = resp_type
self.service = service
async def __call__(
self,
req: ServiceRequest,
*,
retry: bool = False,
metadata: Mapping[str, str] = {},
timeout: Optional[timedelta] = None,
) -> ServiceResponse:
"""Invoke underlying client with the given request.
Args:
req: Request for the call.
retry: If true, will use retry config to retry failed calls.
metadata: Headers used on the RPC call. Keys here override
client-level RPC metadata keys.
timeout: Optional RPC deadline to set for the RPC call.
Returns:
RPC response.
Raises:
RPCError: Any RPC error that occurs during the call.
"""
return await self.service_client._rpc_call(
self.name,
req,
self.resp_type,
service=self.service,
retry=retry,
metadata=metadata,
timeout=timeout,
)
class _BridgeServiceClient(ServiceClient):
@staticmethod
async def connect(config: ConnectConfig) -> _BridgeServiceClient:
client = _BridgeServiceClient(config)
# If not lazy, try to connect
if not config.lazy:
await client._connected_client()
return client
def __init__(self, config: ConnectConfig) -> None:
super().__init__(config)
self._bridge_config = config._to_bridge_config()
self._bridge_client: Optional[temporalio.bridge.client.Client] = None
self._bridge_client_connect_lock = asyncio.Lock()
async def _connected_client(self) -> temporalio.bridge.client.Client:
async with self._bridge_client_connect_lock:
if not self._bridge_client:
runtime = self.config.runtime or temporalio.runtime.Runtime.default()
self._bridge_client = await temporalio.bridge.client.Client.connect(
runtime._core_runtime,
self._bridge_config,
)
return self._bridge_client
@property
def worker_service_client(self) -> _BridgeServiceClient:
"""Underlying service client."""
return self
def update_rpc_metadata(self, metadata: Mapping[str, str]) -> None:
"""Update Core client metadata."""
# Mutate the bridge config and then only mutate the running client
# metadata if already connected
self._bridge_config.metadata = metadata
if self._bridge_client:
self._bridge_client.update_metadata(metadata)
def update_api_key(self, api_key: Optional[str]) -> None:
"""Update Core client API key."""
# Mutate the bridge config and then only mutate the running client
# metadata if already connected
self._bridge_config.api_key = api_key
if self._bridge_client:
self._bridge_client.update_api_key(api_key)
async def _rpc_call(
self,
rpc: str,
req: google.protobuf.message.Message,
resp_type: Type[ServiceResponse],
*,
service: str,
retry: bool,
metadata: Mapping[str, str],
timeout: Optional[timedelta],
) -> ServiceResponse:
global LOG_PROTOS
if LOG_PROTOS:
logger.debug("Service %s request to %s: %s", service, rpc, req)
try:
client = await self._connected_client()
resp = await client.call(
service=service,
rpc=rpc,
req=req,
resp_type=resp_type,
retry=retry,
metadata=metadata,
timeout=timeout,
)
if LOG_PROTOS:
logger.debug("Service %s response from %s: %s", service, rpc, resp)
return resp
except temporalio.bridge.client.RPCError as err:
# Intentionally swallowing the cause instead of using "from"
status, message, details = err.args
raise RPCError(message, RPCStatusCode(status), details)
class RPCStatusCode(IntEnum):
"""Status code for :py:class:`RPCError`."""
OK = 0
CANCELLED = 1
UNKNOWN = 2
INVALID_ARGUMENT = 3
DEADLINE_EXCEEDED = 4
NOT_FOUND = 5
ALREADY_EXISTS = 6
PERMISSION_DENIED = 7
RESOURCE_EXHAUSTED = 8
FAILED_PRECONDITION = 9
ABORTED = 10
OUT_OF_RANGE = 11
UNIMPLEMENTED = 12
INTERNAL = 13
UNAVAILABLE = 14
DATA_LOSS = 15
UNAUTHENTICATED = 16
class RPCError(temporalio.exceptions.TemporalError):
"""Error during RPC call."""
def __init__(
self, message: str, status: RPCStatusCode, raw_grpc_status: bytes
) -> None:
"""Initialize RPC error."""
super().__init__(message)
self._message = message
self._status = status
self._raw_grpc_status = raw_grpc_status
self._grpc_status: Optional[temporalio.api.common.v1.GrpcStatus] = None
@property
def message(self) -> str:
"""Message for the error."""
return self._message
@property
def status(self) -> RPCStatusCode:
"""Status code for the error."""
return self._status
@property
def raw_grpc_status(self) -> bytes:
"""Raw gRPC status bytes."""
return self._raw_grpc_status
@property
def grpc_status(self) -> temporalio.api.common.v1.GrpcStatus:
"""Status of the gRPC call with details."""
if self._grpc_status is None:
status = temporalio.api.common.v1.GrpcStatus()
status.ParseFromString(self._raw_grpc_status)
self._grpc_status = status
return self._grpc_status