Skip to content

Commit 4f16b8b

Browse files
committed
ICM: File strategy - initial.
1 parent 6965098 commit 4f16b8b

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Mutex.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
namespace Illuminated\Console;
44

55
use Illuminate\Console\Command;
6+
use NinjaMutex\Lock\FlockLock;
7+
use NinjaMutex\Mutex as Ninja;
8+
use NinjaMutex\MutexException;
69

710
class Mutex
811
{
912
private $command;
1013
private $strategy;
14+
private $ninja;
1115

1216
public function __construct(Command $command)
1317
{
1418
$this->command = $command;
1519
$this->strategy = $this->strategy();
20+
$this->ninja = new Ninja('test', $this->strategy);
1621
}
1722

1823
private function strategy()
@@ -23,11 +28,25 @@ private function strategy()
2328

2429
switch ($this->command->getMutexStrategy()) {
2530
case 'mysql':
26-
break;
31+
throw new MutexException('Strategy `mysql` is not implemented yet.');
32+
33+
case 'redis':
34+
throw new MutexException('Strategy `redis` is not implemented yet.');
35+
36+
case 'memcache':
37+
throw new MutexException('Strategy `memcache` is not implemented yet.');
38+
39+
case 'memcached':
40+
throw new MutexException('Strategy `memcached` is not implemented yet.');
2741

2842
case 'file':
2943
default:
30-
break;
44+
return new FlockLock(storage_path('framework'));
3145
}
3246
}
47+
48+
public function __call($method, $parameters)
49+
{
50+
return call_user_func_array([$this->ninja, $method], $parameters);
51+
}
3352
}

src/WithoutOverlapping.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ trait WithoutOverlapping
1010
protected function execute(InputInterface $input, OutputInterface $output)
1111
{
1212
$mutex = new Mutex($this);
13+
if (!$mutex->acquireLock(0)) {
14+
$this->info('Command already running!');
15+
return;
16+
}
1317

14-
return parent::execute($input, $output);
18+
$code = parent::execute($input, $output);
19+
$mutex->releaseLock();
20+
21+
return $code;
1522
}
1623

1724
public function getMutexStrategy()

0 commit comments

Comments
 (0)