1- """Backend registry for managing multiple backend configurations.
2-
3- This module provides a registry for managing multiple scheduler backends.
4- Each backend is a complete execution environment with its own scheduler,
5- execution manager, and optionally database manager.
6- """
7-
81import logging
92from dataclasses import dataclass
103from typing import Any , Dict , List , Optional , Type
1811
1912
2013def import_class (class_path : str ) -> Type :
21- """Import a class from a fully qualified class path.
22-
23- Parameters
24- ----------
25- class_path : str
26- Fully qualified class path (e.g., "jupyter_scheduler.scheduler.Scheduler")
27-
28- Returns
29- -------
30- Type
31- The imported class
32- """
14+ """Import a class from a fully qualified path like 'module.submodule.ClassName'."""
3315 module_path , class_name = class_path .rsplit ("." , 1 )
3416 module = __import__ (module_path , fromlist = [class_name ])
3517 return getattr (module , class_name )
3618
3719
3820@dataclass
3921class BackendInstance :
40- """A running instance of a backend with initialized scheduler.
41-
42- Attributes
43- ----------
44- config : BackendConfig
45- The configuration used to create this backend
46- scheduler : BaseScheduler
47- The initialized scheduler instance for this backend
48- """
22+ """A running backend with its configuration and initialized scheduler."""
4923
5024 config : BackendConfig
5125 scheduler : BaseScheduler
5226
5327
5428class BackendRegistry :
55- """Registry managing multiple backend configurations.
56-
57- This class is responsible for:
58- - Storing and managing multiple backend configurations
59- - Creating and initializing backend instances (schedulers)
60- - Routing requests to the appropriate backend based on ID or file extension
61-
62- Parameters
63- ----------
64- configs : List[BackendConfig]
65- List of backend configurations to register
66- default_backend : str
67- The ID of the default backend to use when none is specified
68- """
29+ """Registry for storing, initializing, and routing to scheduler backends."""
6930
7031 def __init__ (self , configs : List [BackendConfig ], default_backend : str ):
7132 self ._configs = configs
@@ -80,19 +41,7 @@ def initialize(
8041 db_url : str ,
8142 config : Optional [Any ] = None ,
8243 ):
83- """Instantiate all backends from configs.
84-
85- Parameters
86- ----------
87- root_dir : str
88- The Jupyter server root directory
89- environments_manager : EnvironmentManager
90- The environment manager instance to use
91- db_url : str
92- Default database URL (used if backend doesn't specify its own)
93- config : Any, optional
94- Traitlets config object
95- """
44+ """Instantiate all backends from configs."""
9645 for cfg in self ._configs :
9746 try :
9847 instance = self ._create_backend (cfg , root_dir , environments_manager , db_url , config )
@@ -118,26 +67,7 @@ def _create_backend(
11867 global_db_url : str ,
11968 config : Optional [Any ] = None ,
12069 ) -> BackendInstance :
121- """Create a backend instance from configuration.
122-
123- Parameters
124- ----------
125- cfg : BackendConfig
126- The backend configuration
127- root_dir : str
128- The Jupyter server root directory
129- environments_manager : EnvironmentManager
130- The environment manager instance
131- global_db_url : str
132- Default database URL (used if backend doesn't specify its own)
133- config : Any, optional
134- Traitlets config object
135-
136- Returns
137- -------
138- BackendInstance
139- The initialized backend instance
140- """
70+ """Create a backend instance from configuration."""
14171 scheduler_class = import_class (cfg .scheduler_class )
14272
14373 # Use backend-specific db_url if provided, otherwise use global
@@ -163,75 +93,30 @@ def _create_backend(
16393 return BackendInstance (config = cfg , scheduler = scheduler )
16494
16595 def get_backend (self , backend_id : str ) -> Optional [BackendInstance ]:
166- """Get a backend by its ID.
167-
168- Parameters
169- ----------
170- backend_id : str
171- The backend ID to look up
172-
173- Returns
174- -------
175- BackendInstance or None
176- The backend instance if found, None otherwise
177- """
96+ """Get a backend by ID, or None if not found."""
17897 return self ._backends .get (backend_id )
17998
18099 def get_default (self ) -> BackendInstance :
181- """Get the default backend.
182-
183- Returns
184- -------
185- BackendInstance
186- The default backend instance
187-
188- Raises
189- ------
190- KeyError
191- If the default backend is not found
192- """
100+ """Get the default backend."""
193101 if self ._default not in self ._backends :
194102 raise KeyError (f"Default backend '{ self ._default } ' not found in registry" )
195103 return self ._backends [self ._default ]
196104
197105 def get_for_file (self , input_uri : str ) -> BackendInstance :
198- """Auto-select backend based on file extension.
199-
200- If multiple backends support the file type, returns the one with
201- highest priority. If no backend matches the extension, returns
202- the default backend.
203-
204- Parameters
205- ----------
206- input_uri : str
207- The input file URI/path
208-
209- Returns
210- -------
211- BackendInstance
212- The selected backend instance
213- """
214- # Extract file extension
106+ """Auto-select backend by file extension (highest priority wins), or return default."""
215107 ext = ""
216108 if "." in input_uri :
217109 ext = input_uri .rsplit ("." , 1 )[- 1 ].lower ()
218110
219111 candidates = self ._extension_map .get (ext , [])
220112 if candidates :
221- # Return highest priority backend
222113 candidate_instances = [self ._backends [bid ] for bid in candidates ]
223114 return max (candidate_instances , key = lambda b : b .config .priority )
224115
225116 return self .get_default ()
226117
227118 def list_backends (self ) -> List [DescribeBackend ]:
228- """Return list of backends for API/UI.
229-
230- Returns
231- -------
232- List[DescribeBackend]
233- List of backend descriptions for frontend consumption
234- """
119+ """Return backend descriptions for API/UI consumption."""
235120 return [
236121 DescribeBackend (
237122 id = b .config .id ,
@@ -244,19 +129,11 @@ def list_backends(self) -> List[DescribeBackend]:
244129 ]
245130
246131 def list_backend_instances (self ) -> List [BackendInstance ]:
247- """Return list of all backend instances.
248-
249- Returns
250- -------
251- List[BackendInstance]
252- List of all backend instances
253- """
132+ """Return all backend instances."""
254133 return list (self ._backends .values ())
255134
256135 def __len__ (self ) -> int :
257- """Return the number of registered backends."""
258136 return len (self ._backends )
259137
260138 def __contains__ (self , backend_id : str ) -> bool :
261- """Check if a backend ID is registered."""
262139 return backend_id in self ._backends
0 commit comments