Skip to content

Commit 086ef4a

Browse files
committed
ICM: Changed mutex integration - now through initialize method.
1 parent 593a4d2 commit 086ef4a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/WithoutOverlapping.php

+20-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@
77

88
trait WithoutOverlapping
99
{
10+
protected function initialize(InputInterface $input, OutputInterface $output)
11+
{
12+
$this->initializeMutex();
13+
14+
parent::initialize($input, $output);
15+
}
16+
17+
protected function initializeMutex()
18+
{
19+
$mutex = new Mutex($this);
20+
if (!$mutex->acquireLock(0)) {
21+
$this->info('Command is running now!');
22+
exit();
23+
}
24+
25+
register_shutdown_function(function () use ($mutex) {
26+
$mutex->releaseLock();
27+
});
28+
}
29+
1030
public function getMutexStrategy()
1131
{
1232
return (isset($this->mutexStrategy) ? $this->mutexStrategy : 'file');
@@ -23,18 +43,4 @@ public function getMutexName()
2343
$arguments = json_encode($this->argument());
2444
return "icmutex-{$name}-" . md5($arguments);
2545
}
26-
27-
protected function execute(InputInterface $input, OutputInterface $output)
28-
{
29-
$mutex = new Mutex($this);
30-
if (!$mutex->acquireLock(0)) {
31-
$this->info('Command is running now!');
32-
return;
33-
}
34-
35-
$code = parent::execute($input, $output);
36-
$mutex->releaseLock();
37-
38-
return $code;
39-
}
4046
}

0 commit comments

Comments
 (0)