-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix translation bug and add test * refactor unique job queueing determination * Apply fixes from StyleCI Co-authored-by: Taylor Otwell <taylorotwell@users.noreply.github.com>
- Loading branch information
1 parent
cca0c5f
commit 4b785c6
Showing
4 changed files
with
146 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Illuminate\Bus; | ||
|
||
use Illuminate\Contracts\Cache\Repository as Cache; | ||
|
||
class UniqueLock | ||
{ | ||
/** | ||
* The cache repository implementation. | ||
* | ||
* @var \Illuminate\Contracts\Cache\Repository | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* Create a new unique lock manager instance. | ||
* | ||
* @param \Illuminate\Contracts\Cache\Repository $cache | ||
* @return void | ||
*/ | ||
public function __construct(Cache $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* Attempt to acquire a lock for the given job. | ||
* | ||
* @param mixed $job | ||
* @return bool | ||
*/ | ||
public function acquire($job) | ||
{ | ||
$uniqueId = method_exists($job, 'uniqueId') | ||
? $job->uniqueId() | ||
: ($job->uniqueId ?? ''); | ||
|
||
$cache = method_exists($job, 'uniqueVia') | ||
? $job->uniqueVia() | ||
: $this->cache; | ||
|
||
return (bool) $cache->lock( | ||
$key = 'laravel_unique_job:'.get_class($job).$uniqueId, | ||
$job->uniqueFor ?? 0 | ||
)->get(); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Console; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Console\Scheduling\Schedule; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Facades\Queue; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class UniqueJobSchedulingTest extends TestCase | ||
{ | ||
public function testJobsPushedToQueue() | ||
{ | ||
Queue::fake(); | ||
$this->dispatch( | ||
TestJob::class, | ||
TestJob::class, | ||
TestJob::class, | ||
TestJob::class | ||
); | ||
|
||
Queue::assertPushed(TestJob::class, 4); | ||
} | ||
|
||
public function testUniqueJobsPushedToQueue() | ||
{ | ||
Queue::fake(); | ||
$this->dispatch( | ||
UniqueTestJob::class, | ||
UniqueTestJob::class, | ||
UniqueTestJob::class, | ||
UniqueTestJob::class | ||
); | ||
|
||
Queue::assertPushed(UniqueTestJob::class, 1); | ||
} | ||
|
||
private function dispatch(...$jobs) | ||
{ | ||
/** @var \Illuminate\Console\Scheduling\Schedule $scheduler */ | ||
$scheduler = $this->app->make(Schedule::class); | ||
foreach ($jobs as $job) { | ||
$scheduler->job($job)->name('')->everyMinute(); | ||
} | ||
$events = $scheduler->events(); | ||
foreach ($events as $event) { | ||
$event->run($this->app); | ||
} | ||
} | ||
} | ||
|
||
class TestJob implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, Dispatchable; | ||
} | ||
|
||
class UniqueTestJob extends TestJob implements ShouldBeUnique | ||
{ | ||
} |