Elgg  Version 5.1
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 
22  protected Database $db;
23 
29  public function __construct(Database $db) {
30  $this->db = $db;
31  }
32 
40  public function lock(string $namespace): bool {
41  $this->assertNamespace($namespace);
42 
43  if (!$this->isLocked($namespace)) {
44  // Lock it
45  $this->db->getConnection('write')->executeStatement("CREATE TABLE {$this->db->prefix}{$namespace}_lock (id INT)");
46 
47  $this->getLogger()->info("Locked mutex for {$namespace}");
48  return true;
49  }
50 
51  $this->getLogger()->warning("Cannot lock mutex for {$namespace}: already locked.");
52  return false;
53  }
54 
62  public function unlock(string $namespace): void {
63  $this->assertNamespace($namespace);
64 
65  $this->db->getConnection('write')->executeStatement("DROP TABLE {$this->db->prefix}{$namespace}_lock");
66 
67  $this->getLogger()->notice("Mutex unlocked for {$namespace}.");
68  }
69 
77  public function isLocked(string $namespace): bool {
78  $this->assertNamespace($namespace);
79 
80  $result = $this->db->getConnection('read')->executeQuery("SHOW TABLES LIKE '{$this->db->prefix}{$namespace}_lock'");
81  return $result->rowCount() > 0;
82  }
83 
92  protected function assertNamespace(string $namespace): void {
93  if (!ctype_alpha($namespace)) {
94  throw new InvalidArgumentException('Mutex namespace can only have characters [A-Za-z].');
95  }
96  }
97 }
assertNamespace(string $namespace)
Assert that the namespace contains only characters [A-Za-z].
Definition: Mutex.php:92
Exception thrown if an argument is not of the expected type.
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:29
isLocked(string $namespace)
Checks if mutex is locked.
Definition: Mutex.php:77
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Database $db
Definition: Mutex.php:22
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:40
unlock(string $namespace)
Unlocks mutex.
Definition: Mutex.php:62