Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk
Created June 20, 2016 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrooksuk/2f5f2d7d9a095b8f193710ed61959a6a to your computer and use it in GitHub Desktop.
Save jbrooksuk/2f5f2d7d9a095b8f193710ed61959a6a to your computer and use it in GitHub Desktop.
AltThree/Locker Usage
<?php
namespace App\Bus\Middleware;
use AltThree\Locker\Locker;
use Closure;
use Illuminate\Contracts\Queue\ShouldQueue;
class LockingMiddleware
{
use NameTrait;
/**
* The locker instance.
*
* @var \AltThree\Locker\Locker
*/
protected $locker;
/**
* Create a new locking middleware instance.
*
* @param \AltThree\Locker\Locker $locker
*
* @return void
*/
public function __construct(Locker $locker)
{
$this->locker = $locker;
}
/**
* Aquire a lock for the command before execution.
*
* @param object $command
* @param \Closure $next
*
* @return void
*/
public function handle($command, Closure $next)
{
$function = function () use ($command, $next) {
return $next($command);
};
$name = $this->name($command);
$timeout = $command instanceof ShouldQueue ? 200000 : 60000;
return $this->locker->execute($function, $name, $timeout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment