1- """Core run_worker implementation for Lambda."""
2-
31from __future__ import annotations
42
53import asyncio
@@ -67,23 +65,21 @@ def _default_extract_lambda_ctx(
6765def run_worker (
6866 version : WorkerDeploymentVersion ,
6967 configure : Callable [[LambdaWorkerConfig ], None ],
70- ) -> Callable [[Any , Any ], Awaitable [ None ] ]:
68+ ) -> Callable [[Any , Any ], None ]:
7169 """Create a Temporal worker Lambda handler.
7270
73- Calls the *configure* callback to collect workflow/activity registrations
74- and option overrides, then returns an async Lambda handler function. On
75- each invocation the handler connects to the Temporal server, starts a
76- worker with Lambda-tuned defaults, polls for tasks until the invocation
71+ Calls the *configure* callback to collect workflow/activity registrations and option overrides,
72+ then returns a Lambda handler function. On each invocation the handler connects to the Temporal
73+ server, starts a worker with Lambda-tuned defaults, polls for tasks until the invocation
7774 deadline approaches, and then gracefully shuts down.
7875
79- The *version* parameter identifies this worker's deployment version.
80- ``run_worker`` always enables Worker Deployment Versioning
81- (``use_worker_versioning=True``). To provide a default versioning behavior
82- for workflows that do not specify one at registration time, set
76+ The *version* parameter identifies this worker's deployment version. ``run_worker`` always
77+ enables Worker Deployment Versioning (``use_worker_versioning=True``). To provide a default
78+ versioning behavior for workflows that do not specify one at registration time, set
8379 ``deployment_config`` in ``worker_config`` in the configure callback.
8480
85- The returned handler has the signature ``async handler(event, context)``
86- and should be set as your Lambda function's handler entry point.
81+ The returned handler has the signature ``handler(event, context)`` and should be set as your
82+ Lambda function's handler entry point.
8783
8884 Args:
8985 version: The worker deployment version. Required.
@@ -92,7 +88,7 @@ def run_worker(
9288 activities, and options on it.
9389
9490 Returns:
95- An async Lambda handler function.
91+ A Lambda handler function.
9692
9793 Example::
9894
@@ -127,7 +123,7 @@ def _run_worker_internal(
127123 version : WorkerDeploymentVersion ,
128124 configure : Callable [[LambdaWorkerConfig ], None ],
129125 deps : _WorkerDeps ,
130- ) -> Callable [[Any , Any ], Awaitable [ None ] ]:
126+ ) -> Callable [[Any , Any ], None ]:
131127 """Core logic with injected dependencies for testability."""
132128 if not version .deployment_name or not version .build_id :
133129 raise ValueError (
@@ -180,12 +176,14 @@ def _run_worker_internal(
180176
181177 extract_lambda_ctx = deps .extract_lambda_ctx or _default_extract_lambda_ctx
182178
183- async def _handler (_event : Any , lambda_context : Any ) -> None :
184- await _invocation_handler (
185- lambda_context = lambda_context ,
186- config = config ,
187- deps = deps ,
188- extract_lambda_ctx = extract_lambda_ctx ,
179+ def _handler (_event : Any , lambda_context : Any ) -> None :
180+ asyncio .run (
181+ _invocation_handler (
182+ lambda_context = lambda_context ,
183+ config = config ,
184+ deps = deps ,
185+ extract_lambda_ctx = extract_lambda_ctx ,
186+ )
189187 )
190188
191189 return _handler
@@ -210,9 +208,10 @@ async def _invocation_handler(
210208 work_time = remaining - shutdown_buffer
211209 if work_time <= timedelta (seconds = 1 ):
212210 raise RuntimeError (
213- f"Lambda timeout leaves almost no time for work "
214- f"(work_time={ work_time } , shutdown_buffer={ shutdown_buffer } ); "
215- f"increase the function timeout or decrease the shutdown "
211+ f"Lambda timeout is too short: { remaining .total_seconds ():.1f} s "
212+ f"remaining but { shutdown_buffer .total_seconds ():.1f} s is "
213+ f"reserved for shutdown, leaving no time for work. "
214+ f"Increase the function timeout or decrease the shutdown "
216215 f"deadline buffer"
217216 )
218217 elif work_time < timedelta (seconds = 5 ):
0 commit comments