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
2 changes: 1 addition & 1 deletion src/Laritor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Laritor
{
public const VERSION = '3.0.1';
public const VERSION = '3.0.2';

/**
* @var array
Expand Down
44 changes: 38 additions & 6 deletions src/Recorders/QueuedJobRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function queued(JobQueued $event)
'connection' => $event->connectionName,
'queue' => $event->job->queue ?? config("queue.connections.{$event->connectionName}.queue", 'default'),
'job' => isset($jobPayload['displayName']) ? $jobPayload['displayName'] : get_class($event->job),
'id' => $event->id,
'id' => $this->resolveJobId($event),
'delay' => isset($event->delay) ? $event->delay : ( isset($jobPayload['delay']) ? $jobPayload['delay'] : 0 ),
'queued_at' => now()->toDateTimeString(),
'status' => 'queued',
Expand All @@ -83,7 +83,7 @@ public function processing(JobProcessing $event)
$jobs = [];
$jobExists = false;
foreach ($this->laritor->getEvents(static::$eventType) as $job) {
if (isset($job['id']) && $job['id'] === $event->job->getJobId()) {
if (isset($job['id']) && $job['id'] === $this->resolveJobId($event)) {
$jobExists = true;
$job['started_at'] = now()->toDateTimeString();
$job['completed_at'] = null;
Expand All @@ -94,16 +94,22 @@ public function processing(JobProcessing $event)
}

if (!$jobExists) {
$jobs[] = [
$job = [
'connection' => $event->connectionName,
'queue' => $event->job->getQueue() ?? config("queue.connections.{$event->connectionName}.queue", 'default'),
'job' => isset($event->job->payload()['displayName']) ? $event->job->payload()['displayName'] : get_class($event->job),
'started_at' => now()->toDateTimeString(),
'completed_at' => null,
'duration' => 0,
'status' => 'processing',
'id' => $event->job->getJobId()
'id' => $this->resolveJobId($event)
];

if ($event->connectionName === 'sync') {
$job['queued_at'] = now()->toDateTimeString();
}

$jobs[] = $job;
}

$this->laritor->setContext('JOB');
Expand All @@ -118,12 +124,11 @@ public function complete($event)
{
$jobs = [];
foreach ($this->laritor->getEvents(static::$eventType) as $job) {
if (isset($job['id']) && $job['id'] === $event->job->getJobId()) {
if (isset($job['id']) && $job['id'] === $this->resolveJobId($event)) {
$start = Carbon::parse($job['started_at']);
$job['duration'] = $start->diffInMilliseconds();
$job['started_at'] = $start->toDateTimeString();
$job['completed_at'] = now()->toDateTimeString();
$job['id'] = $event->job->getJobId();
$job['status'] = $event instanceof JobExceptionOccurred ? 'failed' : 'processed';
$job['custom_context'] = DataHelper::getRedactedContext();
}
Expand All @@ -137,4 +142,31 @@ public function complete($event)
$this->laritor->sendEvents();
}
}

/**
* @param JobQueued|JobProcessing|JobProcessed|JobExceptionOccurred $event
* @return mixed
*/
private function resolveJobId($event)
{
try{
if ($event instanceof JobQueued) {
$jobPayload = [];
/** @phpstan-ignore-next-line */
if (method_exists($event, 'payload')) {
$jobPayload = $event->payload();
}
} else {
$jobPayload = $event->job->payload();
}
} catch (\Exception $e) {
$jobPayload = [];
}

if (isset($jobPayload['uuid'])) {
return $jobPayload['uuid'];
}

return $event instanceof JobQueued ? $event->id : $event->job->getJobId();
}
}
Loading