Skip to content

Commit

Permalink
Introduce JobsCollector (#1470)
Browse files Browse the repository at this point in the history
* Introduce JobsCollector

* Tweak config

---------

Co-authored-by: Barry vd. Heuvel <[email protected]>
  • Loading branch information
julienbourdeau and barryvdh authored Oct 23, 2023
1 parent b736241 commit 6931d7a
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
'cache' => false, // Display cache events
'models' => true, // Display models
'livewire' => true, // Display Livewire (when available)
'jobs' => false, // Display dispatched jobs
],

/*
Expand Down
60 changes: 60 additions & 0 deletions src/DataCollector/JobsCollector.php
Original file line number Diff line number Diff line change
@@ -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
]
];
}
}
11 changes: 10 additions & 1 deletion src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function (\Illuminate\Database\Events\QueryExecuted $query) use ($db, $queryColl
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;
Expand Down Expand Up @@ -557,6 +557,15 @@ public function __toString(): string{ $this->originalTransport->__toString(); }
}
}

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));
Expand Down
77 changes: 77 additions & 0 deletions tests/DataCollector/JobsCollectorTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
23 changes: 23 additions & 0 deletions tests/Jobs/OrderShipped.php
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 16 additions & 0 deletions tests/Jobs/SendNotification.php
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 6931d7a

Please sign in to comment.