Requirements 1-8 + all non-negotiables:
1. Model manager (CRUD): PRV_Model_Registry, prv_models v2 option, versioned
idempotent migration from v0.1.x flat-string format, Settings page CRUD.
2. Per-model run-health: PRV_Model_Registry::update_health() after each run;
health badges (Healthy/Retired?/Disabled/Unknown) in model table + dashboard.
3. Metric comparability: PRV_Config_Version stamps every run with config_version;
save-bar warning on scoring-relevant changes; orphaned rows excluded by version.
4. Projected cost at save: PRV_Config::get_projected_cost(); over-cap warning;
prv_last_run_truncated option + visible notice when cap hit mid-run.
5. Config surface: peptide set, intents, cadence (reschedules cron), budget cap;
all validated + sanitized; next-run timestamp shown.
6. API-key three-state: not-defined / defined-ok / defined-failed; derived from
ledger; never displays the key.
7. Run-lock: PRV_Run_Lock (transient); cron tick skips if locked; Run-now
refuses with clear message; lock released in finally block.
8. Design fixes: dark-ink focus ring on bright fills; disabled-row full AA
contrast (no opacity); toggle disabled on retired rows; save-bar-level
warning (role=alert); chip text #B4BDC7 6.83:1 AA; keyboard-sortable headers
(<button>+aria-sort); aria-live on Test chip; z-index:50 save-bar;
run-now double-fire guard; @media print light overrides.
New: PRV_Upgrader (runs migrations on plugins_loaded, not just activation),
PRV_Config_Version, PRV_Run_Lock, PRV_Settings_Page, PRV_Settings_Renderer,
PRV_Model_Manager_Table, PRV_Model_Test_Ajax.
Tests: test-model-registry, test-run-lock, test-config-version,
test-projected-cost, test-cron-reschedule (+ updated probe-runner,
uninstall, bootstrap).
Schema: config_version INT column added via dbDelta (idempotent).
Uninstall: purges all v0.2 prv_* options + transients.
95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
/**
|
|
* Run-lock: prevents concurrent cron + Run-now executions.
|
|
*
|
|
* @package PrVision
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Distributed run-lock using a WP transient.
|
|
*
|
|
* Ensures exactly one probe run (cron or manual) executes at a time.
|
|
* Acquiring the lock is atomic via the transient API with a TTL so stale
|
|
* locks (e.g. from a crashed run) self-expire after PRV_RUN_LOCK_TTL_SECONDS.
|
|
*
|
|
* Who triggers: PRV_Probe_Runner::run() acquires; PRV_Admin_Page "Run now"
|
|
* handler checks before dispatching.
|
|
* Dependencies: set_transient(), get_transient(), delete_transient().
|
|
*
|
|
* @see class-prv-probe-runner.php -- Acquires + releases lock around the run.
|
|
* @see class-prv-settings-page.php -- Checks is_locked() before Run now.
|
|
* @package PrVision
|
|
*/
|
|
class PRV_Run_Lock {
|
|
|
|
/**
|
|
* Transient key for the run lock.
|
|
*/
|
|
const LOCK_KEY = 'prv_run_lock';
|
|
|
|
/**
|
|
* Lock TTL in seconds. Should exceed the longest plausible run.
|
|
*/
|
|
const TTL_SECONDS = 3600; // 1 hour.
|
|
|
|
/**
|
|
* Attempt to acquire the run lock.
|
|
*
|
|
* Returns true only if the lock was not already held. Uses a non-blocking
|
|
* check-and-set approach: if the transient already exists the lock is held.
|
|
*
|
|
* Side effects: Sets a WP transient when successful.
|
|
*
|
|
* @return bool True if lock acquired; false if already held.
|
|
*/
|
|
public static function acquire(): bool {
|
|
if ( false !== get_transient( self::LOCK_KEY ) ) {
|
|
return false; // Already locked.
|
|
}
|
|
// set_transient returns false if the transient already exists (on some hosts);
|
|
// re-check after set to guard against the race window.
|
|
set_transient( self::LOCK_KEY, time(), self::TTL_SECONDS );
|
|
// Brief window for a second writer -- re-check.
|
|
$val = get_transient( self::LOCK_KEY );
|
|
return false !== $val;
|
|
}
|
|
|
|
/**
|
|
* Release the run lock.
|
|
*
|
|
* Should be called in a finally block after every run, whether successful
|
|
* or not, to prevent lock starvation on non-fatal errors.
|
|
*
|
|
* Side effects: Deletes the WP transient.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function release(): void {
|
|
delete_transient( self::LOCK_KEY );
|
|
}
|
|
|
|
/**
|
|
* Check whether a run is currently in progress.
|
|
*
|
|
* @return bool True when locked.
|
|
*/
|
|
public static function is_locked(): bool {
|
|
return false !== get_transient( self::LOCK_KEY );
|
|
}
|
|
|
|
/**
|
|
* Return approximate seconds since the lock was acquired.
|
|
*
|
|
* @return int|null Null if not locked.
|
|
*/
|
|
public static function locked_since(): ?int {
|
|
$val = get_transient( self::LOCK_KEY );
|
|
if ( false === $val ) {
|
|
return null;
|
|
}
|
|
return (int) ( time() - (int) $val );
|
|
}
|
|
}
|