Skip to content

Commit

Permalink
Merge pull request #5 from chocofamilyme/job.profiling
Browse files Browse the repository at this point in the history
Job.profiling
  • Loading branch information
Vadim89 authored Jan 5, 2021
2 parents d6a3820 + 6159740 commit d8edb82
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.2.0
- Added profiling jobs

## 2.1.0
- Added profiling of console commands

Expand Down
76 changes: 41 additions & 35 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
{
"name": "chocofamilyme/laravel-pinba",
"description": "Integrates pinba with Laravel",
"keywords": ["laravel", "pinba", "profiler", "debug"],
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "IT Chocolife.me",
"homepage": "https://chocolife.me"
}
],
"require": {
"php": ">= 7.4.0",
"illuminate/log": "^8.0",
"illuminate/http": "^8.0",
"illuminate/routing": "^8.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.3"
},
"autoload": {
"psr-4": {
"Chocofamilyme\\LaravelPinba\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Chocofamilyme\\LaravelPinba\\Providers\\PinbaServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
"name": "chocofamilyme/laravel-pinba",
"description": "Integrates pinba with Laravel",
"keywords": [
"laravel",
"pinba",
"profiler",
"debug"
],
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "IT Chocolife.me",
"homepage": "https://chocolife.me"
}
],
"require": {
"php": ">= 7.4.0",
"illuminate/log": "^8.0",
"illuminate/http": "^8.0",
"illuminate/queue": "^8.0",
"illuminate/routing": "^8.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.3"
},
"autoload": {
"psr-4": {
"Chocofamilyme\\LaravelPinba\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Chocofamilyme\\LaravelPinba\\Providers\\PinbaServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
49 changes: 49 additions & 0 deletions src/Listeners/ProfileJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Chocofamilyme\LaravelPinba\Listeners;

use Chocofamilyme\LaravelPinba\Profiler\ProfilerInterface;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobFailed;

final class ProfileJob
{
private ProfilerInterface $profiler;
private static int $timerId;

public function __construct(ProfilerInterface $profiler)
{
$this->profiler = $profiler;
}

public function handle($event)
{
if ($event instanceof JobProcessing) {
self::$timerId = $this->profiler->startTimer(
'worker',
'run',
$event->job->getName(),
'messenger:consume'
);

return;
}

if ($event instanceof JobProcessed) {
$this->profiler->stopTimer(self::$timerId);
$this->profiler->flush($event->job->resolveName());

return;
}

if ($event instanceof JobFailed) {
$this->profiler->stopTimer(self::$timerId);
$this->profiler->flush($event->job->resolveName());

return;
}
}
}
16 changes: 16 additions & 0 deletions src/Providers/PinbaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Chocofamilyme\LaravelPinba\Providers;

use Chocofamilyme\LaravelPinba\Listeners\ProfileJob;
use Chocofamilyme\LaravelPinba\Listeners\ProfileStartCommand;
use Chocofamilyme\LaravelPinba\Middlewares\RightUrlMiddleware;
use Chocofamilyme\LaravelPinba\Profiler\FileDestination;
Expand Down Expand Up @@ -109,6 +110,21 @@ private function initListeners(): void
\Illuminate\Console\Events\CommandStarting::class,
ProfileStartCommand::class
);

Event::listen(
\Illuminate\Queue\Events\JobProcessing::class,
ProfileJob::class
);

Event::listen(
\Illuminate\Queue\Events\JobProcessed::class,
ProfileJob::class
);

Event::listen(
\Illuminate\Queue\Events\JobFailed::class,
ProfileJob::class
);
}

/** @psalm-suppress UndefinedDocblockClass */
Expand Down

0 comments on commit d8edb82

Please sign in to comment.