Introduce JobsCollector (#1470)
* Introduce JobsCollector * Tweak config --------- Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
This commit is contained in:
parent
b736241043
commit
6931d7a4f2
|
@ -176,6 +176,7 @@ return [
|
|||
'cache' => false, // Display cache events
|
||||
'models' => true, // Display models
|
||||
'livewire' => true, // Display Livewire (when available)
|
||||
'jobs' => false, // Display dispatched jobs
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Barryvdh\Debugbar\DataCollector;
|
||||
|
||||
use DebugBar\DataCollector\DataCollector;
|
||||
use DebugBar\DataCollector\DataCollectorInterface;
|
||||
use DebugBar\DataCollector\Renderable;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class JobsCollector extends DataCollector implements DataCollectorInterface, Renderable
|
||||
{
|
||||
public $jobs = [];
|
||||
public $count = 0;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function __construct(Dispatcher $events)
|
||||
{
|
||||
$events->listen(\Illuminate\Queue\Events\JobQueued::class, function ($event) {
|
||||
$class = get_class($event->job);
|
||||
$this->jobs[$class] = ($this->jobs[$class] ?? 0) + 1;
|
||||
$this->count++;
|
||||
});
|
||||
}
|
||||
|
||||
public function collect()
|
||||
{
|
||||
ksort($this->jobs, SORT_NUMERIC);
|
||||
|
||||
return ['data' => array_reverse($this->jobs), 'count' => $this->count];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'jobs';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getWidgets()
|
||||
{
|
||||
return [
|
||||
"jobs" => [
|
||||
"icon" => "briefcase",
|
||||
"widget" => "PhpDebugBar.Widgets.HtmlVariableListWidget",
|
||||
"map" => "jobs.data",
|
||||
"default" => "{}"
|
||||
],
|
||||
'jobs:badge' => [
|
||||
'map' => 'jobs.count',
|
||||
'default' => 0
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
|
@ -348,7 +348,7 @@ class LaravelDebugbar extends DebugBar
|
|||
if (!app(static::class)->shouldCollect('db', true)) {
|
||||
return; // Issue 776 : We've turned off collecting after the listener was attached
|
||||
}
|
||||
|
||||
|
||||
$bindings = $query->bindings;
|
||||
$time = $query->time;
|
||||
$connection = $query->connection;
|
||||
|
@ -557,6 +557,15 @@ class LaravelDebugbar extends DebugBar
|
|||
}
|
||||
}
|
||||
|
||||
if ($this->shouldCollect('jobs', false)) {
|
||||
try {
|
||||
$jobsCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\JobsCollector');
|
||||
$this->addCollector($jobsCollector);
|
||||
} catch (\Exception $e) {
|
||||
// No Jobs collector
|
||||
}
|
||||
}
|
||||
|
||||
$renderer = $this->getJavascriptRenderer();
|
||||
$renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
|
||||
$renderer->setBindAjaxHandlerToFetch($app['config']->get('debugbar.capture_ajax', true));
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Barryvdh\Debugbar\Tests\DataCollector;
|
||||
|
||||
use Barryvdh\Debugbar\Tests\Jobs\OrderShipped;
|
||||
use Barryvdh\Debugbar\Tests\Jobs\SendNotification;
|
||||
use Barryvdh\Debugbar\Tests\TestCase;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class JobsCollectorTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
$app['config']->set('debugbar.collectors.jobs', true);
|
||||
// The `sync` and `null` driver don't dispatch events
|
||||
// `database` or `redis` driver work great
|
||||
$app['config']->set('queue.default', 'database');
|
||||
|
||||
parent::getEnvironmentSetUp($app);
|
||||
}
|
||||
|
||||
public function testItCollectsDispatchedJobs()
|
||||
{
|
||||
$this->loadLaravelMigrations();
|
||||
$this->createJobsTable();
|
||||
|
||||
debugbar()->boot();
|
||||
|
||||
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
|
||||
$collector = debugbar()->getCollector('jobs');
|
||||
|
||||
$this->assertEquals(
|
||||
['data' => [], 'count' => 0],
|
||||
$collector->collect()
|
||||
);
|
||||
|
||||
OrderShipped::dispatch(1);
|
||||
|
||||
$this->assertEquals(
|
||||
['data' => [OrderShipped::class => 1], 'count' => 1],
|
||||
$collector->collect()
|
||||
);
|
||||
|
||||
dispatch(new SendNotification());
|
||||
dispatch(new SendNotification());
|
||||
dispatch(new SendNotification());
|
||||
|
||||
$this->assertEquals(
|
||||
['data' => [OrderShipped::class => 1, SendNotification::class => 3], 'count' => 4],
|
||||
$collector->collect()
|
||||
);
|
||||
}
|
||||
|
||||
protected function createJobsTable()
|
||||
{
|
||||
(new class extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
Schema::create('jobs', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('queue')->index();
|
||||
$table->longText('payload');
|
||||
$table->unsignedTinyInteger('attempts');
|
||||
$table->unsignedInteger('reserved_at')->nullable();
|
||||
$table->unsignedInteger('available_at');
|
||||
$table->unsignedInteger('created_at');
|
||||
});
|
||||
}
|
||||
})->up();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Barryvdh\Debugbar\Tests\Jobs;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class OrderShipped implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
private $orderId;
|
||||
|
||||
public function __construct($orderId)
|
||||
{
|
||||
$this->orderId = $orderId;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// Do Nothing
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Barryvdh\Debugbar\Tests\Jobs;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class SendNotification implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// Do Nothing
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue