diff --git a/src/Laritor.php b/src/Laritor.php index 3e3173b..9864661 100644 --- a/src/Laritor.php +++ b/src/Laritor.php @@ -2,6 +2,8 @@ namespace BinaryBuilds\LaritorClient; +use BinaryBuilds\LaritorClient\Recorders\LogRecorder; +use Carbon\Carbon; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Http; @@ -11,7 +13,7 @@ class Laritor { - public const VERSION = '2.3.9'; + public const VERSION = '2.3.10'; /** * @var array @@ -147,6 +149,17 @@ public function addEvents($name, $events) return $this; } + public function addCustomLog(string $type, string $level, string $message, array $context = [], ?Carbon $written_at = null) + { + $this->events[LogRecorder::$eventType][] = [ + 'level' => $level, + 'message' => $message, + 'log_context' => $context, + 'occurred_at' => $written_at ? $written_at->format('Y-m-d H:i:s') : now()->format('Y-m-d H:i:s'), + 'context' => $type + ]; + } + public function removeScheduler() { unset($this->events[SchedulerRecorder::$eventType]); @@ -300,6 +313,10 @@ public function sync($data) */ public function shouldSendEvents() { + if (empty($this->events)) { + return false; + } + try { $timeout = trim(file_get_contents(storage_path('laritor-timeout.txt'))); @@ -309,19 +326,6 @@ public function shouldSendEvents() } catch (\Throwable $exception) {} - $hasOccurrence = false; - - foreach ($this->events as $type => $event) { - if (in_array($type, ['requests', 'commands', 'scheduler', 'scheduled_tasks', 'jobs','server_stats'])) { - $hasOccurrence = true; - break; - } - } - - if (! $hasOccurrence) { - return false; - } - if (app()->runningInConsole() || ! $this->isRateLimiterEnabled() ) { return true; } diff --git a/src/Override/TestOverride.php b/src/Override/TestOverride.php index 5ed6d84..490ab3b 100644 --- a/src/Override/TestOverride.php +++ b/src/Override/TestOverride.php @@ -19,4 +19,9 @@ public function recordRequest($request): bool return true; } + + public function recordException($exception): bool + { + return !request()->is('laritor-failed-job'); + } } \ No newline at end of file diff --git a/tests/JobTest.php b/tests/JobTest.php index da810f8..84b45f2 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -29,11 +29,10 @@ public function test_it_records_failed_jobs(): void $this->assertFileExists($file); $data = json_decode(file_get_contents($file), true); + $this->assertIsArray($data, 'Payload is not valid JSON'); $this->assertArrayHasKey('events', $data); $this->assertArrayHasKey(QueuedJobRecorder::$eventType, $data['events']); $this->assertNotEmpty( $data['events'][QueuedJobRecorder::$eventType]); - $this->assertArrayHasKey(ExceptionRecorder::$eventType, $data['events']); - $this->assertNotEmpty( $data['events'][ExceptionRecorder::$eventType]); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4325fc3..23519e1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -128,12 +128,17 @@ public function toMail($notifiable) { }); $app['router']->get('/laritor-failed-job', function () { - dispatch(function (){ - /** - * @phpstan-ignore-next-line - */ - return $invalid; - }); + try{ + dispatch(function (){ + /** + * @phpstan-ignore-next-line + */ + return $invalid; + }); + } catch (\Throwable $e) { + return response('NOT OK', 500); + } + return response('OK', 200); }); }