44import pwd
55import psutil
66import time
7+ import glob
78
89nginx_global_conf = "/etc/nginx/nginx.conf"
910nginx_config_dir = "/etc/nginx/conf.d"
@@ -174,8 +175,18 @@ def php_fpm_create_conf_file(test_dir, test_name, user, env):
174175 return php_fpm_config_file_path
175176
176177
178+ def cleanup_glob (pattern ):
179+ for f in glob .glob (pattern ):
180+ try :
181+ if os .path .isdir (f ):
182+ subprocess .run (['rm' , '-rf' , f ], check = False )
183+ else :
184+ os .remove (f )
185+ except Exception as e :
186+ print (f"Failed to remove { f } : { e } " )
187+
177188def nginx_php_fpm_init (tests_dir ):
178- subprocess . run ([ 'rm' , '-rf' , f'{ php_fpm_config_dir } /*' ] )
189+ cleanup_glob ( f'{ php_fpm_config_dir } /*' )
179190
180191
181192def nginx_php_fpm_process_test (test_data ):
@@ -186,31 +197,54 @@ def nginx_php_fpm_process_test(test_data):
186197 return test_data
187198
188199
200+ def wait_for_fpm_sockets (timeout = 15 ):
201+ """Wait for all PHP-FPM pool sockets to be created."""
202+ expected_sockets = glob .glob (f'{ php_fpm_config_dir } /*.conf' )
203+ if not expected_sockets :
204+ return True
205+
206+ start_time = time .time ()
207+ while time .time () - start_time < timeout :
208+ existing_sockets = glob .glob (f'{ php_fpm_run_dir } /php-fpm-*.sock' )
209+ if len (existing_sockets ) >= len (expected_sockets ):
210+ print (f"All { len (existing_sockets )} PHP-FPM sockets are ready" )
211+ return True
212+ time .sleep (0.5 )
213+
214+ existing_sockets = glob .glob (f'{ php_fpm_run_dir } /php-fpm-*.sock' )
215+ print (f"Warning: Only { len (existing_sockets )} /{ len (expected_sockets )} PHP-FPM sockets ready after { timeout } s" )
216+ return False
217+
189218def nginx_php_fpm_pre_tests ():
190- subprocess .run (['pkill' , 'nginx' ])
191- subprocess .run (['pkill' , '-9' , 'php-fpm' ])
219+ subprocess .run (['pkill' , 'nginx' ], check = False )
220+ subprocess .run (['pkill' , '-9' , 'php-fpm' ], check = False )
192221 time .sleep (2 )
193222
194- subprocess .run (['rm' , '-rf' , f'{ log_dir } /nginx/*' ])
195- subprocess .run (['rm' , '-rf' , f'{ log_dir } /php-fpm/*' ])
196- subprocess .run (['rm' , '-rf' , f'{ log_dir } /aikido-*/*' ])
197- subprocess .run (['rm' , '-rf' , f'{ php_fpm_run_dir } /*' ])
198- subprocess .run (['rm' , '-f' , '/var/run/php-fpm.pid' ])
199- subprocess .run (['rm' , '-f' , '/run/php-fpm.pid' ])
223+ cleanup_glob (f'{ log_dir } /nginx/*' )
224+ cleanup_glob (f'{ log_dir } /php-fpm/*' )
225+ cleanup_glob (f'{ log_dir } /aikido-*/*' )
226+ cleanup_glob (f'{ php_fpm_run_dir } /*' )
227+ for pid_file in ['/var/run/php-fpm.pid' , '/run/php-fpm.pid' ]:
228+ try :
229+ os .remove (pid_file )
230+ except FileNotFoundError :
231+ pass
200232
201233 create_folder (php_fpm_run_dir )
202234 create_folder (f'{ log_dir } /php-fpm' )
203235 modify_nginx_conf (nginx_global_conf )
204236 subprocess .run (['nginx' ], check = True )
205237 subprocess .run ([php_fpm_bin , '--allow-to-run-as-root' ], check = True )
206- print ("nginx and php-fpm servers restarted! " )
207- time . sleep ( 5 )
238+ print ("nginx and php-fpm servers started, waiting for sockets... " )
239+ wait_for_fpm_sockets ( )
208240
209241
210242def nginx_php_fpm_start_server (test_data , test_lib_dir , valgrind ):
211243 return None
212244
213245
214246def nginx_php_fpm_uninit ():
215- subprocess .run (['pkill' , 'nginx' ])
216- subprocess .run (['pkill' , 'php-fpm' ])
247+ subprocess .run (['pkill' , 'nginx' ], check = False )
248+ subprocess .run (['pkill' , '-9' , 'php-fpm' ], check = False )
249+ time .sleep (1 )
250+ cleanup_glob (f'{ php_fpm_run_dir } /*' )
0 commit comments