-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Fixes jobs saying "pending" forever (#1349)
* Fixes "pending" job status * Apply fixes from StyleCI * Style * style * Test failed updates * Fixes contract * More tests * Fix type * Fixes tests * formatting * formatting --------- Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
cc5a219
commit 9bbfc8f
Showing
7 changed files
with
192 additions
and
7 deletions.
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,59 @@ | ||
<?php | ||
|
||
namespace Laravel\Telescope\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Laravel\Telescope\Contracts\EntriesRepository; | ||
|
||
class ProcessPendingUpdates implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* The pending entry updates. | ||
* | ||
* @var \Illuminate\Support\Collection<int, \Laravel\Telescope\EntryUpdate> | ||
*/ | ||
public $pendingUpdates; | ||
|
||
/** | ||
* The number of times the job has been attempted. | ||
* | ||
* @var int | ||
*/ | ||
public $attempt; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param \Illuminate\Support\Collection<int, \Laravel\Telescope\EntryUpdate> | ||
* @param int $attempt | ||
* @return void | ||
*/ | ||
public function __construct($pendingUpdates, $attempt = 0) | ||
{ | ||
$this->pendingUpdates = $pendingUpdates; | ||
$this->attempt = $attempt; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @param \Laravel\Telescope\Contracts\EntriesRepository $repository | ||
* @return void | ||
*/ | ||
public function handle(EntriesRepository $repository) | ||
{ | ||
$this->attempt++; | ||
|
||
$repository->update($this->pendingUpdates)->whenNotEmpty( | ||
fn ($pendingUpdates) => static::dispatchIf( | ||
$this->attempt < 3, $pendingUpdates, $this->attempt | ||
)->delay(now()->addSeconds(10)), | ||
); | ||
} | ||
} |
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
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,87 @@ | ||
<?php | ||
|
||
namespace Laravel\Telescope\Tests\Storage; | ||
|
||
use Illuminate\Support\Facades\Bus; | ||
use Laravel\Telescope\Contracts\EntriesRepository; | ||
use Laravel\Telescope\Jobs\ProcessPendingUpdates; | ||
use Laravel\Telescope\Tests\FeatureTestCase; | ||
use Mockery as m; | ||
|
||
class ProcessPendingUpdatesTest extends FeatureTestCase | ||
{ | ||
public function test_pending_updates() | ||
{ | ||
Bus::fake(); | ||
|
||
$pendingUpdates = collect([ | ||
['id' => 1, 'content' => 'foo'], | ||
['id' => 2, 'content' => 'bar'], | ||
]); | ||
|
||
$failedUpdates = collect(); | ||
$repository = m::mock(EntriesRepository::class); | ||
|
||
$repository | ||
->shouldReceive('update') | ||
->once() | ||
->with($pendingUpdates) | ||
->andReturn($failedUpdates); | ||
|
||
(new ProcessPendingUpdates($pendingUpdates))->handle($repository); | ||
|
||
Bus::assertNothingDispatched(); | ||
} | ||
|
||
public function test_pending_updates_may_stay_pending() | ||
{ | ||
Bus::fake(); | ||
|
||
$pendingUpdates = collect([ | ||
['id' => 1, 'content' => 'foo'], | ||
['id' => 2, 'content' => 'bar'], | ||
]); | ||
$failedUpdates = collect([ | ||
$pendingUpdates->get(1), | ||
]); | ||
|
||
$repository = m::mock(EntriesRepository::class); | ||
|
||
$repository | ||
->shouldReceive('update') | ||
->once() | ||
->with($pendingUpdates) | ||
->andReturn($failedUpdates); | ||
|
||
(new ProcessPendingUpdates($pendingUpdates))->handle($repository); | ||
|
||
Bus::assertDispatched(ProcessPendingUpdates::class, function ($job) { | ||
return $job->attempt == 1 && $job->pendingUpdates->toArray() == [['id' => 2, 'content' => 'bar']]; | ||
}); | ||
} | ||
|
||
public function test_pending_updates_may_stay_pending_only_three_times() | ||
{ | ||
Bus::fake(); | ||
|
||
$pendingUpdates = collect([ | ||
['id' => 1, 'content' => 'foo'], | ||
['id' => 2, 'content' => 'bar'], | ||
]); | ||
$failedUpdates = collect([ | ||
$pendingUpdates->get(1), | ||
]); | ||
|
||
$repository = m::mock(EntriesRepository::class); | ||
|
||
$repository | ||
->shouldReceive('update') | ||
->once() | ||
->with($pendingUpdates) | ||
->andReturn($failedUpdates); | ||
|
||
(new ProcessPendingUpdates($pendingUpdates, 2))->handle($repository); | ||
|
||
Bus::assertNothingDispatched(); | ||
} | ||
} |
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
9bbfc8f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this PR made new bugs, after updated Telescope, there are Tens of thousands
Laravel\Telescope\Jobs\ProcessPendingUpdates
jobs shows in my Horizon panel