Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 4, 2020
1 parent 7c5a18c commit 2123e60
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Contracts\Queue;

interface UniqueJob
interface ShouldBeUnique
{
//
}
18 changes: 10 additions & 8 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\UniqueJob;
use Illuminate\Contracts\Queue\ShouldBeUnique;

class PendingDispatch
{
Expand Down Expand Up @@ -131,20 +131,22 @@ public function afterResponse()
*/
protected function shouldDispatch()
{
if (! ($this->job instanceof UniqueJob)) {
if (! $this->job instanceof ShouldBeUnique) {
return true;
}

$uniqueId = method_exists($this->job, 'uniqueId')
? $this->job->uniqueId()
: ($this->job->uniqueId ?? '');

$lock = Container::getInstance()->make(Cache::class)->lock(
$key = 'unique:'.get_class($this->job).$uniqueId,
$this->job->uniqueFor ?? 0
);
$cache = method_exists($this->job, 'uniqueVia')
? $this->job->uniqueVia()
: Container::getInstance()->make(Cache::class);

return (bool) $lock->get();
return (bool) $cache->lock(
$key = 'laravel_unique_job:'.get_class($this->job).$uniqueId,
$this->job->uniqueFor ?? 0
)->get();
}

/**
Expand All @@ -169,7 +171,7 @@ public function __call($method, $parameters)
public function __destruct()
{
if (! $this->shouldDispatch()) {
// Do nothing.
return;
} elseif ($this->afterResponse) {
app(Dispatcher::class)->dispatchAfterResponse($this->job);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/stubs/job.queued.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace {{ namespace }};

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\UniqueJob;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
Expand Down
26 changes: 16 additions & 10 deletions src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Queue\UniqueJob;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pipeline\Pipeline;
use ReflectionClass;
Expand Down Expand Up @@ -160,22 +160,28 @@ protected function ensureSuccessfulBatchJobIsRecorded($command)
}

/**
* Ensure the lock for the unique job is released.
* Ensure the lock for a unique job is released.
*
* @param mixed $command
* @return void
*/
protected function ensureUniqueJobLockIsReleased($command)
{
if ($command instanceof UniqueJob) {
$uniqueId = method_exists($command, 'uniqueId')
? $command->uniqueId()
: ($command->uniqueId ?? '');

$this->container->make(Cache::class)
->lock('unique:'.get_class($command).$uniqueId)
->forceRelease();
if (! $command instanceof ShouldBeUnique) {
return;
}

$uniqueId = method_exists($command, 'uniqueId')
? $command->uniqueId()
: ($command->uniqueId ?? '');

$cache = method_exists($command, 'uniqueVia')
? $command->uniqueVia()
: $this->container->make(Cache::class);

$cache->lock(
'laravel_unique_job:'.get_class($command).$uniqueId
)->forceRelease();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Console/Scheduling/FrequencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\EventMutex;
use Illuminate\Support\Carbon;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -91,7 +92,7 @@ public function testMonthlyOn()

public function testLastDayOfMonth()
{
$this->assertSame('0 0 31 * *', $this->event->lastDayOfMonth()->getExpression());
$this->assertSame('0 0 '.Carbon::now()->endOfMonth()->day.' * *', $this->event->lastDayOfMonth()->getExpression());
}

public function testTwiceMonthly()
Expand Down
11 changes: 7 additions & 4 deletions tests/Integration/Queue/UniqueJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\UniqueJob;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
Expand All @@ -21,6 +21,7 @@ class UniqueJobTest extends TestCase
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
Expand Down Expand Up @@ -51,6 +52,7 @@ protected function tearDown(): void
public function testUniqueJobsAreNotDispatched()
{
Bus::fake();

UniqueTestJob::dispatch();
Bus::assertDispatched(UniqueTestJob::class);

Expand Down Expand Up @@ -93,6 +95,7 @@ public function testLockIsReleasedForFailedJobs()
public function testLockIsNotReleasedForJobRetries()
{
UniqueTestRetryJob::$handled = false;

dispatch($job = new UniqueTestRetryJob);

$this->assertFalse($this->app->get(Cache::class)->lock($this->getLockKey($job), 10)->get());
Expand Down Expand Up @@ -142,11 +145,11 @@ public function testLockIsNotReleasedForJobReleases()

protected function getLockKey($job)
{
return 'unique:'.(is_string($job) ? $job : get_class($job));
return 'laravel_unique_job:'.(is_string($job) ? $job : get_class($job));
}
}

class UniqueTestJob implements ShouldQueue, UniqueJob
class UniqueTestJob implements ShouldQueue, ShouldBeUnique
{
use InteractsWithQueue, Queueable, Dispatchable;

Expand All @@ -158,7 +161,7 @@ public function handle()
}
}

class UniqueTestFailJob implements ShouldQueue, UniqueJob
class UniqueTestFailJob implements ShouldQueue, ShouldBeUnique
{
use InteractsWithQueue, Queueable, Dispatchable;

Expand Down

0 comments on commit 2123e60

Please sign in to comment.