Bearsampp 2026.5.5
Loading...
Searching...
No Matches
class.action.restartAllServices.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
16{
20 private $splash;
21
25 private $processed = false;
26
30 const GAUGE_PER_SERVICE = 2; // 1 for stop, 1 for start
31
38 public function __construct($args)
39 {
40 global $bearsamppCore, $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
41
42 // Count enabled services for progress bar
43 $enabledServicesCount = count($bearsamppBins->getServices());
44
45 // Start splash screen (2 operations per service: stop + start)
46 $this->splash = new Splash();
47 $this->splash->init(
49 self::GAUGE_PER_SERVICE * $enabledServicesCount + 1,
51 );
52
53 // Set handler for the splash screen window with 1000ms timeout
54 $bearsamppWinbinder->setHandler($this->splash->getWbWindow(), $this, 'processWindow', 1000);
55 $bearsamppWinbinder->mainLoop();
56 $bearsamppWinbinder->reset();
57 }
58
66 private function getServiceShutdownOrder()
67 {
68 // Define shutdown order: dependent services first, then core services
69 // This prevents connection errors and ensures clean shutdown
70 return [
71 // Tier 1: Application services (no dependencies on other services)
72 BinMailpit::SERVICE_NAME, // Mail testing tool
73 BinMemcached::SERVICE_NAME, // Caching service
74 BinXlight::SERVICE_NAME, // FTP server
75
76 // Tier 2: Database services (web server depends on these)
77 BinPostgresql::SERVICE_NAME, // PostgreSQL database
78 BinMariadb::SERVICE_NAME, // MariaDB database
79 BinMysql::SERVICE_NAME, // MySQL database
80
81 // Tier 3: Web server (depends on databases and other services)
82 BinApache::SERVICE_NAME, // Apache web server (stopped last)
83 ];
84 }
85
93 private function getServiceStartupOrder()
94 {
95 // Define startup order: core services first, then dependent services
96 // This ensures dependencies are available when needed
97 return [
98 // Tier 1: Web server (should start first as it's the foundation)
99 BinApache::SERVICE_NAME, // Apache web server (started first)
100
101 // Tier 2: Database services (web server may depend on these)
102 BinMysql::SERVICE_NAME, // MySQL database
103 BinMariadb::SERVICE_NAME, // MariaDB database
104 BinPostgresql::SERVICE_NAME, // PostgreSQL database
105
106 // Tier 3: Application services (no dependencies on other services)
107 BinXlight::SERVICE_NAME, // FTP server
108 BinMemcached::SERVICE_NAME, // Caching service
109 BinMailpit::SERVICE_NAME, // Mail testing tool
110 ];
111 }
112
123 public function processWindow($window, $id, $ctrl, $param1, $param2)
124 {
125 global $bearsamppBins, $bearsamppLang, $bearsamppWinbinder;
126
127 // Only process once
128 if ($this->processed) {
129 return;
130 }
131 $this->processed = true;
132
133 // Get all available services
134 $allServices = $bearsamppBins->getServices();
135
136 // Get optimal shutdown order
137 $shutdownOrder = $this->getServiceShutdownOrder();
138
139 // First, stop all services in optimal shutdown order
140 foreach ($shutdownOrder as $serviceName) {
141 // Check if this service exists and is enabled
142 if (!isset($allServices[$serviceName])) {
143 continue;
144 }
145
146 $service = $allServices[$serviceName];
148
149 if ($bin !== null) {
150 $name = ServiceHelper::getServiceDisplayName($bin, $service);
151
152 $this->splash->incrProgressBar();
153 $this->splash->setTextLoading(sprintf($bearsamppLang->getValue(Lang::LOADING_STOP_SERVICE), $name));
154
155 // Stop the service
157 }
158 }
159
160 // Get optimal startup order
161 $startupOrder = $this->getServiceStartupOrder();
162
163 // Then, start all services in optimal startup order
164 foreach ($startupOrder as $serviceName) {
165 // Check if this service exists and is enabled
166 if (!isset($allServices[$serviceName])) {
167 continue;
168 }
169
170 $service = $allServices[$serviceName];
172 $syntaxCheckCmd = ServiceHelper::getSyntaxCheckCmd($serviceName);
173
174 if ($bin !== null) {
175 $name = ServiceHelper::getServiceDisplayName($bin, $service);
176
177 $this->splash->incrProgressBar();
178 $this->splash->setTextLoading(sprintf($bearsamppLang->getValue(Lang::LOADING_START_SERVICE), $name));
179
180 // Start the service
181 ServiceHelper::startService($bin, $syntaxCheckCmd, false);
182 }
183 }
184
185 // Final update
186 $this->splash->incrProgressBar();
187 $this->splash->setTextLoading($bearsamppLang->getValue(Lang::LOADING_COMPLETE));
188
189 // Close the splash screen and exit cleanly
190 $bearsamppWinbinder->destroyWindow($window);
191 $bearsamppWinbinder->reset();
192 exit(0);
193 }
194}
global $bearsamppBins
global $bearsamppLang
global $bearsamppCore
processWindow($window, $id, $ctrl, $param1, $param2)
const SERVICE_NAME
const MENU_RESTART_SERVICES
const LOADING_COMPLETE
const LOADING_START_SERVICE
const LOADING_STOP_SERVICE
const LOADING_RESTART_SERVICES
static getServiceDisplayName($bin, $service)
static stopService($service)
static getSyntaxCheckCmd($serviceName, $bearsamppBins=null)
static startService($bin, $syntaxCheckCmd=null, $showErrors=true)
static getBinFromServiceName($serviceName, $bearsamppBins)