Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 14, 2020
1 parent 2a587e5 commit eed05b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,53 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Support\InteractsWithTime;

class PreventOverlappingJobs
class WithoutOverlapping
{
use InteractsWithTime;

/**
* The amount of time (in seconds) to expire the lock.
* The job's unique key used for preventing overlaps.
*
* @var int
* @var string
*/
public $expiresAt;
public $key;

/**
* The key of the job.
* The number of seconds before a job should be available again if no lock was acquired.
*
* @var string
* @var \DateTimeInterface|int|null
*/
public $key;
public $releaseAfter;

/**
* The prefix of the lock key.
* The number of seconds before the lock should expire.
*
* @var string
* @var int
*/
public $prefix = 'overlap:';
public $expiresAfter;

/**
* The delay (in seconds) to release the job back to the queue.
* The prefix of the lock key.
*
* @var int|null
* @var string
*/
public $releaseAfter;
public $prefix = 'laravel-queue-overlap:';

/**
* Create a new overlapping jobs middleware instance.
* Create a new middleware instance.
*
* @param string $key
* @param int|null $releaseAfter
* @param int $expiresAt
*
* @param \DateTimeInterface|int|null $releaseAfter
* @param \DateTimeInterface|int $expiresAfter
* @return void
*/
public function __construct($key = '', $releaseAfter = 0, $expiresAt = 0)
public function __construct($key = '', $releaseAfter = 0, $expiresAfter = 0)
{
$this->key = $key;
$this->releaseAfter = $releaseAfter;
$this->expiresAt = $expiresAt;
$this->expiresAfter = $this->secondsUntil($expiresAfter);
}

/**
Expand All @@ -60,8 +62,9 @@ public function __construct($key = '', $releaseAfter = 0, $expiresAt = 0)
*/
public function handle($job, $next)
{
$lock = Container::getInstance()->make(Cache::class)
->lock($this->getLockKey($job), $this->expiresAt);
$lock = Container::getInstance()->make(Cache::class)->lock(
$this->getLockKey($job), $this->expiresAfter
);

if ($lock->get()) {
try {
Expand All @@ -75,39 +78,39 @@ public function handle($job, $next)
}

/**
* Do not release the job back to the queue.
* Set the delay (in seconds) to release the job back to the queue.
*
* @param int $releaseAfter
* @return $this
*/
public function dontRelease()
public function releaseAfter($releaseAfter)
{
$this->releaseAfter = null;
$this->releaseAfter = $releaseAfter;

return $this;
}

/**
* Set the expiry (in seconds) of the lock key.
* Do not release the job back to the queue if no lock can be acquired.
*
* @param int $expiresAt
* @return $this
*/
public function expireAt($expiresAt)
public function dontRelease()
{
$this->expiresAt = $expiresAt;
$this->releaseAfter = null;

return $this;
}

/**
* Set the delay (in seconds) to release the job back to the queue.
* Set the maximum number of seconds that can elapse before the lock is released.
*
* @param int $releaseAfter
* @param \DateTimeInterface|int $expiresAfter
* @return $this
*/
public function releaseAfter($releaseAfter)
public function expireAfter($expiresAfter)
{
$this->releaseAfter = $releaseAfter;
$this->expiresAfter = $this->secondsUntil($expiresAfter);

return $this;
}
Expand All @@ -118,15 +121,15 @@ public function releaseAfter($releaseAfter)
* @param string $prefix
* @return $this
*/
public function withPrefix($prefix)
public function withPrefix(string $prefix)
{
$this->prefix = $prefix;

return $this;
}

/**
* Get the lock key.
* Get the lock key for the given job.
*
* @param mixed $job
* @return string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\PreventOverlappingJobs;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Mockery as m;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class PreventOverlappingJobsTest extends TestCase
class WithoutOverlappingJobsTest extends TestCase
{
protected function tearDown(): void
{
Expand All @@ -40,7 +40,7 @@ public function testNonOverlappingJobsAreExecuted()
'command' => serialize($command = new OverlappingTestJob),
]);

$lockKey = (new PreventOverlappingJobs)->getLockKey($command);
$lockKey = (new WithoutOverlapping)->getLockKey($command);

$this->assertTrue(OverlappingTestJob::$handled);
$this->assertTrue($this->app->get(Cache::class)->lock($lockKey, 10)->acquire());
Expand All @@ -64,7 +64,7 @@ public function testLockIsReleasedOnJobExceptions()
'command' => serialize($command = new FailedOverlappingTestJob),
]);
} finally {
$lockKey = (new PreventOverlappingJobs)->getLockKey($command);
$lockKey = (new WithoutOverlapping)->getLockKey($command);

$this->assertTrue(FailedOverlappingTestJob::$handled);
$this->assertTrue($this->app->get(Cache::class)->lock($lockKey, 10)->acquire());
Expand All @@ -76,7 +76,7 @@ public function testOverlappingJobsAreReleased()
OverlappingTestJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$lockKey = (new PreventOverlappingJobs)->getLockKey($command = new OverlappingTestJob);
$lockKey = (new WithoutOverlapping)->getLockKey($command = new OverlappingTestJob);
$this->app->get(Cache::class)->lock($lockKey, 10)->acquire();

$job = m::mock(Job::class);
Expand All @@ -98,7 +98,7 @@ public function testOverlappingJobsCanBeSkipped()
SkipOverlappingTestJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$lockKey = (new PreventOverlappingJobs)->getLockKey($command = new SkipOverlappingTestJob);
$lockKey = (new WithoutOverlapping)->getLockKey($command = new SkipOverlappingTestJob);
$this->app->get(Cache::class)->lock($lockKey, 10)->acquire();

$job = m::mock(Job::class);
Expand Down Expand Up @@ -129,15 +129,15 @@ public function handle()

public function middleware()
{
return [new PreventOverlappingJobs];
return [new WithoutOverlapping];
}
}

class SkipOverlappingTestJob extends OverlappingTestJob
{
public function middleware()
{
return [(new PreventOverlappingJobs)->dontRelease()];
return [(new WithoutOverlapping)->dontRelease()];
}
}

Expand Down

0 comments on commit eed05b4

Please sign in to comment.