|null */ private static $active = null; /** * Whether the shutdown handler is registered. * * @var bool */ private static $shutdown_registered = false; /** * Memory reserved during a tick and released in the shutdown handler. * * @var string|null */ private static $reserve = null; /** * Shutdown handler: record a fatal error that killed the tick. */ public static function on_shutdown(): void { $active = self::$active; if ( null === $active ) { return; } self::$active = null; self::$reserve = null; // Give the handler some memory back after an out-of-memory error. $err = error_get_last(); if ( ! is_array( $err ) || ! in_array( (int) $err['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) { return; } // The fatal error may have interrupted a query; reconnect so the failure can be recorded. global $wpdb; if ( isset( $wpdb ) && is_object( $wpdb ) && method_exists( $wpdb, 'close' ) && method_exists( $wpdb, 'db_connect' ) ) { $wpdb->close(); $wpdb->db_connect( false ); } $id = (int) $active['id']; $repo = new Job_Repository(); $job = $repo->get( $id ); $phase = is_array( $job ) && ! empty( $job['phase'] ) ? (string) $job['phase'] : (string) $active['phase']; $msg = Job_Error::describe_fatal( $err, $phase ); try { Logger::error( $id, $msg ); $repo->update_progress( $id, array( 'status' => Job_States::STATUS_FAILED, 'error_message' => $msg, 'finished' => true, ) ); Job_Lock::release( $id, (string) $active['token'] ); if ( is_array( $job ) ) { $job['status'] = Job_States::STATUS_FAILED; $job['error_message'] = $msg; $job['phase'] = $phase; if ( Job_States::TYPE_RESTORE === (string) $active['type'] ) { // Leave the site usable: drop maintenance mode (files may need a manual restore). \BRVerify\Restore\Maintenance::exit(); } Failure_Mail::maybe_send( $id, $job ); } } catch ( \Throwable $e ) { unset( $e ); } } /** * Constructor. */ public function __construct( ?Job_Repository $repo = null ) { $this->repo = $repo ?? new Job_Repository(); } /** * Create a pending backup job (does not run ticks). * * @param array $config Config. * @return array{ok:bool,job_id?:int,error?:string} */ public function queue_backup( array $config = array() ): array { if ( is_multisite() ) { return array( 'ok' => false, 'error' => __( 'Multisite is not supported.', '007aj-backup-restore-verifier' ), ); } if ( ! Working_Dir::ensure() ) { return array( 'ok' => false, 'error' => __( 'Unable to prepare working directory.', '007aj-backup-restore-verifier' ), ); } if ( $this->repo->has_active_heavy_job() ) { return array( 'ok' => false, 'error' => __( 'Another backup or restore job is already in progress.', '007aj-backup-restore-verifier' ), ); } $defaults = array( 'components' => array( 'database' => true, 'plugins' => true, 'themes' => true, 'uploads' => true, ), 'adapter' => 'local', // legacy single-adapter hint 'destinations' => Settings::backup_destinations(), ); $config = array_merge( $defaults, $config ); // Ensure destinations always present after merge (caller may omit). if ( empty( $config['destinations'] ) || ! is_array( $config['destinations'] ) ) { $config['destinations'] = Settings::backup_destinations(); } // Keep legacy adapter key aligned with first destination. $config['adapter'] = (string) ( $config['destinations'][0] ?? 'local' ); // Free default is full backups; the extended edition may set backup_mode=incremental. if ( empty( $config['backup_mode'] ) ) { $config['backup_mode'] = 'full'; } /** * Filter backup job config before create (mode, chain parents, etc.). * * @param array $config Job config. */ $filtered_cfg = apply_filters( 'brverify_backup_job_config', $config ); if ( is_array( $filtered_cfg ) ) { $config = $filtered_cfg; } if ( empty( $config['backup_mode'] ) || 'incremental' !== (string) $config['backup_mode'] ) { $config['backup_mode'] = 'full'; } $job_id = $this->repo->create( Job_States::TYPE_BACKUP, $config ); if ( $job_id <= 0 ) { return array( 'ok' => false, 'error' => __( 'Failed to create backup job.', '007aj-backup-restore-verifier' ), ); } Logger::info( $job_id, 'Backup job queued.' ); return array( 'ok' => true, 'job_id' => $job_id, ); } /** * Create a pending restore job from a local artifact. * * @param array $config Must include artifact_id; optional components / force_fail_after. * @return array{ok:bool,job_id?:int,error?:string} */ public function queue_restore( array $config = array() ): array { if ( is_multisite() ) { return array( 'ok' => false, 'error' => __( 'Multisite is not supported.', '007aj-backup-restore-verifier' ), ); } if ( ! Working_Dir::ensure() ) { return array( 'ok' => false, 'error' => __( 'Unable to prepare working directory.', '007aj-backup-restore-verifier' ), ); } if ( $this->repo->has_active_heavy_job() ) { return array( 'ok' => false, 'error' => __( 'Another backup or restore job is already in progress.', '007aj-backup-restore-verifier' ), ); } $artifact_id = (int) ( $config['artifact_id'] ?? 0 ); if ( $artifact_id <= 0 ) { return array( 'ok' => false, 'error' => __( 'Missing artifact_id for restore.', '007aj-backup-restore-verifier' ), ); } $art = $this->repo->get_artifact( $artifact_id ); if ( null === $art ) { return array( 'ok' => false, 'error' => __( 'Artifact not found.', '007aj-backup-restore-verifier' ), ); } $defaults = array( 'artifact_id' => $artifact_id, 'components' => array( 'database' => true, 'plugins' => true, 'themes' => true, 'uploads' => true, ), ); $config = array_merge( $defaults, $config ); $config['artifact_id'] = $artifact_id; $job_id = $this->repo->create( Job_States::TYPE_RESTORE, $config ); if ( $job_id <= 0 ) { return array( 'ok' => false, 'error' => __( 'Failed to create restore job.', '007aj-backup-restore-verifier' ), ); } // Start restore at validate phase. $this->repo->update_progress( $job_id, array( 'phase' => Job_States::PHASE_VALIDATE, ) ); Logger::info( $job_id, sprintf( 'Restore job queued for artifact #%d.', $artifact_id ) ); return array( 'ok' => true, 'job_id' => $job_id, ); } /** * Claim and advance one tick for a job (or next due job). * * @param int|null $job_id Optional job id. * @return array */ public function tick( ?int $job_id = null ): array { $token = $this->locker_token(); $lock = new Job_Tick_Lock(); $busy = array( 'ok' => false, 'job_id' => (int) $job_id, 'status' => 'locked', 'error' => __( 'Another request is already advancing this job; it will continue shortly.', '007aj-backup-restore-verifier' ), ); if ( null !== $job_id ) { // Take the per-job lock before reading the job, so the cursor we read is the last committed one. if ( ! $lock->acquire( (int) $job_id ) ) { return $busy; } $claimed = $this->repo->claim( $token, $job_id ); } else { $claimed = $this->repo->claim( $token, null ); if ( null !== $claimed ) { if ( ! $lock->acquire( (int) $claimed['id'] ) ) { $busy['job_id'] = (int) $claimed['id']; return $busy; } // Re-read under the lock: another request may have committed a tick meanwhile. $claimed = $this->repo->get( (int) $claimed['id'] ); if ( ! is_array( $claimed ) || ! in_array( (string) $claimed['status'], array( Job_States::STATUS_PENDING, Job_States::STATUS_RUNNING ), true ) ) { $claimed = null; } } } if ( null === $claimed ) { $lock->release(); return array( 'ok' => false, 'status' => 'idle', 'error' => __( 'No claimable job.', '007aj-backup-restore-verifier' ), ); } $id = (int) $claimed['id']; try { return $this->tick_locked( $claimed, $token ); } finally { $lock->release(); $fresh = $this->repo->get( $id ); if ( is_array( $fresh ) && ! in_array( (string) $fresh['status'], array( Job_States::STATUS_PENDING, Job_States::STATUS_RUNNING ), true ) ) { Job_Tick_Lock::forget( $id ); } } } /** * Run one tick of a claimed job while the per-job tick lock is held. * * @param array $claimed Claimed job row (read under the lock). * @param string $token Locker token. * @return array */ private function tick_locked( array $claimed, string $token ): array { $id = (int) $claimed['id']; if ( ! Job_Lock::acquire( $id, $token ) ) { return array( 'ok' => false, 'job_id' => $id, 'status' => 'locked', 'error' => __( 'Global lock held by another worker.', '007aj-backup-restore-verifier' ), ); } $driver = $this->driver_for( (string) $claimed['type'] ); if ( null === $driver ) { $this->repo->update_progress( $id, array( 'status' => Job_States::STATUS_FAILED, 'error_message' => __( 'No driver for job type.', '007aj-backup-restore-verifier' ), 'finished' => true, ) ); Job_Lock::release( $id, $token ); return array( 'ok' => false, 'job_id' => $id, 'status' => Job_States::STATUS_FAILED, 'error' => __( 'No driver for job type.', '007aj-backup-restore-verifier' ), ); } // Budget the tick from the configured limit before raising it as a safety net. Tick_Budget::start(); if ( function_exists( 'set_time_limit' ) ) { // Some hosts disable it. @set_time_limit( (int) max( 60, (int) ceil( Tick_Budget::seconds() * 3 ) ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged } if ( function_exists( 'wp_raise_memory_limit' ) ) { wp_raise_memory_limit( 'admin' ); } // A PHP fatal error (timeout, memory) never reaches the driver's catch block: // record it on the job so the real cause is visible. self::$active = array( 'id' => $id, 'phase' => (string) ( $claimed['phase'] ?? '' ), 'type' => (string) $claimed['type'], 'token' => $token, ); self::$reserve = str_repeat( ' ', 1048576 ); if ( ! self::$shutdown_registered ) { register_shutdown_function( array( self::class, 'on_shutdown' ) ); self::$shutdown_registered = true; } $phase_before = (string) ( $claimed['phase'] ?? '' ); try { $result = $driver->tick( $claimed ); } catch ( \Throwable $e ) { $result = array( 'status' => Job_States::STATUS_FAILED, 'error' => Job_Error::describe( $e, $phase_before ), ); } if ( Job_States::TYPE_RESTORE === (string) $claimed['type'] && function_exists( 'wp_cache_flush' ) ) { // A restore replaces the options table underneath WordPress. Without this, a stale // in-process/persistent options cache keeps a lock row the database no longer has and // the next tick reports "Global lock held by another worker" (found on real MySQL). wp_cache_flush(); } Job_Lock::heartbeat( $id, $token ); $this->repo->heartbeat( $id ); $status = (string) ( $result['status'] ?? Job_States::STATUS_RUNNING ); $fields = array( 'status' => $status, 'heartbeat' => true, ); if ( isset( $result['phase'] ) ) { $fields['phase'] = (string) $result['phase']; } if ( isset( $result['progress_pct'] ) ) { $fields['progress_pct'] = (int) $result['progress_pct']; } if ( isset( $result['cursor'] ) && is_array( $result['cursor'] ) ) { $fields['cursor'] = $result['cursor']; } if ( isset( $result['result'] ) && is_array( $result['result'] ) ) { $fields['result'] = $result['result']; } if ( ! empty( $result['error'] ) ) { $fields['error_message'] = Job_Error::with_phase( (string) $result['error'], $phase_before ); Logger::error( $id, $fields['error_message'] ); } if ( in_array( $status, array( Job_States::STATUS_COMPLETED, Job_States::STATUS_FAILED, Job_States::STATUS_CANCELLED ), true ) ) { $fields['finished'] = true; } try { $this->repo->update_progress( $id, $fields ); } catch ( \Throwable $e ) { // The cursor guard (or the database) refused the update: fail with the reason. $msg = Job_Error::with_phase( $e->getMessage(), (string) ( $fields['phase'] ?? $phase_before ) ); Logger::error( $id, $msg ); $status = Job_States::STATUS_FAILED; $fields = array( 'status' => $status, 'error_message' => $msg, 'finished' => true, ); try { $this->repo->update_progress( $id, $fields ); } catch ( \Throwable $e2 ) { Logger::error( $id, 'Unable to record job failure: ' . $e2->getMessage() ); } } self::$active = null; self::$reserve = null; if ( ! empty( $fields['finished'] ) ) { Job_Lock::release( $id, $token ); } // Retention prune on successful backup; failure email on failed jobs. if ( ! empty( $fields['finished'] ) ) { $this->on_job_finished( $id, $status, (string) $claimed['type'] ); } $fresh = $this->repo->get( $id ); return array( 'ok' => true, 'job_id' => $id, 'status' => $status, 'phase' => $fresh['phase'] ?? ( $fields['phase'] ?? '' ), 'progress_pct' => (int) ( $fresh['progress_pct'] ?? 0 ), 'error' => $fresh['error_message'] ?? ( $result['error'] ?? null ), 'result' => $fresh['result'] ?? array(), ); } /** * Run ticks until completed/failed or max ticks (for CLI smoke). * * @param int $job_id Job id. * @param int $max_ticks Safety cap. * @return array */ public function run_until_done( int $job_id, int $max_ticks = 500 ): array { $last = array(); for ( $i = 0; $i < $max_ticks; $i++ ) { $last = $this->tick( $job_id ); $status = (string) ( $last['status'] ?? '' ); if ( in_array( $status, array( Job_States::STATUS_COMPLETED, Job_States::STATUS_FAILED, Job_States::STATUS_CANCELLED, 'idle', 'locked' ), true ) ) { if ( 'idle' === $status || 'locked' === $status ) { $job = $this->repo->get( $job_id ); if ( $job && in_array( (string) $job['status'], array( Job_States::STATUS_COMPLETED, Job_States::STATUS_FAILED, Job_States::STATUS_CANCELLED ), true ) ) { return array( 'ok' => Job_States::STATUS_COMPLETED === $job['status'], 'job_id' => $job_id, 'status' => $job['status'], 'phase' => $job['phase'], 'progress_pct' => (int) $job['progress_pct'], 'error' => $job['error_message'] ?? null, 'result' => $job['result'] ?? array(), 'ticks' => $i + 1, ); } } if ( 'locked' === $status ) { // Another request holds this job's tick lock: wait for it instead of giving up. usleep( 250000 ); continue; } $last['ticks'] = $i + 1; return $last; } } $last['ok'] = false; $last['error'] = __( 'Job exceeded maximum tick limit.', '007aj-backup-restore-verifier' ); $last['ticks'] = $max_ticks; return $last; } /** * Cron wakeup: advance one due job. */ public function cron_tick(): void { $this->tick( null ); } /** * Driver factory. * * @param string $type Job type. */ private function driver_for( string $type ): ?JobDriverInterface { if ( Job_States::TYPE_BACKUP === $type ) { return new Backup_Driver(); } if ( Job_States::TYPE_RESTORE === $type ) { return new Restore_Driver(); } return null; } /** * Post-finish hooks: retention (backup completed) + failure mail. * * @param int $job_id Job id. * @param string $status Final status. * @param string $type Job type. */ private function on_job_finished( int $job_id, string $status, string $type ): void { /** * Fires when any job reaches a terminal state (Phase L; add-on cleanup hook). * * @param int $job_id Job id. * @param string $status completed|failed|cancelled. * @param string $type Job type. */ do_action( 'brverify_job_finished', $job_id, $status, $type ); if ( Job_States::STATUS_COMPLETED === $status && Job_States::TYPE_BACKUP === $type ) { ( new Retention() )->after_backup_success( $job_id ); } if ( Job_States::STATUS_FAILED === $status ) { $job = $this->repo->get( $job_id ); if ( null !== $job ) { Failure_Mail::maybe_send( $job_id, $job ); } } } /** * Unique locker identity. */ private function locker_token(): string { $host = function_exists( 'gethostname' ) ? (string) gethostname() : 'host'; $pid = function_exists( 'getmypid' ) ? (int) getmypid() : 0; return $host . '|' . $pid . '|' . wp_generate_password( 8, false, false ); } }