enable foreground mode for services (#7061)

Authored-by: Tim Fry <tim@fusionpbx.com>
This commit is contained in:
frytimo 2024-07-23 10:40:49 -03:00 committed by GitHub
parent 2b3381f95c
commit f80347504a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 13 deletions

View File

@ -78,6 +78,12 @@ abstract class service {
*/ */
protected static $config_file = ""; protected static $config_file = "";
/**
* Fork the service to it's own process ID
* @var bool
*/
protected static $forking_enabled = true;
/** /**
* Child classes must provide a mechanism to reload settings * Child classes must provide a mechanism to reload settings
*/ */
@ -199,6 +205,12 @@ abstract class service {
* @return void * @return void
*/ */
protected static function parse_service_command_options(): void { protected static function parse_service_command_options(): void {
//ensure we have a PID so that reload and exit send commands work
if (empty(self::$pid_file)) {
self::$pid_file = self::get_pid_filename();
}
//base class short options //base class short options
self::$available_command_options = self::base_command_options(); self::$available_command_options = self::base_command_options();
@ -402,8 +414,13 @@ abstract class service {
$level = self::$log_level; $level = self::$log_level;
} }
//enable sending message to the console directly
if (self::$log_level === LOG_DEBUG || !self::$forking_enabled) {
echo $message . "\n";
}
// Log the message to syslog // Log the message to syslog
syslog($level, 'fusionpbx[' . posix_getpid() . ']: ['.self::class.'] '.$message); syslog($level, 'fusionpbx[' . posix_getpid() . ']: ['.static::class.'] '.$message);
} }
/** /**
@ -583,6 +600,13 @@ abstract class service {
$help_options[$index]['long_description'] = '--config <path>'; $help_options[$index]['long_description'] = '--config <path>';
$help_options[$index]['functions'][] = 'set_config_file'; $help_options[$index]['functions'][] = 'set_config_file';
$index++; $index++;
$help_options[$index]['short_option'] = '1';
$help_options[$index]['long_option'] = 'no-fork';
$help_options[$index]['description'] = 'Do not fork the process';
$help_options[$index]['short_description'] = '-1';
$help_options[$index]['long_description'] = '--no-fork';
$help_options[$index]['functions'][] = 'set_no_fork';
$index++;
$help_options[$index]['short_option'] = 'x'; $help_options[$index]['short_option'] = 'x';
$help_options[$index]['long_option'] = 'exit'; $help_options[$index]['long_option'] = 'exit';
$help_options[$index]['description'] = 'Exit the service gracefully'; $help_options[$index]['description'] = 'Exit the service gracefully';
@ -593,6 +617,14 @@ abstract class service {
return $help_options; return $help_options;
} }
/**
* Set to not fork when started
*/
public static function set_no_fork() {
echo "Running in forground\n";
self::$forking_enabled = false;
}
/** /**
* Set the configuration file location to use for a config object * Set the configuration file location to use for a config object
*/ */
@ -704,17 +736,21 @@ abstract class service {
//can only start from command line //can only start from command line
defined('STDIN') or die('Unauthorized'); defined('STDIN') or die('Unauthorized');
//force launching in a seperate process //parse the cli options and store them statically
if ($pid = pcntl_fork()) { self::parse_service_command_options();
exit;
}
if ($cid = pcntl_fork()) { //fork process
exit; if (self::$forking_enabled) {
} echo "Running in daemon mode\n";
//force launching in a seperate process
if ($pid = pcntl_fork()) {
exit;
}
//set the PID file we will use if ($cid = pcntl_fork()) {
self::$pid_file = self::get_pid_filename(); exit;
}
}
//TODO remove updated settings object after merge //TODO remove updated settings object after merge
if (file_exists( __DIR__ . '/settings.php')) { if (file_exists( __DIR__ . '/settings.php')) {
@ -726,9 +762,6 @@ abstract class service {
require_once dirname(__DIR__).'/functions.php'; require_once dirname(__DIR__).'/functions.php';
} }
//parse the cli options and store them statically
self::parse_service_command_options();
//create the config object if not already created //create the config object if not already created
if (self::$config === null) { if (self::$config === null) {
self::$config = new config(self::$config_file); self::$config = new config(self::$config_file);