Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/Laritor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,7 +13,7 @@

class Laritor
{
public const VERSION = '2.3.9';
public const VERSION = '2.3.10';

/**
* @var array
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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')));

Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Override/TestOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function recordRequest($request): bool

return true;
}

public function recordException($exception): bool
{
return !request()->is('laritor-failed-job');
}
}
3 changes: 1 addition & 2 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
17 changes: 11 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down