Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit c4ffdee

Browse files
unknownunknown
unknown
authored and
unknown
committed
Close log handle before restart
1 parent 5be509c commit c4ffdee

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Diff for: Core/Daemon.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ abstract class Core_Daemon
128128
'parent' => true,
129129
);
130130

131+
/**
132+
* Handle for log() method,
133+
* @see Core_Daemon::log()
134+
* @see Core_Daemon::restart();
135+
* @var stream
136+
*/
137+
private static $log_handle = false;
131138

132139
/**
133140
* Implement this method to define plugins
@@ -615,8 +622,7 @@ public function task($task)
615622
* @param string $label Truncated at 12 chars
616623
*/
617624
public function log($message, $label = '', $indent = 0)
618-
{
619-
static $handle = false;
625+
{
620626
static $log_file = '';
621627
static $log_file_check_at = 0;
622628
static $log_file_error = false;
@@ -630,14 +636,14 @@ public function log($message, $label = '', $indent = 0)
630636
if (time() >= $log_file_check_at && $this->log_file() != $log_file) {
631637
$log_file = $this->log_file();
632638
$log_file_check_at = mktime(date('H'), (date('i') - (date('i') % 5)) + 5, null);
633-
@fclose($handle);
634-
$handle = $log_file_error = false;
639+
@fclose(self::$log_handle);
640+
self::$log_handle = $log_file_error = false;
635641
}
636642

637-
if ($handle === false) {
638-
if (strlen($log_file) > 0 && $handle = @fopen($log_file, 'a+')) {
643+
if (self::$log_handle === false) {
644+
if (strlen($log_file) > 0 && self::$log_handle = @fopen($log_file, 'a+')) {
639645
if ($this->is('parent')) {
640-
fwrite($handle, $header);
646+
fwrite(self::$log_handle, $header);
641647
if ($this->is('stdout'))
642648
echo $header;
643649
}
@@ -649,8 +655,8 @@ public function log($message, $label = '', $indent = 0)
649655

650656
$message = $prefix . ' ' . str_replace("\n", "\n$prefix ", trim($message)) . "\n";
651657

652-
if ($handle)
653-
fwrite($handle, $message);
658+
if (self::$log_handle)
659+
fwrite(self::$log_handle, $message);
654660

655661
if ($this->is('stdout'))
656662
echo $message;
@@ -927,6 +933,8 @@ public function restart()
927933
if (is_resource(STDOUT)) fclose(STDOUT);
928934
if (is_resource(STDERR)) fclose(STDERR);
929935
if (is_resource(STDIN)) fclose(STDIN);
936+
// Close the static log handle to prevent it being inherrited by the new process.
937+
if (is_resource(self::$log_handle)) fclose(self::$log_handle);
930938
exec($this->command());
931939

932940
// A new daemon process has been created. This one will stick around just long enough to clean up the worker processes.

0 commit comments

Comments
 (0)