Elgg  Version 4.3
Mutex.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
8 
18 class Mutex {
19 
20  use Loggable;
21 
25  private $db;
26 
32  public function __construct(Database $db) {
33  $this->db = $db;
34  }
35 
43  public function lock(string $namespace): bool {
44  $this->assertNamespace($namespace);
45 
46  if (!$this->isLocked($namespace)) {
47  // Lock it
48  $this->db->getConnection('write')->executeStatement("CREATE TABLE {$this->db->prefix}{$namespace}_lock (id INT)");
49 
50  $this->getLogger()->info("Locked mutex for {$namespace}");
51  return true;
52  }
53 
54  $this->getLogger()->warning("Cannot lock mutex for {$namespace}: already locked.");
55  return false;
56  }
57 
65  public function unlock(string $namespace): void {
66  $this->assertNamespace($namespace);
67 
68  $this->db->getConnection('write')->executeStatement("DROP TABLE {$this->db->prefix}{$namespace}_lock");
69 
70  $this->getLogger()->notice("Mutex unlocked for {$namespace}.");
71  }
72 
80  public function isLocked(string $namespace): bool {
81  $this->assertNamespace($namespace);
82 
83  $result = $this->db->getConnection('read')->executeQuery("SHOW TABLES LIKE '{$this->db->prefix}{$namespace}_lock'");
84  return $result->rowCount() > 0;
85  }
86 
95  private function assertNamespace(string $namespace): void {
96  if (!ctype_alpha($namespace)) {
97  throw new InvalidParameterException("Mutex namespace can only have characters [A-Za-z].");
98  }
99  }
100 }
The Elgg database.
Definition: Database.php:25
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Provides database mutex that can be used to prevent race conditions between two processes that affect...
Definition: Mutex.php:18
__construct(Database $db)
Constructor.
Definition: Mutex.php:32
isLocked(string $namespace)
Checks if mutex is locked.
Definition: Mutex.php:80
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
getLogger()
Returns logger.
Definition: Loggable.php:37
lock(string $namespace)
Creates a table {prefix}{$namespace}_lock that is used as a mutex.
Definition: Mutex.php:43
unlock(string $namespace)
Unlocks mutex.
Definition: Mutex.php:65