-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhandler_generator.py
More file actions
475 lines (389 loc) · 15.1 KB
/
handler_generator.py
File metadata and controls
475 lines (389 loc) · 15.1 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
"""Generator for handler_<name>.py files."""
import ast
import logging
from pathlib import Path
from typing import Any, Dict, List, Union
from runpod_flash.runtime.models import Manifest
logger = logging.getLogger(__name__)
DEPLOYED_HANDLER_TEMPLATE = '''"""
Auto-generated deployed handler for resource: {resource_name}
Generated at: {timestamp}
Deployed endpoint handler: accepts plain JSON, no cloudpickle.
One function per endpoint, identified by FLASH_RESOURCE_NAME.
This file is generated by the Flash build process. Do not edit manually.
"""
import asyncio
import importlib
import inspect
import logging
import traceback
_logger = logging.getLogger(__name__)
# Import the function for this endpoint
{import_statement}
def handler(job):
"""Handler for deployed QB endpoint. Accepts plain JSON kwargs."""
job_input = job.get("input", {{}})
try:
result = {function_name}(**job_input)
if inspect.iscoroutine(result):
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as pool:
result = pool.submit(asyncio.run, result).result()
else:
result = asyncio.run(result)
return result
except Exception as e:
_logger.error(
"Deployed handler error for {function_name}: %s",
e,
exc_info=True,
)
return {{"error": str(e), "traceback": traceback.format_exc()}}
if __name__ == "__main__":
import runpod
runpod.serverless.start({{"handler": handler}})
'''
DEPLOYED_ASYNC_HANDLER_TEMPLATE = '''"""
Auto-generated deployed handler for resource: {resource_name}
Generated at: {timestamp}
Concurrent async endpoint handler: accepts plain JSON, no cloudpickle.
max_concurrency={max_concurrency}
This file is generated by the Flash build process. Do not edit manually.
"""
import importlib
import logging
import traceback
_logger = logging.getLogger(__name__)
# Import the function for this endpoint
{import_statement}
async def handler(job):
"""Async handler for concurrent QB endpoint. Accepts plain JSON kwargs."""
job_input = job.get("input", {{}})
try:
result = await {function_name}(**job_input)
return result
except Exception as e:
_logger.error(
"Deployed handler error for {function_name}: %s",
e,
exc_info=True,
)
return {{"error": str(e), "traceback": traceback.format_exc()}}
if __name__ == "__main__":
import runpod
runpod.serverless.start({{
"handler": handler,
"concurrency_modifier": lambda current: {max_concurrency},
}})
'''
DEPLOYED_CLASS_HANDLER_TEMPLATE = '''"""
Auto-generated deployed handler for class-based resource: {resource_name}
Generated at: {timestamp}
Deployed endpoint handler for a @remote class. Instantiates the class
once at cold start, then dispatches to methods per request.
This file is generated by the Flash build process. Do not edit manually.
"""
import asyncio
import importlib
import inspect
import logging
import traceback
_logger = logging.getLogger(__name__)
# import the class for this endpoint
{import_statement}
# instantiate once at cold start
_instance = {class_name}()
# public methods available for dispatch
_METHODS = {methods_dict}
def _run_maybe_async(result):
"""resolve a possibly-async result to a plain value."""
if inspect.iscoroutine(result):
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as pool:
return pool.submit(asyncio.run, result).result()
return asyncio.run(result)
return result
def handler(job):
"""Handler for deployed class-based QB endpoint.
Dispatches to a method on the singleton class instance.
If the class has exactly one public method, input is passed
directly as kwargs. If multiple methods exist, input must
include a "method" key to select the target.
"""
job_input = job.get("input", {{}})
try:
if len(_METHODS) == 1:
method_name = next(iter(_METHODS))
else:
method_name = job_input.pop("method", None)
if method_name is None:
return {{
"error": (
"class {class_name} has multiple methods: "
+ ", ".join(sorted(_METHODS))
+ ". include a \\"method\\" key in the input."
)
}}
if method_name not in _METHODS:
return {{
"error": (
f"unknown method '{{method_name}}' on {class_name}. "
f"available: {{', '.join(sorted(_METHODS))}}"
)
}}
method = getattr(_instance, method_name)
result = _run_maybe_async(method(**job_input))
return result
except Exception as e:
_logger.error(
"Deployed handler error for {class_name}.%s: %s",
job_input.get("method", "<default>"),
e,
exc_info=True,
)
return {{"error": str(e), "traceback": traceback.format_exc()}}
if __name__ == "__main__":
import runpod
runpod.serverless.start({{"handler": handler}})
'''
DEPLOYED_ASYNC_CLASS_HANDLER_TEMPLATE = '''"""
Auto-generated deployed handler for class-based resource: {resource_name}
Generated at: {timestamp}
Concurrent async class endpoint handler: accepts plain JSON, no cloudpickle.
max_concurrency={max_concurrency}
This file is generated by the Flash build process. Do not edit manually.
"""
import importlib
import logging
import traceback
_logger = logging.getLogger(__name__)
# import the class for this endpoint
{import_statement}
# instantiate once at cold start
_instance = {class_name}()
# public methods available for dispatch
_METHODS = {methods_dict}
async def handler(job):
"""Async handler for concurrent class-based QB endpoint.
Dispatches to a method on the singleton class instance.
If the class has exactly one public method, input is passed
directly as kwargs. If multiple methods exist, input must
include a "method" key to select the target.
"""
job_input = job.get("input", {{}})
try:
if len(_METHODS) == 1:
method_name = next(iter(_METHODS))
else:
method_name = job_input.pop("method", None)
if method_name is None:
return {{
"error": (
"class {class_name} has multiple methods: "
+ ", ".join(sorted(_METHODS))
+ ". include a \\"method\\" key in the input."
)
}}
if method_name not in _METHODS:
return {{
"error": (
f"unknown method '{{method_name}}' on {class_name}. "
f"available: {{', '.join(sorted(_METHODS))}}"
)
}}
method = getattr(_instance, method_name)
result = await method(**job_input)
return result
except Exception as e:
_logger.error(
"Deployed handler error for {class_name}.%s: %s",
job_input.get("method", "<default>"),
e,
exc_info=True,
)
return {{"error": str(e), "traceback": traceback.format_exc()}}
if __name__ == "__main__":
import runpod
runpod.serverless.start({{
"handler": handler,
"concurrency_modifier": lambda current: {max_concurrency},
}})
'''
class HandlerGenerator:
"""Generates handler_<name>.py files for each resource config."""
def __init__(self, manifest: Union[Dict[str, Any], Manifest], build_dir: Path):
self.manifest = manifest
self.build_dir = build_dir
def generate_handlers(self) -> List[Path]:
"""Generate all handler files for queue-based (non-LB) resources."""
handler_paths = []
# Handle both dict and Manifest types
resources = (
self.manifest.resources
if isinstance(self.manifest, Manifest)
else self.manifest.get("resources", {})
)
for resource_name, resource_data in resources.items():
# Skip load-balanced resources (handled by LBHandlerGenerator)
# Use flag determined by isinstance() at scan time
is_load_balanced = (
resource_data.is_load_balanced
if hasattr(resource_data, "is_load_balanced")
else resource_data.get("is_load_balanced", False)
)
if is_load_balanced:
continue
handler_path = self._generate_handler(resource_name, resource_data)
handler_paths.append(handler_path)
return handler_paths
def _generate_handler(self, resource_name: str, resource_data: Any) -> Path:
"""Generate a handler file for a deployed QB endpoint."""
handler_filename = f"handler_{resource_name}.py"
handler_path = self.build_dir / handler_filename
timestamp = (
self.manifest.generated_at
if isinstance(self.manifest, Manifest)
else self.manifest.get("generated_at", "")
)
functions = (
resource_data.functions
if hasattr(resource_data, "functions")
else resource_data.get("functions", [])
)
max_concurrency = (
resource_data.max_concurrency
if hasattr(resource_data, "max_concurrency")
else resource_data.get("max_concurrency", 1)
)
handler_code = self._generate_deployed_handler_code(
resource_name, timestamp, functions, max_concurrency
)
handler_path.write_text(handler_code)
self._validate_handler_imports(handler_path)
return handler_path
def _generate_deployed_handler_code(
self,
resource_name: str,
timestamp: str,
functions: List[Any],
max_concurrency: int = 1,
) -> str:
"""Generate deployed handler code for a QB endpoint.
Selects template based on max_concurrency and is_async:
- max_concurrency=1: current sync templates (unchanged behavior)
- max_concurrency>1 + async: new async templates with concurrency_modifier
- max_concurrency>1 + sync: sync templates with concurrency_modifier injected
"""
if not functions:
raise ValueError(
f"Resource '{resource_name}' has no functions. "
f"Cannot generate a deployed handler without at least one function."
)
func = functions[0]
module = func.module if hasattr(func, "module") else func.get("module")
name = func.name if hasattr(func, "name") else func.get("name")
is_class = (
func.is_class if hasattr(func, "is_class") else func.get("is_class", False)
)
is_async = (
func.is_async if hasattr(func, "is_async") else func.get("is_async", False)
)
import_statement = (
f"{name} = importlib.import_module('{module}').{name}"
if module and name
else "# No function to import"
)
if max_concurrency > 100:
logger.warning(
"High max_concurrency=%d for resource '%s'. Ensure your handler "
"and GPU can support this level of concurrent execution.",
max_concurrency,
resource_name,
)
if max_concurrency > 1 and not is_async:
logger.warning(
"max_concurrency=%d set on sync handler '%s'. "
"Only async handlers benefit from concurrent execution. "
"Consider making the handler async.",
max_concurrency,
resource_name,
)
if is_class:
class_methods = (
func.class_methods
if hasattr(func, "class_methods")
else func.get("class_methods", [])
)
methods_dict = {m: m for m in class_methods} if class_methods else {}
if max_concurrency > 1 and is_async:
return DEPLOYED_ASYNC_CLASS_HANDLER_TEMPLATE.format(
resource_name=resource_name,
timestamp=timestamp,
import_statement=import_statement,
class_name=name or "None",
methods_dict=repr(methods_dict),
max_concurrency=max_concurrency,
)
code = DEPLOYED_CLASS_HANDLER_TEMPLATE.format(
resource_name=resource_name,
timestamp=timestamp,
import_statement=import_statement,
class_name=name or "None",
methods_dict=repr(methods_dict),
)
if max_concurrency > 1:
code = self._inject_concurrency_modifier(code, max_concurrency)
return code
# Function-based handler
if max_concurrency > 1 and is_async:
return DEPLOYED_ASYNC_HANDLER_TEMPLATE.format(
resource_name=resource_name,
timestamp=timestamp,
import_statement=import_statement,
function_name=name or "None",
max_concurrency=max_concurrency,
)
code = DEPLOYED_HANDLER_TEMPLATE.format(
resource_name=resource_name,
timestamp=timestamp,
import_statement=import_statement,
function_name=name or "None",
)
if max_concurrency > 1:
code = self._inject_concurrency_modifier(code, max_concurrency)
return code
@staticmethod
def _inject_concurrency_modifier(code: str, max_concurrency: int) -> str:
"""Replace the default runpod.serverless.start call with one including concurrency_modifier."""
return code.replace(
'runpod.serverless.start({"handler": handler})',
"runpod.serverless.start({\n"
' "handler": handler,\n'
f' "concurrency_modifier": lambda current: {max_concurrency},\n'
" })",
)
def _validate_handler_imports(self, handler_path: Path) -> None:
"""Validate that generated handler has valid Python syntax.
Uses ast.parse() to check syntax without executing the module.
This avoids ImportErrors from modules that only resolve at runtime
inside Docker (e.g., numeric-prefixed module paths).
Args:
handler_path: Path to generated handler file
Raises:
ValueError: If handler has syntax errors
"""
try:
source = handler_path.read_text(encoding="utf-8")
ast.parse(source, filename=str(handler_path))
except SyntaxError as e:
raise ValueError(f"Handler has syntax errors: {e}") from e