-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JobProcessing and JobProcessed events
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Honeybadger\HoneybadgerLaravel\Concerns; | ||
|
||
trait MeasuresDuration | ||
{ | ||
/** | ||
* @var array{string, float} | ||
*/ | ||
private array $durations = []; | ||
|
||
/** | ||
* Helper to measure the duration between blocks of code that span multiple files. | ||
* If the name already exists in the durations array, it will return the duration in milliseconds. | ||
* If it does not exist, a timer will be started for this name and null will be returned. | ||
*/ | ||
public function time(string $name): ?float { | ||
if (array_key_exists($name, $this->durations)) { | ||
$start = $this->durations[$name]; | ||
$end = microtime(true); | ||
$duration = ($end - $start) * 1000; | ||
unset($this->durations[$name]); | ||
return $duration; | ||
} else { | ||
$this->durations[$name] = microtime(true); | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Honeybadger\HoneybadgerLaravel\Events; | ||
|
||
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger; | ||
use Illuminate\Queue\Events\JobProcessed as LaravelJobProcessed; | ||
|
||
class JobProcessed | ||
{ | ||
public string $handles = LaravelJobProcessed::class; | ||
|
||
/** | ||
* @param LaravelJobProcessed $event | ||
* @return EventPayload | ||
*/ | ||
public function getEventPayload($event): EventPayload | ||
{ | ||
$jobClass = get_class($event->job); | ||
|
||
$metadata = [ | ||
'connectionName' => $event->connectionName, | ||
'queue' => $event->job->queue, | ||
'job' => get_class($event->job), | ||
'duration' => Honeybadger::time($jobClass) | ||
]; | ||
|
||
return new EventPayload( | ||
'job', | ||
'job.processed', | ||
'Job processed', | ||
$metadata, | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Honeybadger\HoneybadgerLaravel\Events; | ||
|
||
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger; | ||
use Illuminate\Queue\Events\JobProcessing as LaravelJobProcessing; | ||
|
||
class JobProcessing extends ApplicationEvent | ||
{ | ||
public string $handles = LaravelJobProcessing::class; | ||
|
||
/** | ||
* @param LaravelJobProcessing $event | ||
* @return EventPayload | ||
*/ | ||
public function getEventPayload($event): EventPayload | ||
{ | ||
$jobClass = get_class($event->job); | ||
|
||
Honeybadger::time($jobClass); | ||
|
||
$metadata = [ | ||
'connectionName' => $event->connectionName, | ||
'queue' => $event->job->queue, | ||
'job' => $jobClass, | ||
]; | ||
|
||
return new EventPayload( | ||
'job', | ||
'job.processing', | ||
'Job processing', | ||
$metadata, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters