Bearsampp
2026.7.11
Toggle main menu visibility
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
15
class
ActionRestartAllServices
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(
48
$bearsamppLang
->getValue(
Lang::MENU_RESTART_SERVICES
),
49
self::GAUGE_PER_SERVICE * $enabledServicesCount + 1,
50
$bearsamppLang
->getValue(
Lang::LOADING_RESTART_SERVICES
)
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];
147
$bin =
ServiceHelper::getBinFromServiceName
($serviceName,
$bearsamppBins
);
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
156
ServiceHelper::stopService
($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];
171
$bin =
ServiceHelper::getBinFromServiceName
($serviceName,
$bearsamppBins
);
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
}
195
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
exit
exit
Definition
ajax.apply.moduleconfig.php:63
$bearsamppCore
global $bearsamppCore
Definition
ajax.latestversion.php:24
ActionRestartAllServices
Definition
class.action.restartAllServices.php:16
ActionRestartAllServices\getServiceStartupOrder
getServiceStartupOrder()
Definition
class.action.restartAllServices.php:93
ActionRestartAllServices\getServiceShutdownOrder
getServiceShutdownOrder()
Definition
class.action.restartAllServices.php:66
ActionRestartAllServices\__construct
__construct($args)
Definition
class.action.restartAllServices.php:38
ActionRestartAllServices\$splash
$splash
Definition
class.action.restartAllServices.php:20
ActionRestartAllServices\$processed
$processed
Definition
class.action.restartAllServices.php:25
ActionRestartAllServices\processWindow
processWindow($window, $id, $ctrl, $param1, $param2)
Definition
class.action.restartAllServices.php:123
ActionRestartAllServices\GAUGE_PER_SERVICE
const GAUGE_PER_SERVICE
Definition
class.action.restartAllServices.php:30
BinApache\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.apache.php:19
BinMailpit\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.mailpit.php:19
BinMariadb\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.mariadb.php:18
BinMemcached\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.memcached.php:18
BinMysql\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.mysql.php:18
BinPostgresql\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.postgresql.php:18
BinXlight\SERVICE_NAME
const SERVICE_NAME
Definition
class.bin.xlight.php:19
Lang\MENU_RESTART_SERVICES
const MENU_RESTART_SERVICES
Definition
class.lang.php:112
Lang\LOADING_COMPLETE
const LOADING_COMPLETE
Definition
class.lang.php:267
Lang\LOADING_START_SERVICE
const LOADING_START_SERVICE
Definition
class.lang.php:263
Lang\LOADING_STOP_SERVICE
const LOADING_STOP_SERVICE
Definition
class.lang.php:265
Lang\LOADING_RESTART_SERVICES
const LOADING_RESTART_SERVICES
Definition
class.lang.php:266
ServiceHelper\getServiceDisplayName
static getServiceDisplayName($bin, $service)
Definition
class.servicehelper.php:95
ServiceHelper\stopService
static stopService($service)
Definition
class.servicehelper.php:140
ServiceHelper\getSyntaxCheckCmd
static getSyntaxCheckCmd($serviceName, $bearsamppBins=null)
Definition
class.servicehelper.php:80
ServiceHelper\startService
static startService($bin, $syntaxCheckCmd=null, $showErrors=true)
Definition
class.servicehelper.php:129
ServiceHelper\getBinFromServiceName
static getBinFromServiceName($serviceName, $bearsamppBins)
Definition
class.servicehelper.php:67
Splash
Definition
class.splash.php:17
sandbox
core
classes
actions
class.action.restartAllServices.php
Generated by
1.17.0