Created
June 20, 2016 10:15
-
-
Save jbrooksuk/2f5f2d7d9a095b8f193710ed61959a6a to your computer and use it in GitHub Desktop.
AltThree/Locker Usage
This file contains hidden or 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
<?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