39 if (self::$serviceMap ===
null) {
51 if (self::$syntaxCheckMap ===
null) {
52 self::$syntaxCheckMap = [
70 return isset(self::$serviceMap[$serviceName]) ? self::$serviceMap[$serviceName] :
null;
85 return isset(self::$syntaxCheckMap[$serviceName]) ? self::$syntaxCheckMap[$serviceName] :
null;
97 return $bin->getName() .
' ' . $bin->getVersion() .
' (' . $service->getName() .
')';
111 foreach (
$bearsamppBins->getServices() as $serviceName => $service) {
116 $callback($serviceName, $service, $bin, $syntaxCheckCmd);
129 public static function startService($bin, $syntaxCheckCmd =
null, $showErrors =
true)
142 return $service->stop();
153 return $service->restart();
182 if (self::$syntaxCheckMap ===
null) {
184 self::$syntaxCheckMap = [
190 return isset(self::$syntaxCheckMap[$serviceName]);
208 if (method_exists($bin,
'getPort')) {
209 return $bin->getPort();
210 } elseif (method_exists($bin,
'getSmtpPort')) {
211 return $bin->getSmtpPort();
232 Log::trace(
'ServiceHelper::stopAllServicesParallel() starting');
233 $startTime = microtime(
true);
236 if (empty($services)) {
241 Log::trace(
'Starting parallel shutdown of ' . count($services) .
' services');
246 if ($parallelSuccess) {
247 $duration = round(microtime(
true) - $startTime, 3);
248 Log::info(
'Parallel shutdown completed successfully in ' . $duration .
's');
253 Log::info(
'Parallel shutdown timed out, falling back to sequential shutdown');
256 $totalDuration = round(microtime(
true) - $startTime, 3);
257 Log::info(
'Sequential shutdown completed in ' . $totalDuration .
's');
259 return $sequentialSuccess;
276 Log::trace(
'Phase 1: Sending stop commands to all services');
277 $totalServices = count($services);
281 foreach ($services as $serviceName => $service) {
283 if ($progressCallback) {
284 $progressCallback($currentIndex, $totalServices, $serviceName);
287 Log::trace(
'Sending stop to: ' . $serviceName);
292 Log::trace(
'Phase 2: Monitoring service status');
293 $monitorStartTime = microtime(
true);
294 $monitorTimeout = $shutdownTimeout;
295 $checkInterval = 0.5;
298 while ((microtime(
true) - $monitorStartTime) < $monitorTimeout) {
301 foreach ($services as $serviceName => $service) {
302 if (!$service->isStopped()) {
309 Log::trace(
'All services stopped in parallel phase');
313 usleep($checkInterval * 1000000);
317 Log::trace(
'Phase 3: Force killing remaining services');
318 foreach ($services as $serviceName => $service) {
319 if (!$service->isStopped()) {
341 Log::trace(
'Starting sequential shutdown phase');
342 $totalServices = count($services);
345 foreach ($services as $serviceName => $service) {
347 if ($progressCallback) {
348 $progressCallback($currentIndex, $totalServices, $serviceName);
351 Log::trace(
'Sequential stop: ' . $serviceName);
354 if (!$service->isStopped()) {
379 'nodejs' =>
'node.exe',
382 if (isset($processMap[$serviceName])) {
383 Log::trace(
'Killing process: ' . $processMap[$serviceName]);
399 foreach ($services as $serviceName => $service) {
400 if (!$service->isStopped()) {
422 Log::trace(
'ServiceHelper::startAllServicesParallel() starting with ' . count($serviceInfos) .
' services');
423 $startTime = microtime(
true);
425 if (empty($serviceInfos)) {
430 Log::trace(
'Starting parallel startup of ' . count($serviceInfos) .
' services');
435 if ($parallelSuccess) {
436 $duration = round(microtime(
true) - $startTime, 3);
437 Log::info(
'Parallel startup completed successfully in ' . $duration .
's');
442 Log::info(
'Parallel startup timed out, falling back to sequential startup');
445 $totalDuration = round(microtime(
true) - $startTime, 3);
446 Log::info(
'Sequential startup completed in ' . $totalDuration .
's');
448 return $sequentialSuccess;
463 private static function startServicesParallel($serviceInfos, ?callable $progressCallback =
null, $startupTimeout = 30)
465 Log::trace(
'Phase 1: Sending start commands to all services');
466 $totalServices = count($serviceInfos);
470 foreach ($serviceInfos as $serviceName => $serviceInfo) {
472 if ($progressCallback) {
473 $progressCallback($currentIndex, $totalServices, $serviceInfo[
'name']);
476 Log::trace(
'Sending start to: ' . $serviceName);
477 $service = $serviceInfo[
'service'];
484 Log::trace(
'Phase 2: Monitoring service status');
485 $monitorStartTime = microtime(
true);
486 $monitorTimeout = $startupTimeout;
487 $checkInterval = 0.5;
489 $failedServices = [];
491 while ((microtime(
true) - $monitorStartTime) < $monitorTimeout) {
494 foreach ($serviceInfos as $serviceName => $serviceInfo) {
495 $service = $serviceInfo[
'service'];
497 if (!$service->isRunning()) {
499 $failedServices[$serviceName] = $serviceInfo;
504 Log::trace(
'All services running in parallel phase');
508 usleep($checkInterval * 1000000);
512 if (!empty($failedServices)) {
513 Log::trace(
'Phase 3: Retrying ' . count($failedServices) .
' failed services');
515 foreach ($failedServices as $serviceName => $serviceInfo) {
516 $service = $serviceInfo[
'service'];
518 if (!$service->isRunning()) {
519 Log::trace(
'Retrying start for: ' . $serviceName);
544 Log::trace(
'Starting sequential startup phase');
545 $totalServices = count($serviceInfos);
548 foreach ($serviceInfos as $serviceName => $serviceInfo) {
550 if ($progressCallback) {
551 $progressCallback($currentIndex, $totalServices, $serviceInfo[
'name']);
554 Log::trace(
'Sequential start: ' . $serviceName);
555 $service = $serviceInfo[
'service'];
562 if (!$service->isRunning()) {
563 Log::trace(
'Service failed to start: ' . $serviceName);
581 foreach ($serviceInfos as $serviceName => $serviceInfo) {
582 $service = $serviceInfo[
'service'];
583 if (!$service->isRunning()) {
static info($data, $file=null)
static trace($data, $file=null)
static startAllServicesParallel($serviceInfos, ?callable $progressCallback=null, $startupTimeout=30)
static getServicePort($serviceName, $bearsamppBins)
static shutdownServicesSequential($services, ?callable $progressCallback=null)
static forceKillService($serviceName)
static allServicesStopped($services)
static allServicesRunning($serviceInfos)
static startServicesParallel($serviceInfos, ?callable $progressCallback=null, $startupTimeout=30)
static getServiceDisplayName($bin, $service)
static hasSyntaxCheck($serviceName)
static stopAllServicesParallel($bearsamppBins, ?callable $progressCallback=null, $shutdownTimeout=15)
static shutdownServicesParallel($services, ?callable $progressCallback=null, $shutdownTimeout=15)
static stopService($service)
static restartService($service)
static getSyntaxCheckCmd($serviceName, $bearsamppBins=null)
static startService($bin, $syntaxCheckCmd=null, $showErrors=true)
static startServicesSequential($serviceInfos, ?callable $progressCallback=null)
static getAllServiceNames()
static processServices($bearsamppBins, callable $callback)
static getBinFromServiceName($serviceName, $bearsamppBins)
static initializeMappings($bearsamppBins)
static startService($bin, $syntaxCheckCmd, $showWindow=false)
static killBins($refreshProcs=false)