prefix . 'acpimg_jobs'; $charset = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS {$table} ( id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, image_id BIGINT(20) UNSIGNED NOT NULL, feature VARCHAR(64) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'pending', prediction_id VARCHAR(128) DEFAULT NULL, provider VARCHAR(32) DEFAULT NULL, cost_estimate DECIMAL(10,4) DEFAULT 0, payload LONGTEXT DEFAULT NULL, result LONGTEXT DEFAULT NULL, error TEXT DEFAULT NULL, started_at DATETIME DEFAULT NULL, completed_at DATETIME DEFAULT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY status (status), KEY feature (feature) ) {$charset};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared dbDelta($sql); } public static function enqueue($image_id, $feature, $payload = []) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->insert( $wpdb->prefix . 'acpimg_jobs', [ 'image_id' => (int) $image_id, 'feature' => sanitize_key($feature), 'status' => 'pending', 'payload' => wp_json_encode($payload), 'created_at' => current_time('mysql', true), ], ['%d', '%s', '%s', '%s', '%s'] ); return $wpdb->insert_id; } // Insert a row that has already been started elsewhere (manual run that ran // out of its bounded-poll window) so the cron can finish it. public static function enqueue_awaiting($image_id, $feature, $prediction_id, $payload = []) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->insert( $wpdb->prefix . 'acpimg_jobs', [ 'image_id' => (int) $image_id, 'feature' => sanitize_key($feature), 'status' => 'awaiting', 'prediction_id' => (string) $prediction_id, 'payload' => wp_json_encode($payload), 'started_at' => current_time('mysql', true), 'created_at' => current_time('mysql', true), ], ['%d', '%s', '%s', '%s', '%s', '%s', '%s'] ); return $wpdb->insert_id; } public static function pending_count() { global $wpdb; // %i is the WP 6.2+ identifier placeholder; prepare() is inlined into the // $wpdb call so PluginCheck's NotPrepared sniff can see it. // phpcs:ignore WordPress.DB.DirectDatabaseQuery return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status = %s", $wpdb->prefix . 'acpimg_jobs', 'pending' ) ); } // Two-phase async drain. Per tick: START up to $limit pending jobs (create a // prediction, move to 'awaiting'), then POLL up to $limit awaiting jobs // (finish or fail). A job started this tick is polled on a later tick. public static function drain($limit = 5) { self::start_pending($limit); self::poll_awaiting($limit); } private static function start_pending($limit) { global $wpdb; $table = $wpdb->prefix . 'acpimg_jobs'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $rows = $wpdb->get_results($wpdb->prepare( "SELECT * FROM %i WHERE status = %s ORDER BY id ASC LIMIT %d", $table, 'pending', (int) $limit )); if (!$rows) return; foreach ($rows as $row) { $payload = $row->payload ? json_decode($row->payload, true) : []; $tier = isset($payload['tier']) ? $payload['tier'] : 'default'; $pid = Acpimg_Managed::start($row->feature, (int) $row->image_id, $payload, $tier); if (is_wp_error($pid)) { self::mark($row->id, 'failed', ['error' => $pid->get_error_message()]); continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->update($table, [ 'status' => 'awaiting', 'prediction_id' => (string) $pid, 'started_at' => current_time('mysql', true), ], ['id' => (int) $row->id], ['%s', '%s', '%s'], ['%d']); } } private static function poll_awaiting($limit) { global $wpdb; $table = $wpdb->prefix . 'acpimg_jobs'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $rows = $wpdb->get_results($wpdb->prepare( "SELECT * FROM %i WHERE status = %s ORDER BY id ASC LIMIT %d", $table, 'awaiting', (int) $limit )); if (!$rows) return; foreach ($rows as $row) { $st = Acpimg_Managed::poll($row->prediction_id); if (is_wp_error($st)) continue; // transient, retry next tick. $status = isset($st['status']) ? $st['status'] : ''; if ($status === 'succeeded') { $payload = $row->payload ? json_decode($row->payload, true) : []; $tier = isset($payload['tier']) ? $payload['tier'] : 'default'; try { $res = Acpimg_Managed::finish($row->feature, (int) $row->image_id, isset($st['output']) ? $st['output'] : null, $tier, $payload); if (is_wp_error($res)) { self::mark($row->id, 'failed', ['error' => $res->get_error_message()]); } else { self::mark($row->id, 'done', ['result' => $res ? wp_json_encode($res) : null]); } } catch (\Throwable $e) { self::mark($row->id, 'failed', ['error' => $e->getMessage()]); } } elseif ($status === 'failed') { self::mark($row->id, 'failed', ['error' => isset($st['error']) ? $st['error'] : 'prediction failed']); } // else still processing, leave for the next tick. } } private static function mark($id, $status, $extra = []) { global $wpdb; $data = array_merge(['status' => $status, 'completed_at' => current_time('mysql', true)], $extra); $formats = array_fill(0, count($data), '%s'); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->update($wpdb->prefix . 'acpimg_jobs', $data, ['id' => (int) $id], $formats, ['%d']); } }