Elgg  Version 6.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 
27  public function __construct(protected Database $db) {
28  }
29 
37  public function lock(string $namespace): bool {
38  $this->assertNamespace($namespace);
39 
40  if (!$this->isLocked($namespace)) {
41  // Lock it
42  $this->db->getConnection('write')->executeStatement("CREATE TABLE {$this->db->prefix}{$namespace}_lock (id INT)");
43 
44  $this->getLogger()->info("Locked mutex for {$namespace}");
45  return true;
46  }
47 
48  $this->getLogger()->warning("Cannot lock mutex for {$namespace}: already locked.");
49  return false;
50  }
51 
59  public function unlock(string $namespace): void {
60  $this->assertNamespace($namespace);
61 
62  $this->db->getConnection('write')->executeStatement("DROP TABLE {$this->db->prefix}{$namespace}_lock");
63 
64  $this->getLogger()->notice("Mutex unlocked for {$namespace}.");
65  }
66 
74  public function isLocked(string $namespace): bool {
75  $this->assertNamespace($namespace);
76 
77  $result = $this->db->getConnection('read')->executeQuery("SHOW TABLES LIKE '{$this->db->prefix}{$namespace}_lock'");
78  return $result->rowCount() > 0;
79  }
80 
89  protected function assertNamespace(string $namespace): void {
90  if (!ctype_alpha($namespace)) {
91  throw new InvalidArgumentException('Mutex namespace can only have characters [A-Za-z].');
92  }
93  }
94 }
assertNamespace(string $namespace)
Assert that the namespace contains only characters [A-Za-z].
Definition: Mutex.php:89
Exception thrown if an argument is not of the expected type.
The Elgg database.
Definition: Database.php:26
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(protected Database $db)
Constructor.
Definition: Mutex.php:27
isLocked(string $namespace)
Checks if mutex is locked.
Definition: Mutex.php:74
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
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:37
unlock(string $namespace)
Unlocks mutex.
Definition: Mutex.php:59