1010
1111logger = logging .getLogger (__name__ )
1212
13+ # Forward declaration variable so runtime references succeed before instantiation
14+ ray_config : Optional ["RayConfig" ] = None
15+
1316
1417class RayConfig :
1518 """Ray configuration manager"""
@@ -82,8 +85,13 @@ def init_ray(self, **kwargs) -> bool:
8285
8386 params = self .get_init_params (** kwargs )
8487
85- logger .info ("Initializing Ray cluster..." )
86- logger .debug (f"Ray configuration parameters:" )
88+ # Get Ray configuration from environment
89+ ray_num_cpus = os .environ .get ('RAY_NUM_CPUS' )
90+ num_cpus = int (ray_num_cpus ) if ray_num_cpus else None # None lets Ray decide
91+
92+ # Log the attempt to initialize
93+ logger .debug ("Initializing Ray cluster..." )
94+ logger .debug ("Ray configuration parameters:" )
8795 for key , value in params .items ():
8896 if key .startswith ('_' ):
8997 logger .debug (f" { key } : { value } " )
@@ -133,7 +141,7 @@ def connect_to_cluster(self, address: str = "auto") -> bool:
133141 return True
134142
135143 except Exception as e :
136- logger .info (f"Failed to connect to Ray cluster: { str (e )} " )
144+ logger .info (f"Cannot connect to Ray cluster: { str (e )} " )
137145 return False
138146
139147 def start_local_cluster (self ,
@@ -167,54 +175,36 @@ def log_configuration(self):
167175 logger .debug (f" ObjectStore memory: { self .object_store_memory_gb } GB" )
168176 logger .debug (f" Temp directory: { self .temp_dir } " )
169177
178+ @classmethod
179+ def init_ray_for_worker (cls , address : str = "auto" ) -> bool :
180+ """Initialize Ray connection for Celery Worker (class method wrapper)."""
181+ logger .info ("Initialize Ray connection for Celery Worker..." )
182+ ray_config .log_configuration ()
183+ return ray_config .connect_to_cluster (address )
170184
171- # Create a global RayConfiguration instance
172- ray_config = RayConfig ()
185+ @classmethod
186+ def init_ray_for_service (cls ,
187+ num_cpus : Optional [int ] = None ,
188+ dashboard_port : int = 8265 ,
189+ try_connect_first : bool = True ,
190+ include_dashboard : bool = True ) -> bool :
191+ """Initialize Ray for data processing service (class method wrapper)."""
192+ ray_config .log_configuration ()
173193
194+ if try_connect_first :
195+ # Try to connect to existing cluster first
196+ logger .debug ("Trying to connect to existing Ray cluster..." )
197+ if ray_config .connect_to_cluster ("auto" ):
198+ return True
199+ logger .info ("Starting local cluster..." )
174200
175- def init_ray_for_worker (address : str = "auto" ) -> bool :
176- """
177- Initialize Ray connection for Celery Worker
178-
179- Args:
180- address: Ray cluster address
181-
182- Returns:
183- Whether initialization is successful
184- """
185- logger .info ("Initialize Ray connection for Celery Worker..." )
186- ray_config .log_configuration ()
187-
188- return ray_config .connect_to_cluster (address )
189-
201+ # Start local cluster
202+ return ray_config .start_local_cluster (
203+ num_cpus = num_cpus ,
204+ include_dashboard = include_dashboard ,
205+ dashboard_port = dashboard_port
206+ )
190207
191- def init_ray_for_service (num_cpus : Optional [int ] = None ,
192- dashboard_port : int = 8265 ,
193- try_connect_first : bool = True ) -> bool :
194- """
195- Initialize Ray for data processing service
196-
197- Args:
198- num_cpus: Number of CPU cores
199- dashboard_port: Dashboard port
200- try_connect_first: Whether to try connecting to existing cluster first
201-
202- Returns:
203- Whether initialization is successful
204- """
205- ray_config .log_configuration ()
206-
207- if try_connect_first :
208- # Try to connect to existing cluster first
209- logger .debug ("Trying to connect to existing Ray cluster..." )
210- if ray_config .connect_to_cluster ("auto" ):
211- return True
212-
213- logger .info ("Starting local cluster..." )
214-
215- # Start local cluster
216- return ray_config .start_local_cluster (
217- num_cpus = num_cpus ,
218- dashboard_port = dashboard_port
219- )
208+ # Create a global RayConfig instance accessible throughout the module
209+ ray_config = RayConfig ()
220210
0 commit comments