ivcap-lambda 0.8.9__tar.gz → 0.8.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivcap-lambda
3
- Version: 0.8.9
3
+ Version: 0.8.11
4
4
  Summary: Helper functions for building lambda-style services on the IVCAP platform
5
5
  License-File: AUTHORS.md
6
6
  License-File: LICENSE
@@ -65,6 +65,10 @@ class ToolOptions(BaseModel):
65
65
  service_id: str | None = Field(
66
66
  None, description="overriding the default service id"
67
67
  )
68
+ service_timeout: int | None = Field(
69
+ None,
70
+ description="service timeout in seconds, included in the REST service description",
71
+ )
68
72
  is_ready: Callable[[], bool | dict[str, Any] | BaseModel] | None = Field(
69
73
  None,
70
74
  description=(
@@ -88,6 +92,7 @@ class ToolDescription:
88
92
  worker_fn: WorkerFn
89
93
  input: tuple[type[BaseModel] | None, dict[str, Any]]
90
94
  executor: Executor
95
+ service_timeout: int | None = None
91
96
 
92
97
 
93
98
  tools: list[ToolDescription] = []
@@ -155,6 +160,7 @@ def add_tool_api_route(
155
160
  worker_fn=worker_fn,
156
161
  input=get_input_type(worker_fn),
157
162
  executor=executor,
163
+ service_timeout=opts.service_timeout,
158
164
  )
159
165
  )
160
166
 
@@ -4,6 +4,8 @@
4
4
  # found in the LICENSE file. See the AUTHORS file for names of contributors.
5
5
  #
6
6
  import argparse
7
+ import asyncio
8
+ import json
7
9
  import os
8
10
  import sys
9
11
  from collections.abc import Callable
@@ -15,6 +17,8 @@ from typing import Any
15
17
  import uvicorn
16
18
  from fastapi import FastAPI, Request, Response
17
19
  from ivcap_service import (
20
+ ExecutionError,
21
+ IvcapResult,
18
22
  Service,
19
23
  SidecarReporter,
20
24
  getLogger,
@@ -24,14 +28,16 @@ from ivcap_service import (
24
28
  service_log_config,
25
29
  set_context,
26
30
  set_event_reporter_factory,
31
+ set_sidecar_delivery_disabled,
27
32
  )
28
33
  from ivcap_service import (
29
34
  get_version as get_service_version,
30
35
  )
31
36
  from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
37
+ from starlette.requests import Request as StarletteRequest
32
38
 
33
39
  # from .context import set_context, otel_instrument
34
- from .builder import tools
40
+ from .builder import ToolDescription, tools
35
41
  from .executor import Executor, get_job_context
36
42
  from .health_handler import add_health_route
37
43
  from .version import get_version, resolve_service_version
@@ -53,6 +59,58 @@ def get_fast_app() -> FastAPI:
53
59
  return _app
54
60
 
55
61
 
62
+ def run_test_file(path: str, tool: ToolDescription) -> None:
63
+ """Run `tool` once on a job file in the ivcap-service batch format and print the result.
64
+
65
+ The job file has the same shape as the ones used by ivcap-service's
66
+ `--test-file`: `id`, `in-content-type` and `in-content`.
67
+
68
+ Args:
69
+ path (str): path to the job JSON file
70
+ tool (ToolDescription): the registered tool to execute
71
+ """
72
+ with open(path) as f:
73
+ job = json.load(f)
74
+ job_id = job.get("id", "unknown_job_id")
75
+ ct = job["in-content-type"]
76
+ if ct != "application/json":
77
+ raise ValueError(f"cannot handle content-type '{ct}'")
78
+ input_model, _ = tool.input
79
+ if input_model is None:
80
+ raise ValueError(f"tool '{tool.name}' has no input model")
81
+ param = input_model(**job["in-content"])
82
+
83
+ scope = {
84
+ "type": "http",
85
+ "method": "POST",
86
+ "scheme": "http",
87
+ "server": ("localhost", 8090),
88
+ "path": "/",
89
+ "query_string": b"",
90
+ "headers": [],
91
+ }
92
+
93
+ async def _execute():
94
+ queue = await tool.executor.execute(
95
+ param, job_id, StarletteRequest(scope), report_result=False
96
+ )
97
+ return await queue.get()
98
+
99
+ res = asyncio.run(_execute())
100
+ if isinstance(res, IvcapResult):
101
+ content = res.content
102
+ try:
103
+ if not isinstance(content, (bytes, str)):
104
+ raise ValueError("content is not in-memory")
105
+ print(json.dumps(json.loads(content), indent=2))
106
+ except ValueError:
107
+ print(content)
108
+ else:
109
+ print(res.model_dump_json(indent=2))
110
+ if isinstance(res, ExecutionError):
111
+ sys.exit(1)
112
+
113
+
56
114
  def start_lambda_server(
57
115
  service: Service,
58
116
  *,
@@ -152,6 +210,25 @@ def start_lambda_server(
152
210
  help=f"Print tool description to stdout [{','.join(tool_names)}]",
153
211
  )
154
212
 
213
+ parser.add_argument(
214
+ "--test-file",
215
+ type=str,
216
+ help=(
217
+ "Run the first tool on a job file (ivcap-service format with 'id', "
218
+ "'in-content-type' and 'in-content') and print the result, then exit"
219
+ ),
220
+ )
221
+ parser.add_argument(
222
+ "--test-without-sidecar",
223
+ action="store_true",
224
+ help=(
225
+ "Silently drop any attempt to deliver events or results to the "
226
+ "sidecar (no HTTP calls, no retries, no warnings). Useful for "
227
+ "local testing (typically together with --test-file) without a "
228
+ "reachable IVCAP_BASE_URL/sidecar."
229
+ ),
230
+ )
231
+
155
232
  if custom_args is not None:
156
233
  args = custom_args(parser)
157
234
  else:
@@ -191,7 +268,11 @@ def start_lambda_server(
191
268
  tool_slug = tool.name.replace("_", "-")
192
269
  name = args.service_name or f"{service.name}-{tool_slug}"
193
270
  print_rest_service_definition(
194
- service, tool.worker_fn, name=name, path=tool.path_prefix
271
+ service,
272
+ tool.worker_fn,
273
+ name=name,
274
+ path=tool.path_prefix,
275
+ service_timeout=tool.service_timeout,
195
276
  )
196
277
  sys.exit(0)
197
278
 
@@ -214,6 +295,13 @@ def start_lambda_server(
214
295
  elif getattr(args, "with_telemetry", False):
215
296
  effective_with_telemetry = True
216
297
 
298
+ if args.test_without_sidecar:
299
+ logger.info(
300
+ "--test-without-sidecar set - events/results will not be delivered "
301
+ "to the sidecar"
302
+ )
303
+ set_sidecar_delivery_disabled(True)
304
+
217
305
  set_event_reporter_factory(SidecarReporter) # type: ignore[arg-type]
218
306
 
219
307
  def get_context():
@@ -226,6 +314,10 @@ def start_lambda_server(
226
314
 
227
315
  set_context(get_context)
228
316
 
317
+ if args.test_file is not None:
318
+ run_test_file(args.test_file, tools[0])
319
+ return
320
+
229
321
  if args.with_mcp_stdio:
230
322
  # Run as a stdio MCP server instead of the HTTP/uvicorn server: no
231
323
  # FastAPI app involved, so there is nothing to instrument there, but
@@ -26,6 +26,9 @@ class RestController(BaseModel):
26
26
  image: str
27
27
  command: list[str] | str
28
28
  resources: Resources = Field(default_factory=Resources)
29
+ service_timeout: int | None = Field(
30
+ default=None, description="service timeout in seconds"
31
+ )
29
32
 
30
33
  def print_rest_service_definition(
31
34
  service_description: Service,
@@ -33,6 +36,7 @@ def print_rest_service_definition(
33
36
  service_id: str | None = None,
34
37
  name: str | None = None,
35
38
  path: str | None = None,
39
+ service_timeout: int | None = None,
36
40
  ):
37
41
  sd = create_rest_service_definition(
38
42
  service_description,
@@ -40,6 +44,7 @@ def print_rest_service_definition(
40
44
  service_id=service_id,
41
45
  name=name,
42
46
  path=path,
47
+ service_timeout=service_timeout,
43
48
  )
44
49
  print(sd.model_dump_json(indent=2, by_alias=True, exclude_none=True))
45
50
 
@@ -49,13 +54,23 @@ def create_rest_service_definition(
49
54
  service_id: str | None = None,
50
55
  name: str | None = None,
51
56
  path: str | None = None,
57
+ service_timeout: int | None = None,
52
58
  ) -> ServiceDefinition:
53
59
  # controller
54
60
  image = os.getenv("DOCKER_IMG", IMAGE_PLACEHOLDER)
55
61
 
56
62
  command = find_command()
57
63
  resources = find_resources_file()
58
- controller = RestController(path=path, image=image, command=command, resources=resources)
64
+ env_timeout = os.getenv("IVCAP_SERVICE_TIMEOUT")
65
+ if env_timeout:
66
+ service_timeout = int(env_timeout)
67
+ controller = RestController(
68
+ path=path,
69
+ image=image,
70
+ command=command,
71
+ resources=resources,
72
+ service_timeout=service_timeout,
73
+ )
59
74
  if name is not None:
60
75
  service_description = service_description.model_copy(update={"name": name})
61
76
  return create_service_definition(service_description, fn, REST_CONTROLLER_SCHEMA, controller, service_id)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ivcap-lambda"
3
- version = "0.8.9"
3
+ version = "0.8.11"
4
4
  description = "Helper functions for building lambda-style services on the IVCAP platform"
5
5
 
6
6
  authors = ["Max Ott <max.ott@csiro.au>"]
File without changes
File without changes
File without changes