purgeExpired( $results_cutoff ); $source_cache_deleted = ( new SourceCacheStore() )->purgeExpired( $source_cache_cutoff ); $snapshots_deleted = $this->pruneSnapshots( $snapshot_cutoff ); return array( 'results' => $results_deleted, 'sourceCache' => $source_cache_deleted, 'snapshots' => $snapshots_deleted, ); } catch ( \Exception $e ) { return null; } finally { delete_transient( self::LOCK_TRANSIENT ); } } /** * Static cron callback. */ public static function cron_callback(): void { try { ( new self() )->run(); } catch ( \Throwable $e ) { } } /** * Prune unreferenced execution snapshots older than $older_than. * * @param string $older_than MySQL UTC datetime cutoff. * @return int Rows deleted. */ private function pruneSnapshots( string $older_than ): int { $store = new ExecutionSnapshotStore(); $protected = $this->protectedSnapshotIds( $store ); if ( null === $protected ) { return 0; } $ids = $store->prunable( $older_than, $protected, self::SNAPSHOT_PRUNE_LIMIT ); $deleted = 0; foreach ( $ids as $id ) { if ( $store->deleteIfUnusedSince( $id, $older_than ) ) { $deleted++; } } return $deleted; } /** * Every snapshot id that a currently non-terminal automation job's run * pin still needs, plus the definition snapshots that run pin itself * references. * * 'completed' is treated as the only truly terminal automation job * status: AutomationJob::VALID_TRANSITIONS has no transition out of * 'completed', while 'failed' can still transition back to 'ready' via * retry. Erring toward protecting a resumable job's pin rather than * deleting a snapshot it might still need. * * Pages through every non-terminal job, PROTECTED_JOB_SCAN_LIMIT rows at * a time, rather than one bounded SELECT: a job this method never sees * is a job whose run pin never enters `$protected`, and * pruneSnapshots() would then permanently delete a snapshot that job * still needs — unlike pruneSnapshots()'s own bounded delete, where any * leftover row is simply caught by the next tick, a missed protected- * set entry is not self-healing: the snapshot is gone before the next * tick ever runs. * * @param ExecutionSnapshotStore $store Snapshot store (reused for the lookups below). * @return int[]|null Protected snapshot ids, or null when a page query failed and the scan could not complete. */ private function protectedSnapshotIds( ExecutionSnapshotStore $store ): ?array { global $wpdb; $table = $wpdb->prefix . 'aipt_automation_jobs'; $protected = array(); $last_id = 0; do { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, settings FROM {$table} WHERE status != 'completed' AND id > %d ORDER BY id ASC LIMIT %d", $last_id, self::PROTECTED_JOB_SCAN_LIMIT ), ARRAY_A ); if ( '' !== (string) $wpdb->last_error ) { error_log( '[AIPT] BlueprintCleanup: protected-job scan failed (' . $wpdb->last_error . '); skipping the snapshot prune for this tick.' ); return null; } if ( ! is_array( $rows ) || empty( $rows ) ) { break; } foreach ( $rows as $row ) { $last_id = max( $last_id, (int) ( $row['id'] ?? 0 ) ); $settings = json_decode( (string) ( $row['settings'] ?? '' ), true ); if ( ! is_array( $settings ) || empty( $settings['blueprint'] ) || ! is_array( $settings['blueprint'] ) ) { continue; } $pin = $settings['blueprint']; $run_pin_id = isset( $pin['runPinId'] ) ? (int) $pin['runPinId'] : 0; $run_pin_hash = isset( $pin['runPinHash'] ) ? (string) $pin['runPinHash'] : ''; $run_row = $run_pin_id > 0 ? $store->get( $run_pin_id ) : null; if ( '' !== (string) $wpdb->last_error ) { error_log( '[AIPT] BlueprintCleanup: run-pin lookup failed (' . $wpdb->last_error . '); skipping the snapshot prune for this tick.' ); return null; } if ( null === $run_row && '' !== $run_pin_hash ) { $run_row = $store->getByHash( $run_pin_hash ); if ( '' !== (string) $wpdb->last_error ) { error_log( '[AIPT] BlueprintCleanup: run-pin lookup failed (' . $wpdb->last_error . '); skipping the snapshot prune for this tick.' ); return null; } } if ( null === $run_row ) { continue; } $protected[] = (int) $run_row['id']; $payload = is_array( $run_row['payload'] ) ? $run_row['payload'] : array(); if ( isset( $payload['definitionSnapshots'] ) && is_array( $payload['definitionSnapshots'] ) ) { foreach ( $payload['definitionSnapshots'] as $definition_snapshot_id ) { $protected[] = (int) $definition_snapshot_id; } } if ( isset( $payload['builtinDefault']['definitionSnapshotId'] ) ) { $protected[] = (int) $payload['builtinDefault']['definitionSnapshotId']; } } } while ( count( $rows ) === self::PROTECTED_JOB_SCAN_LIMIT ); return array_values( array_unique( array_filter( $protected ) ) ); } }