Elgg\Database\Mutex::lock PHP Метод

lock() публичный Метод

Creates a table {prefix}{$namespace}_lock that is used as a mutex.
public lock ( string $namespace ) : boolean
$namespace string Allows having separate locks for separate processes
Результат boolean
    public function lock($namespace)
    {
        $this->assertNamespace($namespace);
        if (!$this->isLocked($namespace)) {
            // Lock it
            $this->db->insertData("CREATE TABLE {$this->db->prefix}{$namespace}_lock (id INT)");
            $this->logger->info("Locked mutex for {$namespace}");
            return true;
        }
        $this->logger->warn("Cannot lock mutex for {$namespace}: already locked.");
        return false;
    }

Usage Example

Пример #1
0
 /**
  * Run the upgrade process
  *
  * @return array $result Associative array containing possible errors
  */
 public function run()
 {
     $result = array('failure' => false, 'reason' => '');
     // prevent someone from running the upgrade script in parallel (see #4643)
     if (!$this->mutex->lock('upgrade')) {
         $result['failure'] = true;
         $result['reason'] = $this->translator->translate('upgrade:locked');
         return $result;
     }
     // disable the system log for upgrades to avoid exceptions when the schema changes.
     $this->events->unregisterHandler('log', 'systemlog', 'system_log_default_logger');
     $this->events->unregisterHandler('all', 'all', 'system_log_listener');
     // turn off time limit
     set_time_limit(0);
     if ($this->getUnprocessedUpgrades()) {
         $this->processUpgrades();
     }
     $this->events->trigger('upgrade', 'system', null);
     elgg_flush_caches();
     $this->mutex->unlock('upgrade');
     return $result;
 }