Skip to content

Commit

Permalink
[4.x] Handle pushed jobs (#1379)
Browse files Browse the repository at this point in the history
* Handle pushed jobs

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
timacdonald and StyleCIBot authored Aug 28, 2023
1 parent 7102145 commit aa7f7cb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Watchers/JobWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public function recordJob($connection, $queue, array $payload)
return;
}

if (in_array(get_class($payload['data']['command']), $this->ignoredJobClasses)) {
$job = isset($payload['data']['command'])
? get_class($payload['data']['command'])
: $payload['job'];

if (in_array($job, $this->ignoredJobClasses)) {
return;
}

Expand Down Expand Up @@ -198,6 +202,10 @@ protected function tags(array $payload)
*/
protected function updateBatch($payload)
{
if (! isset($payload['data']['command'])) {
return;
}

$wasRecordingEnabled = Telescope::$shouldRecord;

Telescope::$shouldRecord = false;
Expand Down
46 changes: 46 additions & 0 deletions tests/Watchers/JobWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use Exception;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Laravel\Telescope\EntryType;
use Laravel\Telescope\Tests\FeatureTestCase;
use Laravel\Telescope\Watchers\JobWatcher;
use Throwable;

class JobWatcherTest extends FeatureTestCase
{
Expand Down Expand Up @@ -100,6 +104,32 @@ public function test_failed_jobs_register_entry()
$this->assertSame('handle', $entry->content['exception']['trace'][0]['function']);
}

public function test_it_handles_pushed_jobs()
{
$queueExceptions = [];
$this->app[ExceptionHandler::class]->reportable(function (Throwable $e) use (&$queueExceptions) {
$queueExceptions[] = $e;
});

$this->app[QueueManager::class]
->connection('database')
->push(MyPushedJobClass::class, ['framework' => 'Laravel']);
$this->artisan('queue:work', [
'connection' => 'database',
'--once' => true,
]);

$entry = $this->loadTelescopeEntries()->first();
$this->assertCount(1, $queueExceptions);
$this->assertInstanceOf(PushedJobFailedException::class, $queueExceptions[0]);
$this->assertSame(EntryType::JOB, $entry->type);
$this->assertSame('failed', $entry->content['status']);
$this->assertSame('database', $entry->content['connection']);
$this->assertSame(MyPushedJobClass::class, $entry->content['name']);
$this->assertSame('default', $entry->content['queue']);
$this->assertSame(['framework' => 'Laravel'], $entry->content['data']);
}

private function createJobsTable(): void
{
Schema::create('jobs', function (Blueprint $table) {
Expand All @@ -113,6 +143,7 @@ private function createJobsTable(): void
});

Schema::create('failed_jobs', function (Blueprint $table) {
$table->uuid('uuid');
$table->bigIncrements('id');
$table->text('connection');
$table->text('queue');
Expand Down Expand Up @@ -179,3 +210,18 @@ public function handle()
throw new Exception($this->message);
}
}

class MyPushedJobClass
{
public $tries = 1;

public function fire(Job $job, array $data)
{
throw new PushedJobFailedException();
}
}

class PushedJobFailedException extends Exception
{
//
}

0 comments on commit aa7f7cb

Please sign in to comment.