<?php
|
namespace JVBase\managers\queue;
|
use wpdb;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
class Locker
|
{
|
private string $lockKey;
|
private int $timeout;
|
protected wpdb $wpdb;
|
private ?string $token = null;
|
|
public function __construct(string $key = 'queue', int $timeout = 0)
|
{
|
$this->lockKey = BASE . $key . '_lock';
|
$this->timeout = $timeout;
|
global $wpdb;
|
$this->wpdb = $wpdb;
|
}
|
|
/**
|
* Execute callback with lock, auto-release after
|
*/
|
public function withLock(callable $callback): void
|
{
|
$acquired = $this->wpdb->get_var(
|
$this->wpdb->prepare(
|
'SELECT GET_LOCK(%s, %d)',
|
$this->lockKey,
|
$this->timeout
|
)
|
);
|
|
if ((int) $acquired !== 1) {
|
// Lock already held — just exit quietly
|
return;
|
}
|
|
try {
|
$callback();
|
} finally {
|
$this->wpdb->get_var(
|
$this->wpdb->prepare(
|
'SELECT RELEASE_LOCK(%s)',
|
$this->lockKey
|
)
|
);
|
}
|
}
|
}
|