Tripal
Public Member Functions | Public Attributes | Protected Attributes | List of all members
Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend Class Reference
Inheritance diagram for Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend:
Inheritance graph
[legend]
Collaboration diagram for Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend:
Collaboration graph
[legend]

Public Member Functions

 __construct (\Drupal\Core\Database\Connection $database, ?\Drupal\Core\Lock\LockBackendInterface $locker=NULL, ?\Drupal\Core\State\StateInterface $state=NULL)
 
 acquire ( $name, $timeout=self::DEFAULT_LOCK_TIMEOUT, string $owner='', ?int $pid=NULL)
 
 acquireShared (string $name, float $timeout=self::DEFAULT_LOCK_TIMEOUT, string $owner='', ?int $pid=NULL)
 
 release ($name)
 
 releaseShared ($name, $owner)
 
 lockMayBeAvailable ($name)
 
 releaseAll ($lock_id=NULL)
 
 cleanUnusedSharedLocks ()
 
 getOwner (string $name)
 
 getOwners (string $name)
 
 getStartTime (string $name, ?string $owner=NULL)
 
 getCurrentExpirationTime (string $name)
 
 getCurrentExpirationDelay (string $name)
 
 getOwnerPid (string $name, ?string $owner=NULL)
 
- Public Member Functions inherited from Drupal\tripal_biodb\Lock\SharedLockBackendInterface
 releaseShared (string $name, string $owner)
 
- Public Member Functions inherited from Drupal\tripal_biodb\Lock\LockInfoInterface
 getStartTime (string $name)
 
 getOwnerPid (string $name)
 

Public Attributes

const STATE_KEY_SHARED = 'tripal_shared_lock_shared'
 
const STATE_KEY_EXCLUSIVE = 'tripal_shared_lock_exclusive'
 
const LOCK_ID = '_persistent_shared'
 
const DEFAULT_LOCK_TIMEOUT = 43200.
 

Protected Attributes

 $modLocker
 
 $state
 
 $shared_locks = []
 

Detailed Description

Defines the persistent database lock backend.

This backend is global for this Drupal installation. See tests for usage examples.

In case of remaining locks that should be removed manually:

// Try that first and check after.
$locker->cleanUnusedSharedLocks();
// If there still a problem and you need to remove ALL shared locks,
// including those which may still be in use (WARNING!), try that:
// First remove persistant shared locks in semaphore table.
$locker = new Drupal\Core\Lock\DatabaseLockBackend(\Drupal::database());
$locker->acquire(mt_rand()); // We need to lock something first to unlock all.
$locker->releaseAll(Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::LOCK_ID);
$locker->releaseAll(); // Release the random lock used at first.
// Finally, remove all states recorded on shared locks.
$state = \Drupal::state();
$state->set('tripal_shared_lock_shared', []);
$state->set('tripal_shared_lock_exclusive', []);
Definition: PersistentDatabaseSharedLockBackend.php:36
const LOCK_ID
Definition: PersistentDatabaseSharedLockBackend.php:51

Constructor & Destructor Documentation

◆ __construct()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::__construct ( \Drupal\Core\Database\Connection  $database,
?\Drupal\Core\Lock\LockBackendInterface  $locker = NULL,
?\Drupal\Core\State\StateInterface  $state = NULL 
)

Constructs a new PersistentDatabaseSharedLockBackend.

Parameters
\Drupal\Core\Database\Connection$databaseThe database connection.
\Drupal\Core\Lock\LockBackendInterface$locker(optional) A lock backend used to lock modifications on shared locks. Default: if NULL, Drupal lock service is used. will be used.
\Drupal\Core\State\StateInterface$stateDrupal state service.

Member Function Documentation

◆ acquire()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::acquire (   $name,
  $timeout = self::DEFAULT_LOCK_TIMEOUT,
string  $owner = '',
?int  $pid = NULL 
)

{Acquires a lock.

Parameters
string$nameLock name. Limit of name's length is 255 characters.
float$timeout(optional) Lock lifetime in seconds. Defaults to 30.0.
string$owner(optional) Name of the (exclusive) lock owner.
?int$pid (optional) An operating system process ID owning the lock. It may be used to release the lock if the given PID becomes unused. A value of 0 will prevent the share to be released based on a PID. Default: if NULL, the current process ID (getmypid()) will be used.
Returns
bool TRUE if acquired and FALSE otherwise.
}

Implements Drupal\tripal_biodb\Lock\SharedLockBackendInterface.

◆ acquireShared()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::acquireShared ( string  $name,
float  $timeout = self::DEFAULT_LOCK_TIMEOUT,
string  $owner = '',
?int  $pid = NULL 
)

Acquires a shared lock.

Shared locks relies on a regular lock as defined in the \Drupal\Core\Lock\PersistentDatabaseLockBackend class plus a list of lock share owners stored by the Drupal State API key static::STATE_KEY_SHARED. In order to avoid race conditions on the state key value modification, a lock backend provided to the constructor is used to manage modifications.

Parameters
string$nameShared lock name. Limit of name's length is 255 characters.
float$timeout(optional) Shared lock lifetime in seconds. Defaults to self::DEFAULT_LOCK_TIMEOUT.
string$ownerIdentifier of operation willing to own a share on the lock.
?int$pid (optional) An operating system process ID owning the share. It may be used to release the lock if the given PID becomes unused. A value of 0 will prevent the share to be released based on a PID. Default: if NULL, the current process ID (getmypid()) will be used.
Returns
mixed The owner name if the shared lock has been acquired and FALSE otherwise.

Implements Drupal\tripal_biodb\Lock\SharedLockBackendInterface.

◆ cleanUnusedSharedLocks()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::cleanUnusedSharedLocks ( )

Removes expired or unused shared locks.

◆ getCurrentExpirationDelay()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getCurrentExpirationDelay ( string  $name)

{Returns the lock time remaining in seconds.Note: this delay may be extended by the lock owner.

Parameters
string$nameName of the lock.
Returns
float The lock remaining time in seconds or 0 if not available or expired.
}

Implements Drupal\tripal_biodb\Lock\LockInfoInterface.

◆ getCurrentExpirationTime()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getCurrentExpirationTime ( string  $name)

{Returns the lock expiration time in seconds.Note: this time may be extended by the lock owner.

Parameters
string$nameName of the lock.
Returns
float The lock expiration time in seconds or 0 if not available.
}

Implements Drupal\tripal_biodb\Lock\LockInfoInterface.

◆ getOwner()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getOwner ( string  $name)

{Returns the lock owner name.

Parameters
string$nameName of the lock.
Returns
string The lock owner name or an empty string if not available.
}

Implements Drupal\tripal_biodb\Lock\LockInfoInterface.

◆ getOwnerPid()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getOwnerPid ( string  $name,
?string  $owner = NULL 
)

{Returns the system process identifier using the shared or exclusive lock.If no owner is specified, the lock is assumed to be exclusive and 0 will be returned in case of a sheared lock. If an owner is specified, the PID of the owner is returned.

Parameters
string$nameName of the lock.
?string$owner Name of the owner of the share of a shared lock.
Returns
int The system process identifier using this lock or 0 if not available.
}

Implements Drupal\tripal_biodb\Lock\SharedLockBackendInterface.

◆ getOwners()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getOwners ( string  $name)

{Returns the list of shared lock owners.

Parameters
string$nameName of the shared lock.
Returns
array An array of shared lock owner names.
}

Implements Drupal\tripal_biodb\Lock\SharedLockBackendInterface.

◆ getStartTime()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::getStartTime ( string  $name,
?string  $owner = NULL 
)

{Returns the time in seconds when the lock started.If no owner is specified, the lock is assumed to be exclusive and 0 will be returned in case of a shared lock. If an owner is specified, the starting time of the lock share of the specified owner is returned.

Parameters
string$nameName of the lock.
?string$owner Name of the owner of the share of a shared lock.
Returns
float The lock starting time in seconds or 0 if not available.
}

Implements Drupal\tripal_biodb\Lock\SharedLockBackendInterface.

◆ lockMayBeAvailable()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::lockMayBeAvailable (   $name)

{}

◆ release()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::release (   $name)

{}

◆ releaseAll()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::releaseAll (   $lock_id = NULL)

{}

◆ releaseShared()

Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::releaseShared (   $name,
  $owner 
)

{}

Member Data Documentation

◆ DEFAULT_LOCK_TIMEOUT

const Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::DEFAULT_LOCK_TIMEOUT = 43200.

Default timeout for both exclusive and shared locks.

43200 seconds = 2 hours

◆ LOCK_ID

const Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::LOCK_ID = '_persistent_shared'

Lock identifier used by all persistent shared locks.

◆ STATE_KEY_EXCLUSIVE

const Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::STATE_KEY_EXCLUSIVE = 'tripal_shared_lock_exclusive'

The Drupal State API state key used to store exclusive lock details.

◆ STATE_KEY_SHARED

const Drupal\tripal_biodb\Lock\PersistentDatabaseSharedLockBackend::STATE_KEY_SHARED = 'tripal_shared_lock_shared'

The Drupal State API state key used to store shared lock details.


The documentation for this class was generated from the following file: