Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Handle pushed jobs #1379

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
//
}