-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathWithoutOverlapping.php
136 lines (121 loc) · 3.17 KB
/
WithoutOverlapping.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Illuminated\Console;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait WithoutOverlapping
{
/**
* Overwrite the console command initialization.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->initializeMutex();
parent::initialize($input, $output);
}
/**
* Initialize the mutex.
*
* @return void
*/
protected function initializeMutex()
{
$mutex = new Mutex($this);
$timeout = $this->getMutexTimeout();
if (!$mutex->acquireLock($timeout)) {
throw new MutexRuntimeException('Command is running now!');
}
register_shutdown_function([$this, 'releaseMutexLock'], $mutex);
}
/**
* Get the mutex strategy.
*
* Currently supported: "file", "mysql", "redis" and "memcached".
*
* @return string
*/
public function getMutexStrategy()
{
return property_exists($this, 'mutexStrategy')
? $this->mutexStrategy
: 'file';
}
/**
* Set the mutex strategy.
*
* Currently supported: "file", "mysql", "redis" and "memcached".
*
* @param string $strategy
* @return void
*/
public function setMutexStrategy($strategy)
{
$this->mutexStrategy = $strategy;
}
/**
* Get the mutex timeout in milliseconds.
*
* Possible values:
* `0` - check without waiting;
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
* `null` - wait, till running command finish its execution;
*
* @return int|null
*/
public function getMutexTimeout()
{
return property_exists($this, 'mutexTimeout')
? $this->mutexTimeout
: 0;
}
/**
* Set the mutex timeout in milliseconds.
*
* Possible values:
* `0` - check without waiting;
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
* `null` - wait, till running command finish its execution;
*
* @param int|null $timeout
* @return void
*/
public function setMutexTimeout($timeout)
{
$this->mutexTimeout = $timeout;
}
/**
* Get the mutex name.
*
* @return string
*/
public function getMutexName()
{
$name = $this->getName();
$argumentsHash = md5(json_encode($this->argument()));
return "icmutex-{$name}-{$argumentsHash}";
}
/**
* Get the mutex file storage path.
*
* @return string
*/
public function getMutexFileStorage()
{
return storage_path('app');
}
/**
* Release the mutex lock.
*
* Called automatically, because it's registered as a shutdown function.
*
* @param \Illuminated\Console\Mutex $mutex
* @return void
*/
public function releaseMutexLock(Mutex $mutex)
{
$mutex->releaseLock();
}
}