From c91be53b6dd9111cb9a0c066ca85e5bc627b302b Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 11 Jul 2024 09:09:06 +0300 Subject: [PATCH 1/2] Add database queue data to the health check --- app/Utils/SystemHealth.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/Utils/SystemHealth.php b/app/Utils/SystemHealth.php index 96522a0427c5..7c2c60f328cc 100644 --- a/app/Utils/SystemHealth.php +++ b/app/Utils/SystemHealth.php @@ -78,9 +78,9 @@ class SystemHealth 'open_basedir' => (bool) self::checkOpenBaseDir(), 'mail_mailer' => (string) self::checkMailMailer(), 'flutter_renderer' => (string) config('ninja.flutter_canvas_kit'), - 'jobs_pending' => (int) self::checkQueueSize(), 'pdf_engine' => (string) self::getPdfEngine(), 'queue' => (string) config('queue.default'), + 'queue_data' => self::checkQueueData(), 'trailing_slash' => (bool) self::checkUrlState(), 'file_permissions' => (string) ($check_file_system ? self::checkFileSystem() : ''), 'exchange_rate_api_not_configured' => (bool)self::checkCurrencySanity(), @@ -117,16 +117,28 @@ class SystemHealth return false; } - private static function checkQueueSize() + private static function checkQueueData() { - $count = 0; + $pending = 0; + $failed = 0; + $last_error = ''; try { - $count = Queue::size(); + $pending = DB::table('jobs')->count(); + $failed = DB::table('failed_jobs')->count(); + + if ($failed > 0) { + $failed_job = DB::table('failed_jobs')->latest('failed_at')->first(); + $last_error = $failed_job->exception; + } } catch (\Exception $e) { } - return $count; + return [ + 'failed' => $failed, + 'pending' => $pending, + 'last_error' => $last_error, + ]; } public static function checkFileSystem() From 11bfb99b0a4496f53b75a822ee2699ffe7f2b32c Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 11 Jul 2024 09:19:16 +0300 Subject: [PATCH 2/2] Update code used to retrieve last error in the logs --- app/Utils/SystemHealth.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Utils/SystemHealth.php b/app/Utils/SystemHealth.php index 7c2c60f328cc..c415d36903d4 100644 --- a/app/Utils/SystemHealth.php +++ b/app/Utils/SystemHealth.php @@ -11,6 +11,8 @@ namespace App\Utils; +use LimitIterator; +use SplFileObject; use App\Libraries\MultiDB; use App\Mail\TestMailServer; use Exception; @@ -337,11 +339,12 @@ class SystemHealth public static function lastError() { - $filepath = storage_path('logs/laravel.log'); - $file = escapeshellarg($filepath); - $end_of_file = `tail -n 500 $file`; + $log_file = new SplFileObject(sprintf('%s/laravel.log', base_path('storage/logs'))); + $log_file->seek(PHP_INT_MAX); + $last_line = $log_file->key(); - $lines = explode("\n", $end_of_file); + $lines = new LimitIterator($log_file, max(0, $last_line - 500), $last_line); + $log_lines = iterator_to_array($lines); $last_error = ''; foreach ($lines as $line) {