class CRM_Core_Lock implements LockInterface

Constants

TIMEOUT

Properties

static bool $jobLog This variable (despite it's name) roughly translates to 'lock that we actually care about'.
protected $_hasLock
protected $_name
protected $_id

Methods

static LockInterface
createGlobalLock(string $name)

Use MySQL's GET_LOCK(). Locks are shared across all Civi instances on the same MySQL server.

static LockInterface
createScopedLock(string $name)

Use MySQL's GET_LOCK(), but apply prefixes to the lock names.

static LockInterface
createCivimailLock(string $name) deprecated

Use MySQL's GET_LOCK(), but conditionally apply prefixes to the lock names (if civimail_server_wide_lock is disabled).

__construct(string $name, int $timeout = NULL, bool $serverWideLock = FALSE)

Initialize the constants used during lock acquire / release

__destruct()

No description

bool
acquire(int|NULL $timeout = NULL)

Acquire lock.

bool|null|string
release()

No description

bool|null|string
isFree()

No description

bool
isAcquired()

No description

bool
hackyHandleBrokenCode(string $jobLog)

CRM-12856 locks were originally set up for jobs, but the concept was extended to caching & groups without understanding that would undermine the job locks (because grabbing a lock implicitly releases existing ones) this is all a big hack to mitigate the impact of that - but should not be seen as a fix. Not sure correct fix but maybe locks should be used more selectively? Or else we need to handle is some cool way that Tim is yet to write :-) if we are running in the context of the cron log then we would rather die (or at least let our process die) than release that lock - so if the attempt is being made by setCache or something relatively trivial we'll just return TRUE, but if it's another job then we will crash as that seems 'safer'

Details

at line 70
static LockInterface createGlobalLock(string $name)

Use MySQL's GET_LOCK(). Locks are shared across all Civi instances on the same MySQL server.

Parameters

string $name Symbolic name for the lock. Names generally look like "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}").

Categories: worker|data|cache|... Component: core|mailing|member|contribute|...

Return Value

LockInterface

at line 86
static LockInterface createScopedLock(string $name)

Use MySQL's GET_LOCK(), but apply prefixes to the lock names.

Locks are unique to each instance of Civi.

Parameters

string $name Symbolic name for the lock. Names generally look like "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}").

Categories: worker|data|cache|... Component: core|mailing|member|contribute|...

Return Value

LockInterface

at line 103
static LockInterface createCivimailLock(string $name) deprecated

deprecated

Use MySQL's GET_LOCK(), but conditionally apply prefixes to the lock names (if civimail_server_wide_lock is disabled).

Parameters

string $name Symbolic name for the lock. Names generally look like "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}").

Categories: worker|data|cache|... Component: core|mailing|member|contribute|...

Return Value

LockInterface

at line 125
__construct(string $name, int $timeout = NULL, bool $serverWideLock = FALSE)

Initialize the constants used during lock acquire / release

Parameters

string $name Symbolic name for the lock. Names generally look like "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}").

Categories: worker|data|cache|... Component: core|mailing|member|contribute|...

int $timeout The number of seconds to wait to get the lock. 1 if not set.
bool $serverWideLock Should this lock be applicable across your entire mysql server. this is useful if you have multiple sites running on the same mysql server and you want to limit the number of parallel cron jobs - CRM-91XX

at line 144
__destruct()

at line 178
bool acquire(int|NULL $timeout = NULL)

Acquire lock.

The advantage of mysql locks is that they can be used across processes. However, only one can be used at once within a process. An attempt to use a second one within a process prior to mysql 5.7.5 results in the first being released.

The process here is 1) first attempt to grab a lock for a mailing job - self::jobLog will be populated with the lock id & a mysql lock will be created for the ID.

If a second function in the same process attempts to grab the lock it will enter the hackyHandleBrokenCode routine which says 'I won't break a mailing lock for you but if you are not a civimail send process I'll let you pretend you have a lock already and you can go ahead with whatever you were doing under the delusion you have a lock.

Parameters

int|NULL $timeout The number of seconds to wait to get the lock. For a default value, use NULL.

Return Value

bool

Exceptions

CRM_Core_Exception

at line 211
bool|null|string release()

Return Value

bool|null|string Trueish/falsish.

at line 231
bool|null|string isFree()

Return Value

bool|null|string Trueish/falsish.

at line 240
bool isAcquired()

Return Value

bool

at line 257
bool hackyHandleBrokenCode(string $jobLog)

CRM-12856 locks were originally set up for jobs, but the concept was extended to caching & groups without understanding that would undermine the job locks (because grabbing a lock implicitly releases existing ones) this is all a big hack to mitigate the impact of that - but should not be seen as a fix. Not sure correct fix but maybe locks should be used more selectively? Or else we need to handle is some cool way that Tim is yet to write :-) if we are running in the context of the cron log then we would rather die (or at least let our process die) than release that lock - so if the attempt is being made by setCache or something relatively trivial we'll just return TRUE, but if it's another job then we will crash as that seems 'safer'

Parameters

string $jobLog

Return Value

bool

Exceptions

CRM_Core_Exception