Skip to content

Commit 759e650

Browse files
committed
Added docstrings
1 parent b5db959 commit 759e650

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

temporalio/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ async def connect(
133133
metadata doesn't already have an "authorization" key.
134134
data_converter: Data converter to use for all data conversions
135135
to/from payloads.
136+
plugins: Set of plugins that are chained together to allow
137+
intercepting and modifying client creation and service connection.
138+
The earlier plugins wrap the later ones.
139+
140+
Any plugins that also implement
141+
:py:class:`temporalio.worker.Plugin` will be used as worker
142+
plugins too so they should not be given when creating a
143+
worker.
136144
interceptors: Set of interceptors that are chained together to allow
137145
intercepting of client calls. The earlier interceptors wrap the
138146
later ones.
@@ -7391,19 +7399,70 @@ async def _decode_user_metadata(
73917399

73927400

73937401
class Plugin:
7402+
"""Base class for client plugins that can intercept and modify client behavior.
7403+
7404+
Plugins allow customization of client creation and service connection processes
7405+
through a chain of responsibility pattern. Each plugin can modify the client
7406+
configuration or intercept service client connections.
7407+
7408+
If the plugin is also a temporalio.worker.Plugin, it will additionally be propagated as a worker plugin.
7409+
You should likley not also provide it to the worker as that will result in the plugin being applied twice.
7410+
"""
7411+
73947412
def name(self) -> str:
7413+
"""Get the name of this plugin. Can be overridden if desired to provide a more appropriate name.
7414+
7415+
Returns:
7416+
The fully qualified name of the plugin class (module.classname).
7417+
"""
73957418
return type(self).__module__ + "." + type(self).__qualname__
73967419

73977420
def init_client_plugin(self, next: Plugin) -> Plugin:
7421+
"""Initialize this plugin in the plugin chain.
7422+
7423+
This method sets up the chain of responsibility pattern by storing a reference
7424+
to the next plugin in the chain. It is called during client creation to build
7425+
the plugin chain.
7426+
7427+
Args:
7428+
next: The next plugin in the chain to delegate to.
7429+
7430+
Returns:
7431+
This plugin instance for method chaining.
7432+
"""
73987433
self.next_client_plugin = next
73997434
return self
74007435

74017436
def on_create_client(self, config: ClientConfig) -> ClientConfig:
7437+
"""Hook called when creating a client to allow modification of configuration.
7438+
7439+
This method is called during client creation and allows plugins to modify
7440+
the client configuration before the client is fully initialized. Plugins
7441+
can add interceptors, modify connection parameters, or change other settings.
7442+
7443+
Args:
7444+
config: The client configuration dictionary to potentially modify.
7445+
7446+
Returns:
7447+
The modified client configuration.
7448+
"""
74027449
return self.next_client_plugin.on_create_client(config)
74037450

74047451
async def connect_service_client(
74057452
self, config: temporalio.service.ConnectConfig
74067453
) -> temporalio.service.ServiceClient:
7454+
"""Hook called when connecting to the Temporal service.
7455+
7456+
This method is called during service client connection and allows plugins
7457+
to intercept or modify the connection process. Plugins can modify connection
7458+
parameters, add authentication, or provide custom connection logic.
7459+
7460+
Args:
7461+
config: The service connection configuration.
7462+
7463+
Returns:
7464+
The connected service client.
7465+
"""
74077466
return await self.next_client_plugin.connect_service_client(config)
74087467

74097468

temporalio/worker/_worker.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,63 @@ def _to_bridge(self) -> temporalio.bridge.worker.PollerBehavior:
9797

9898

9999
class Plugin:
100+
"""Base class for worker plugins that can intercept and modify worker behavior.
101+
102+
Plugins allow customization of worker creation and execution processes
103+
through a chain of responsibility pattern. Each plugin can modify the worker
104+
configuration or intercept worker execution.
105+
"""
106+
100107
def name(self) -> str:
108+
"""Get the qualified name of this plugin. Can be overridden if desired to provide a more appropriate name.
109+
110+
Returns:
111+
The fully qualified name of the plugin class (module.classname).
112+
"""
101113
return type(self).__module__ + "." + type(self).__qualname__
102114

103115
def init_worker_plugin(self, next: Plugin) -> Plugin:
116+
"""Initialize this plugin in the plugin chain.
117+
118+
This method sets up the chain of responsibility pattern by storing a reference
119+
to the next plugin in the chain. It is called during worker creation to build
120+
the plugin chain.
121+
122+
Args:
123+
next: The next plugin in the chain to delegate to.
124+
125+
Returns:
126+
This plugin instance for method chaining.
127+
"""
104128
self.next_worker_plugin = next
105129
return self
106130

107131
def on_create_worker(self, config: WorkerConfig) -> WorkerConfig:
132+
"""Hook called when creating a worker to allow modification of configuration.
133+
134+
This method is called during worker creation and allows plugins to modify
135+
the worker configuration before the worker is fully initialized. Plugins
136+
can modify task queue names, adjust concurrency settings, add interceptors,
137+
or change other worker settings.
138+
139+
Args:
140+
config: The worker configuration dictionary to potentially modify.
141+
142+
Returns:
143+
The modified worker configuration.
144+
"""
108145
return self.next_worker_plugin.on_create_worker(config)
109146

110147
async def run_worker(self, worker: Worker) -> None:
148+
"""Hook called when running a worker to allow interception of execution.
149+
150+
This method is called when the worker is started and allows plugins to
151+
intercept or wrap the worker execution. Plugins can add monitoring,
152+
custom lifecycle management, or other execution-time behavior.
153+
154+
Args:
155+
worker: The worker instance to run.
156+
"""
111157
await self.next_worker_plugin.run_worker(worker)
112158

113159

@@ -224,6 +270,10 @@ def __init__(
224270
workflow_runner: Runner for workflows.
225271
unsandboxed_workflow_runner: Runner for workflows that opt-out of
226272
sandboxing.
273+
plugins: Collection of plugins for this worker. Any plugins already
274+
on the client that also implement :py:class:`temporalio.worker.Plugin` are
275+
prepended to this list and should not be explicitly given here
276+
to avoid running the plugin twice.
227277
interceptors: Collection of interceptors for this worker. Any
228278
interceptors already on the client that also implement
229279
:py:class:`Interceptor` are prepended to this list and should

0 commit comments

Comments
 (0)