-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce JobsCollector * Tweak config --------- Co-authored-by: Barry vd. Heuvel <[email protected]>
- Loading branch information
1 parent
b736241
commit 6931d7a
Showing
6 changed files
with
187 additions
and
1 deletion.
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
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,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 | ||
] | ||
]; | ||
} | ||
} |
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
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(); | ||
} | ||
} |
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,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 | ||
} | ||
} |
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,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 | ||
} | ||
} |