Changeset 3676770
- Timestamp:
- 09/01/2026 07:56:51 PM (3 months ago)
- Location:
- savoy-accessible/trunk
- Files:
-
- 5 edited
-
assets/js/admin.js (modified) (3 diffs)
-
includes/class-admin.php (modified) (8 diffs)
-
includes/class-api-client.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
-
savoy-accessible.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
savoy-accessible/trunk/assets/js/admin.js
r3574799 r3676770 272 272 } 273 273 274 // Poll status every 3s while any row is pending or processing. Stops as 275 // soon as no non-ready rows remain (or after enough idle ticks to avoid 276 // running forever on a forgotten tab). 274 // Poll status once a minute while any row is pending or processing. Stops 275 // as soon as no non-ready rows remain (or after enough idle ticks to avoid 276 // running forever on a forgotten tab). Each tick only checks/reports a 277 // small batch server-side (see ajax_refresh_statuses), so this stays cheap 278 // even on a site with a large backlog of documents. 277 279 function hasNonReadyRows() { 278 280 // Skip limit_reached (stays until billing reset) and error (needs manual action) … … 282 284 if ($refreshBtn.length && hasNonReadyRows()) { 283 285 var _savoyPollTicks = 0; 284 var _savoyPollMax = 200; // 200 * 3s = 10 minsafety cap286 var _savoyPollMax = 180; // 180 * 60s = 3 hour safety cap 285 287 var _savoyPollTimer = setInterval(function () { 286 288 _savoyPollTicks++; … … 293 295 nonce: savoyAccessible.nonce 294 296 }).done(function (response) { 295 if (response && response.success && response.data && response.data. updated > 0) {297 if (response && response.success && response.data && response.data.changed > 0) { 296 298 clearInterval(_savoyPollTimer); 297 299 window.location.reload(); 298 300 } 299 301 }); 300 }, 3000);302 }, 60000); 301 303 } 302 304 -
savoy-accessible/trunk/includes/class-admin.php
r3670919 r3676770 1039 1039 } 1040 1040 1041 // Drain a bit more of the pending-report backlog on every status-refresh 1042 // tick too, so a big initial scan finishes reporting within a few minutes 1043 // without needing another page action. report_pending_pdfs() itself only 1044 // sends up to 10 at a time. 1045 Savoy_Accessible::get_instance()->report_pending_pdfs(); 1046 1041 1047 // Get documents that are not yet ready, OR ready docs missing a WP post. 1042 1048 // Also include limit_reached to re-check after billing cycle rolls over. … … 1044 1050 // reprocess don't stay stuck; also include all ready docs (not just 1045 1051 // those missing wp_post_id) so savoy_doc_id updates get picked up. 1052 // 1053 // Only check a small batch per call (oldest-checked first) -- a site 1054 // with 100+ documents checking all of them in one AJAX call is what was 1055 // bogging the server down. The rest get picked up on the next poll tick. 1046 1056 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table; no WP meta API equivalent. 1047 1057 $docs = $wpdb->get_results( 1048 1058 $wpdb->prepare( 1049 'SELECT id, url FROM %i WHERE status IN (%s, %s, %s, %s, %s)',1059 'SELECT id, url, status, savoy_doc_id, html_version, wp_post_id FROM %i WHERE status IN (%s, %s, %s, %s, %s) ORDER BY updated_at ASC LIMIT %d', 1050 1060 $table_name, 1051 1061 'pending', … … 1053 1063 'limit_reached', 1054 1064 'not_found', 1055 'ready' 1065 'ready', 1066 10 1056 1067 ) 1057 1068 ); 1058 1069 1070 if ( empty( $docs ) ) { 1071 wp_send_json_success( 1072 array( 1073 'updated' => 0, 1074 'changed' => 0, 1075 'message' => __( 'No documents to refresh.', 'savoy-accessible' ), 1076 ) 1077 ); 1078 } 1079 1080 // One request for the whole batch instead of one request per document. 1081 $urls_by_id = wp_list_pluck( $docs, 'url', 'id' ); 1082 $results = $client->check_status_bulk( array_values( $urls_by_id ) ); 1083 1084 if ( is_wp_error( $results ) ) { 1085 wp_send_json_error( array( 'message' => $results->get_error_message() ) ); 1086 } 1087 1059 1088 $updated = 0; 1060 1061 foreach ( $docs as $doc ) { 1062 // Load current local state to detect changes (reprocess, etc.) 1063 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table; no WP meta API equivalent. 1064 $local = $wpdb->get_row( 1065 $wpdb->prepare( 'SELECT status, savoy_doc_id, html_version, wp_post_id FROM %i WHERE id = %d', $table_name, $doc->id ) 1066 ); 1067 $status_result = $client->check_status( $doc->url ); 1068 1069 if ( ! is_wp_error( $status_result ) && isset( $status_result['status'] ) ) { 1089 $changed = 0; 1090 1091 foreach ( $docs as $local ) { 1092 $status_result = isset( $results[ $local->url ] ) ? $results[ $local->url ] : null; 1093 1094 if ( $status_result && isset( $status_result['status'] ) ) { 1070 1095 $new_status = sanitize_text_field( $status_result['status'] ); 1071 1096 $server_doc_id = isset( $status_result['doc_id'] ) ? sanitize_text_field( $status_result['doc_id'] ) : null; … … 1077 1102 // may be mid-ingest, the URL may have had an encoding glitch, etc. Only 1078 1103 // persist 'not_found' if we were already in error (nothing to lose). 1079 if ( 'not_found' === $new_status && $local &&'error' !== $local->status ) {1104 if ( 'not_found' === $new_status && 'error' !== $local->status ) { 1080 1105 continue; 1081 1106 } … … 1108 1133 $table_name, 1109 1134 $update_data, 1110 array( 'id' => $ doc->id ),1135 array( 'id' => $local->id ), 1111 1136 $update_formats, 1112 1137 array( '%d' ) … … 1120 1145 // 4. html_version incremented (HTML was re-generated server-side) 1121 1146 if ( 'ready' === $new_status ) { 1122 $local_html_version = isset( $local->html_version ) ? (int) $local->html_version : 0;1147 $local_html_version = (int) $local->html_version; 1123 1148 $needs_sync = false; 1124 1149 if ( empty( $local->wp_post_id ) ) { 1125 1150 $needs_sync = true; 1126 } elseif ( $ local && $server_doc_id && $local->savoy_doc_id !== $server_doc_id ) {1151 } elseif ( $server_doc_id && $local->savoy_doc_id !== $server_doc_id ) { 1127 1152 $needs_sync = true; // Reprocess detected 1128 } elseif ( $local && $local->status !== 'ready') {1153 } elseif ( 'ready' !== $local->status ) { 1129 1154 $needs_sync = true; // Just transitioned to ready 1130 1155 } elseif ( $server_html_version > $local_html_version ) { … … 1133 1158 1134 1159 if ( $needs_sync ) { 1135 $html_result = $client->fetch_accessible_html( $ doc->url );1160 $html_result = $client->fetch_accessible_html( $local->url ); 1136 1161 if ( ! is_wp_error( $html_result ) && ! empty( $html_result['html'] ) ) { 1137 $title = ! empty( $html_result['title'] ) ? $html_result['title'] : basename( $ doc->url );1138 Savoy_Rewriter::create_or_update_post( $ doc->url, $title, $html_result['html'] );1162 $title = ! empty( $html_result['title'] ) ? $html_result['title'] : basename( $local->url ); 1163 Savoy_Rewriter::create_or_update_post( $local->url, $title, $html_result['html'] ); 1139 1164 } 1140 1165 } 1141 1166 } 1142 1167 1168 // "Changed" (as opposed to just successfully re-checked) is what the 1169 // admin page actually needs to reload for -- reloading on every poll 1170 // tick just because a still-processing doc reconfirmed its status was 1171 // itself a source of load (a full wp-admin reload every tick). 1172 if ( $new_status !== $local->status ) { 1173 $changed++; 1174 } 1175 1143 1176 $updated++; 1144 1177 } … … 1148 1181 array( 1149 1182 'updated' => $updated, 1183 'changed' => $changed, 1150 1184 'message' => sprintf( 1151 1185 /* translators: %d: number of documents updated */ -
savoy-accessible/trunk/includes/class-api-client.php
r3670919 r3676770 152 152 153 153 /** 154 * Check the processing status of multiple documents in a single request. 155 * 156 * @param array $urls Array of PDF URLs (max 25; caller should chunk larger batches). 157 * @return array|WP_Error Array of url => status array on success, WP_Error on failure. 158 */ 159 public function check_status_bulk( $urls ) { 160 if ( empty( $this->api_key ) ) { 161 return new WP_Error( 'no_key', __( 'No API key configured.', 'savoy-accessible' ) ); 162 } 163 164 if ( empty( $urls ) ) { 165 return array(); 166 } 167 168 $response = wp_remote_post( 169 $this->api_base . '/v1/plugin/doc-status-bulk', 170 array( 171 'headers' => array( 172 'X-Api-Key' => $this->api_key, 173 'Content-Type' => 'application/json', 174 ), 175 'body' => wp_json_encode( array( 'urls' => array_values( $urls ) ) ), 176 'timeout' => 30, 177 ) 178 ); 179 180 if ( is_wp_error( $response ) ) { 181 return $response; 182 } 183 184 $code = wp_remote_retrieve_response_code( $response ); 185 $body = json_decode( wp_remote_retrieve_body( $response ), true ); 186 187 if ( 200 !== $code ) { 188 $message = isset( $body['error'] ) ? $body['error'] : __( 'Failed to check document statuses.', 'savoy-accessible' ); 189 return new WP_Error( 'api_error', $message ); 190 } 191 192 return isset( $body['results'] ) && is_array( $body['results'] ) ? $body['results'] : array(); 193 } 194 195 /** 154 196 * Get the viewer URL for a PDF. 155 197 * -
savoy-accessible/trunk/readme.txt
r3670919 r3676770 5 5 Tested up to: 6.9 6 6 Requires PHP: 7.4 7 Stable tag: 1.0.4 57 Stable tag: 1.0.46 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 122 122 == Changelog == 123 123 124 = 1.0.46 = 125 * Fix: a site with a large backlog of PDFs (100+) could bog down under the status-check polling. The admin page now checks statuses in small batches (a single combined request instead of one per document) once a minute instead of every 3 seconds, and only reloads the page when a status actually changes. Newly discovered PDFs are now also reported for conversion in small batches instead of all at once. 126 124 127 = 1.0.45 = 125 128 * Added a tooltip on the "X / Y documents this month" usage count explaining that documents over 30 pages count as more than one conversion, so the number can run ahead of your actual document count. -
savoy-accessible/trunk/savoy-accessible.php
r3670919 r3676770 4 4 * Plugin URI: https://savoy.chat/accessible 5 5 * Description: Automatically convert PDFs on your site into WCAG-accessible HTML versions with AI-powered document chat. 6 * Version: 1.0.4 56 * Version: 1.0.46 7 7 * Author: Savoy 8 8 * Author URI: https://savoy.chat … … 20 20 } 21 21 22 define( 'SAVOY_ACCESSIBLE_VERSION', '1.0.4 5' );22 define( 'SAVOY_ACCESSIBLE_VERSION', '1.0.46' ); 23 23 define( 'SAVOY_ACCESSIBLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); 24 24 define( 'SAVOY_ACCESSIBLE_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); … … 480 480 /** 481 481 * Report pending PDFs to the API. 482 * 483 * Only sends a small batch at a time (rather than the entire backlog in 484 * one call) so a big scan -- e.g. a site with 100+ existing PDFs -- doesn't 485 * kick off a wall of concurrent conversions on the backend. Whatever is 486 * left over stays 'pending' and goes out on the next call (next scan, 487 * post save, media upload, the daily cron, or the status-refresh poll). 482 488 */ 483 489 public function report_pending_pdfs() { … … 488 494 $pending = $wpdb->get_results( 489 495 $wpdb->prepare( 490 'SELECT url FROM %i WHERE status = %s ',496 'SELECT url FROM %i WHERE status = %s ORDER BY created_at ASC LIMIT %d', 491 497 $table_name, 492 'pending' 498 'pending', 499 10 493 500 ) 494 501 );
Note:
See TracChangeset
for help on using the changeset viewer.