-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62147b4
commit 67ececf
Showing
4 changed files
with
112 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Platformsh\Cli\Service; | ||
|
||
class FileLock | ||
{ | ||
private $config; | ||
private $fs; | ||
|
||
private $checkIntervalMs; | ||
private $timeLimit; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
$this->config = $config; | ||
$this->fs = new \Symfony\Component\Filesystem\Filesystem(); | ||
$this->checkIntervalMs = 300; | ||
$this->timeLimit = 30; | ||
} | ||
|
||
/** | ||
* Acquires a lock, or waits for one if it already exists. | ||
* | ||
* @param string $lockName | ||
* A unique name for the lock. | ||
* @param callable|null $onWait | ||
* A function to run when waiting starts. | ||
* @param callable|null $check | ||
* A function to run each time the interval has passed. If it returns a | ||
* non-null value, waiting will stop, and the value will be returned | ||
* from this method. | ||
* | ||
* @return mixed|null | ||
*/ | ||
public function acquireOrWait($lockName, callable $onWait = null, callable $check = null) | ||
{ | ||
$runOnWait = false; | ||
$filename = $this->filename($lockName); | ||
$start = \time(); | ||
while (\time() - $start < $this->timeLimit) { | ||
if (!\file_exists($filename)) { | ||
break; | ||
} | ||
$content = \file_get_contents($filename); | ||
if ($content === false || $content === '') { | ||
break; | ||
} | ||
$lockedAt = \intval($content); | ||
if ($lockedAt === 0 || \time() >= $lockedAt + $this->timeLimit) { | ||
break; | ||
} | ||
if ($onWait !== null && !$runOnWait) { | ||
$onWait(); | ||
$runOnWait = true; | ||
} | ||
\usleep($this->checkIntervalMs * 1000); | ||
if ($check !== null) { | ||
$result = $check(); | ||
if ($result !== null) { | ||
$this->release($lockName); | ||
return $result; | ||
} | ||
} | ||
} | ||
$this->fs->dumpFile($filename, (string) \time()); | ||
return null; | ||
} | ||
|
||
/** | ||
* Releases a lock that was created by acquire(). | ||
* | ||
* @param string $lockName | ||
*/ | ||
public function release($lockName) | ||
{ | ||
// Truncate the file (don't delete it). | ||
$this->fs->dumpFile($this->filename($lockName), ''); | ||
} | ||
|
||
/** | ||
* @param string $lockName | ||
* @return string | ||
*/ | ||
private function filename($lockName) | ||
{ | ||
return $this->config->getWritableUserDir() | ||
. DIRECTORY_SEPARATOR . 'locks' | ||
. DIRECTORY_SEPARATOR | ||
. preg_replace('/[^\w_-]+/', '-', $lockName) | ||
. '.lock'; | ||
} | ||
} |
This file contains 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