-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMutex.php
115 lines (100 loc) · 2.84 KB
/
Mutex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Illuminated\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis as RedisFacade;
use NinjaMutex\Lock\FlockLock;
use NinjaMutex\Lock\MemcachedLock;
use NinjaMutex\Lock\MySqlLock;
use NinjaMutex\Lock\PhpRedisLock;
use NinjaMutex\Lock\PredisRedisLock;
use NinjaMutex\Mutex as NinjaMutex;
/**
* @mixin \NinjaMutex\Mutex
*/
class Mutex
{
/**
* The console command.
*
* @var \Illuminate\Console\Command
*/
private $command;
/**
* The NinjaMutex.
*
* @var \NinjaMutex\Mutex
*/
private $ninjaMutex;
/**
* The NinjaMutex lock.
*
* @var \NinjaMutex\Lock\LockAbstract
*/
private $ninjaMutexLock;
/**
* Create a new instance of the mutex.
*
* @param \Illuminate\Console\Command|\Illuminated\Console\WithoutOverlapping $command
* @return void
*/
public function __construct(Command $command)
{
$this->command = $command;
$mutexName = $command->getMutexName();
$this->ninjaMutexLock = $this->getNinjaMutexLock();
$this->ninjaMutex = new NinjaMutex($mutexName, $this->ninjaMutexLock);
}
/**
* Get the NinjaMutex lock.
*
* @return \NinjaMutex\Lock\LockAbstract
*/
public function getNinjaMutexLock()
{
if (!empty($this->ninjaMutexLock)) {
return $this->ninjaMutexLock;
}
$strategy = $this->command->getMutexStrategy();
switch ($strategy) {
case 'mysql':
return new MySqlLock(
config('database.connections.mysql.username'),
config('database.connections.mysql.password'),
config('database.connections.mysql.host'),
config('database.connections.mysql.port', 3306)
);
case 'redis':
return $this->getRedisLock(config('database.redis.client', 'phpredis'));
case 'memcached':
return new MemcachedLock(Cache::getStore()->getMemcached());
case 'file':
default:
return new FlockLock($this->command->getMutexFileStorage());
}
}
/**
* Get the redis lock.
*
* @param string $client
* @return \NinjaMutex\Lock\LockAbstract
*/
private function getRedisLock($client)
{
$redis = RedisFacade::connection()->client();
return $client === 'phpredis'
? new PhpRedisLock($redis)
: new PredisRedisLock($redis);
}
/**
* Forward method calls to NinjaMutex.
*
* @param string $method
* @param mixed $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->ninjaMutex, $method], $parameters);
}
}