diff --git a/app/xml_cdr/resources/classes/xml_cdr_service.php b/app/xml_cdr/resources/classes/xml_cdr_service.php index 01da68143..c9b181771 100644 --- a/app/xml_cdr/resources/classes/xml_cdr_service.php +++ b/app/xml_cdr/resources/classes/xml_cdr_service.php @@ -5,6 +5,13 @@ */ class xml_cdr_service extends service { + /** + * Message to show when reloading the settings + * + * @var string + */ + private $message; + /** * database object * @var database @@ -49,10 +56,19 @@ class xml_cdr_service extends service { //get the xml_cdr directory $this->xml_cdr_dir = $this->settings->get('switch', 'log', '/var/log/freeswitch').'/xml_cdr'; + + // Show the message in the log so we can track when the settings are reloaded + $this->notice($this->message); + + // Set the message for the next reload + $this->message = "Settings reloaded"; } public function run(): int { + // Set the initial message + $this->message = "Settings loaded"; + // Reload the settings $this->reload_settings(); @@ -76,11 +92,6 @@ class xml_cdr_service extends service { mkdir($this->xml_cdr_dir.'/failed/sql', 0770, true); } - //update permissions to correct systems with the wrong permissions - if (file_exists($this->xml_cdr_dir.'/failed')) { - exec('chmod 770 -R '.$this->xml_cdr_dir.'/failed'); - } - //import the call detail records from HTTP POST or file system $cdr = new xml_cdr; @@ -144,7 +155,7 @@ class xml_cdr_service extends service { } //parse the xml and insert the data into the database - $cdr->xml_array($i, $leg, $call_details); + $cdr->xml_array(0, $leg, $call_details); } } } @@ -152,11 +163,13 @@ class xml_cdr_service extends service { //sleep for 100 ms usleep(100000); } + + // Return a successful exit code return 0; } protected static function display_version(): void { - echo "1.1\n"; + echo "XML CDR Service version 1.1\n"; } protected static function set_command_options() { diff --git a/resources/classes/service.php b/resources/classes/service.php index b2530e3cb..f2209795b 100644 --- a/resources/classes/service.php +++ b/resources/classes/service.php @@ -122,7 +122,7 @@ abstract class service { * calling another method from the constructor) will fail to register the signal handlers.

*/ protected function __construct() { - openlog('[php][' . self::class . ']', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); + openlog('[php][' . static::class . ']', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); } /** @@ -134,13 +134,15 @@ abstract class service { /** * Sends a shutdown signal to the running process. + * + * @return bool True if the signals were sent successfully, false if no service is running */ - public static function send_shutdown() { + public static function send_shutdown(): bool { if (self::is_any_running()) { - self::send_signal(SIGTERM); - } else { - die("Service Not Started\n"); + return self::send_signal(15); // SIGTERM } + + return false; } /** @@ -166,30 +168,17 @@ abstract class service { * 'service' command as root user. It is possible to start the * service with user www-data and then the web UI would in fact * be able to send the reload signal to the running service.

+ * + * @param int $posix_signal The POSIX signal to send. + * + * @return bool True if the signal was sent successfully, false otherwise. */ - public static function send_signal($posix_signal) { - $signal_name = ""; - switch ($posix_signal) { - case 1: //SIGHUP - case 10: //SIGUSR1 - $signal_name = "Reload"; - break; - case 12: //SIGUSR2 - case 15: //SIGTERM - $signal_name = "Shutdown"; - break; - } + public static function send_signal(int $posix_signal): bool { $pid = self::get_service_pid(); - if ($pid === false) { - self::log("service not running", LOG_EMERG); - } else { - if (posix_kill((int)$pid, $posix_signal)) { - echo "Sent $signal_name\n"; - } else { - $err = posix_strerror(posix_get_last_error()); - echo "Failed to send $signal_name: $err\n"; - } + if ($pid !== false && posix_kill((int)$pid, (int)$posix_signal)) { + return true; } + return false; } /** @@ -227,15 +216,13 @@ abstract class service { /** * Sends a reload signal to running services, or exits if no service is running. * - * @return void + * @return bool True if the reload signal was sent successfully, false if no service is running */ - public static function send_reload() { + public static function send_reload(): bool { if (self::is_any_running()) { - self::send_signal(10); - } else { - die("Service Not Started\n"); + return self::send_signal(10); // SIGUSR1 } - exit(); + return false; } /** @@ -452,10 +439,6 @@ abstract class service { } } - // - // - // - /** * Returns an array of base command options. * @@ -505,6 +488,7 @@ abstract class service { $help_options[$index]['short_description'] = '-r'; $help_options[$index]['long_description'] = '--reload'; $help_options[$index]['functions'][] = 'send_reload'; + $help_options[$index]['functions'][] = 'shutdown'; $index++; $help_options[$index]['short_option'] = 'd:'; $help_options[$index]['long_option'] = 'debug:';