2828over HTTP.
2929"""
3030
31- import atexit
32- import json
33- import os
34- from collections .abc import Callable
3531from dataclasses import dataclass
36- from datetime import datetime
37- from pathlib import Path
3832
3933import structlog
4034
41- from genkit .core .constants import DEFAULT_GENKIT_VERSION
42-
4335logger = structlog .get_logger (__name__ )
4436
4537
@@ -58,55 +50,3 @@ class ServerSpec:
5850 def url (self ) -> str :
5951 """URL evaluates to the host base URL given the server specs."""
6052 return f'{ self .scheme } ://{ self .host } :{ self .port } '
61-
62-
63- def create_runtime (
64- runtime_dir : str ,
65- reflection_server_spec : ServerSpec ,
66- id : str ,
67- at_exit_fn : Callable [[Path ], None ] | None = None ,
68- ) -> Path :
69- """Create a runtime configuration for use with the genkit CLI.
70-
71- The runtime information is stored in the form of a timestamped JSON file.
72- Note that the file will be cleaned up as soon as the program terminates.
73-
74- Args:
75- runtime_dir: The directory to store the runtime file in.
76- reflection_server_spec: The server specification for the reflection
77- server.
78- id: The unique identifier for this runtime instance.
79- at_exit_fn: A function to call when the runtime file is deleted.
80-
81- Returns:
82- A path object representing the created runtime metadata file.
83- """
84- if not os .path .exists (runtime_dir ):
85- os .makedirs (runtime_dir )
86-
87- current_datetime = datetime .now ()
88- runtime_file_name = f'{ current_datetime .strftime ("%Y-%m-%d_%H-%M-%S.%f" )} .json'
89- runtime_file_path = Path (os .path .join (runtime_dir , runtime_file_name ))
90- metadata = json .dumps ({
91- 'reflectionApiSpecVersion' : 1 ,
92- 'id' : id ,
93- 'pid' : os .getpid (),
94- 'genkitVersion' : 'py/' + DEFAULT_GENKIT_VERSION ,
95- 'reflectionServerUrl' : reflection_server_spec .url ,
96- 'timestamp' : f'{ current_datetime .isoformat ()} ' ,
97- })
98- runtime_file_path .write_text (metadata , encoding = 'utf-8' )
99-
100- def cleanup_runtime () -> None :
101- logger .info ('Cleaning up runtime file' , path = runtime_file_path )
102- at_exit_fn (runtime_file_path )
103-
104- if at_exit_fn :
105- atexit .register (cleanup_runtime )
106- return runtime_file_path
107-
108-
109- def init_default_runtime (spec : ServerSpec , id : str ) -> None :
110- """Initialize the runtime for the Genkit instance."""
111- runtimes_dir = os .path .join (os .getcwd (), '.genkit/runtimes' )
112- create_runtime (runtime_dir = runtimes_dir , reflection_server_spec = spec , id = id , at_exit_fn = os .remove )
0 commit comments