Plugin Directory

Changeset 3676770


Ignore:
Timestamp:
09/01/2026 07:56:51 PM (3 months ago)
Author:
peteclark3
Message:

Update to 1.0.46: throttle doc-status polling (60s, batched) and PDF report batching (10 at a time) to prevent server overload on sites with large PDF backlogs

Location:
savoy-accessible/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • savoy-accessible/trunk/assets/js/admin.js

    r3574799 r3676770  
    272272        }
    273273
    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.
    277279        function hasNonReadyRows() {
    278280                // Skip limit_reached (stays until billing reset) and error (needs manual action)
    … …  
    282284        if ($refreshBtn.length && hasNonReadyRows()) {
    283285                var _savoyPollTicks = 0;
    284                 var _savoyPollMax   = 200; // 200 * 3s = 10 min safety cap
     286                var _savoyPollMax   = 180; // 180 * 60s = 3 hour safety cap
    285287                var _savoyPollTimer = setInterval(function () {
    286288                        _savoyPollTicks++;
    … …  
    293295                                nonce: savoyAccessible.nonce
    294296                        }).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) {
    296298                                        clearInterval(_savoyPollTimer);
    297299                                        window.location.reload();
    298300                                }
    299301                        });
    300                 }, 3000);
     302                }, 60000);
    301303        }
    302304
  • savoy-accessible/trunk/includes/class-admin.php

    r3670919 r3676770  
    10391039                }
    10401040
     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
    10411047                // Get documents that are not yet ready, OR ready docs missing a WP post.
    10421048                // Also include limit_reached to re-check after billing cycle rolls over.
    … …  
    10441050                // reprocess don't stay stuck; also include all ready docs (not just
    10451051                // 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.
    10461056                // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table; no WP meta API equivalent.
    10471057                $docs = $wpdb->get_results(
    10481058                        $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',
    10501060                                $table_name,
    10511061                                'pending',
    … …  
    10531063                                'limit_reached',
    10541064                                'not_found',
    1055                                 'ready'
     1065                                'ready',
     1066                                10
    10561067                        )
    10571068                );
    10581069
     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
    10591088                $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'] ) ) {
    10701095                                $new_status = sanitize_text_field( $status_result['status'] );
    10711096                                $server_doc_id = isset( $status_result['doc_id'] ) ? sanitize_text_field( $status_result['doc_id'] ) : null;
    … …  
    10771102                                // may be mid-ingest, the URL may have had an encoding glitch, etc. Only
    10781103                                // 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 ) {
    10801105                                        continue;
    10811106                                }
    … …  
    11081133                                        $table_name,
    11091134                                        $update_data,
    1110                                         array( 'id' => $doc->id ),
     1135                                        array( 'id' => $local->id ),
    11111136                                        $update_formats,
    11121137                                        array( '%d' )
    … …  
    11201145                                // 4. html_version incremented (HTML was re-generated server-side)
    11211146                                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;
    11231148                                        $needs_sync = false;
    11241149                                        if ( empty( $local->wp_post_id ) ) {
    11251150                                                $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 ) {
    11271152                                                $needs_sync = true; // Reprocess detected
    1128                                         } elseif ( $local && $local->status !== 'ready' ) {
     1153                                        } elseif ( 'ready' !== $local->status ) {
    11291154                                                $needs_sync = true; // Just transitioned to ready
    11301155                                        } elseif ( $server_html_version > $local_html_version ) {
    … …  
    11331158
    11341159                                        if ( $needs_sync ) {
    1135                                                 $html_result = $client->fetch_accessible_html( $doc->url );
     1160                                                $html_result = $client->fetch_accessible_html( $local->url );
    11361161                                                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'] );
    11391164                                                }
    11401165                                        }
    11411166                                }
    11421167
     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
    11431176                                $updated++;
    11441177                        }
    … …  
    11481181                        array(
    11491182                                'updated' => $updated,
     1183                                'changed' => $changed,
    11501184                                'message' => sprintf(
    11511185                                        /* translators: %d: number of documents updated */
  • savoy-accessible/trunk/includes/class-api-client.php

    r3670919 r3676770  
    152152
    153153        /**
     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        /**
    154196         * Get the viewer URL for a PDF.
    155197         *
  • savoy-accessible/trunk/readme.txt

    r3670919 r3676770  
    55Tested up to: 6.9
    66Requires PHP: 7.4
    7 Stable tag: 1.0.45
     7Stable tag: 1.0.46
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    122122== Changelog ==
    123123
     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
    124127= 1.0.45 =
    125128* 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  
    44 * Plugin URI: https://savoy.chat/accessible
    55 * Description: Automatically convert PDFs on your site into WCAG-accessible HTML versions with AI-powered document chat.
    6  * Version: 1.0.45
     6 * Version: 1.0.46
    77 * Author: Savoy
    88 * Author URI: https://savoy.chat
    … …  
    2020}
    2121
    22 define( 'SAVOY_ACCESSIBLE_VERSION', '1.0.45' );
     22define( 'SAVOY_ACCESSIBLE_VERSION', '1.0.46' );
    2323define( 'SAVOY_ACCESSIBLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    2424define( 'SAVOY_ACCESSIBLE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    … …  
    480480        /**
    481481         * 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).
    482488         */
    483489        public function report_pending_pdfs() {
    … …  
    488494                $pending = $wpdb->get_results(
    489495                        $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',
    491497                                $table_name,
    492                                 'pending'
     498                                'pending',
     499                                10
    493500                        )
    494501                );
Note: See TracChangeset for help on using the changeset viewer.