Plugin Directory

Changeset 2588650


Ignore:
Timestamp:
08/25/2021 03:45:53 PM (5 years ago)
Author:
winrid
Message:

3.9.9 - Sync now supports sites more comments for a more reliable migration. They do not have to fit in memory during sync.

Location:
fastcomments
Files:
34 added
5 edited

Legend:

Unmodified
Added
Removed
  • fastcomments/trunk/README.txt

    r2586216 r2588650  
    44Requires at least: 4.6
    55Tested up to: 5.8
    6 Stable tag: 3.9.8
     6Stable tag: 3.9.9
    77Requires PHP: 5.2.4
    88License: GPLv2 or later
     
    8484
    8585== Changelog ==
     86
     87= 3.9.9 =
     88* Sync now supports sites more comments for a more reliable migration. They do not have to fit in memory during sync.
    8689
    8790= 3.9.8 =
  • fastcomments/trunk/core/FastCommentsIntegrationCore.php

    r2586120 r2588650  
    3838    public abstract function handleEvents($events);
    3939
    40     public abstract function getCommentCount($startFromDateTime);
    41 
    42     public abstract function getComments($startFromDateTime);
     40    public abstract function getCommentCount($afterId);
     41
     42    public abstract function getComments($afterId);
    4343
    4444    public function base64Encode($stringValue) {
     
    217217    }
    218218
    219     private function canAckLock($name, $window) {
     219    private function canAckLock($name, $windowSeconds) {
    220220        $settingName = $this->getLockName($name);
    221221        $lastTime = $this->getSettingValue($settingName);
    222222        $now = time();
    223         if ($lastTime && $now - $lastTime < $window) {
     223        if ($lastTime && $now - $lastTime < $windowSeconds) {
    224224            return false;
    225225        }
     
    238238
    239239    public function commandSendComments($token) {
     240        /**
     241         * Fetch 500 comments a time from the DB.
     242         * Split them up into chunks of 100.
     243         * If the server complains the payload is too large, recursively split the chunk by / 10.
     244         */
    240245        $this->log('debug', 'Starting to send comments');
    241         if (!$this->canAckLock("commandSendComments", 3000)) {
     246        if (!$this->canAckLock("commandSendComments", 60)) {
    242247            $this->log('debug', 'Can not send right now, waiting for previous attempt to finish.');
    243248            return;
    244249        }
    245250        $lastSendDate = $this->getSettingValue('fastcomments_stream_last_send_timestamp');
    246         $startedAt = time();
    247         $hasMore = true;
    248         if ($this->getCommentCount(0) == 0) {
    249             $this->log('debug', 'No comments to send. Telling server.');
     251        $lastSentId = $this->getSettingValue('fastcomments_stream_last_send_id');
     252        $commentCount = $this->getCommentCount($lastSentId ? $lastSentId : 0);
     253        if ($commentCount == 0) {
     254            $this->log('debug', "No comments to send. Telling server. lastSendDate=[$lastSendDate] lastSentId=[$lastSentId]");
    250255            $requestBody = json_encode(
    251256                array(
     
    259264            return;
    260265        }
    261 //        while ($hasMore && time() - $startedAt < 30 * 1000) {
    262266        $this->log('debug', 'Send comments command loop...');
    263         $getCommentsResponse = $this->getComments($lastSendDate ? $lastSendDate : 0);
     267        $getCommentsResponse = $this->getComments($lastSentId ? $lastSentId : 0);
    264268        if ($getCommentsResponse['status'] === 'success') {
    265269            $count = count($getCommentsResponse['comments']);
    266             $hasMore = $getCommentsResponse['hasMore'];
    267             $this->log('info', "Got comments to send count=[$count] hasMore=[$hasMore]");
    268             $countRemaining = $getCommentsResponse['comments'] ? count($getCommentsResponse['comments']) : 0;
     270            $this->log('info', "Got comments to send count=[$count] from totalCount=[$commentCount] lastSendDate=[$lastSendDate] lastSentId=[$lastSentId]");
     271            $countRemaining = $commentCount;
    269272            $chunkSize = 100;
    270273            if ($countRemaining > 0) {
     
    277280                    while ($chunkAttemptsRemaining > 0) {
    278281                        foreach ($dynamicChunks as $dynamicChunk) {
    279                             $lastCommentFromDateTime = strtotime($dynamicChunk[count($dynamicChunk) - 1]['date']) * 1000;
     282                            $lastComment = $dynamicChunk[count($dynamicChunk) - 1];
     283                            $lastCommentFromDateTime = strtotime($lastComment['date']) * 1000;
    280284                            $countRemainingIfSuccessful = $countRemaining - count($dynamicChunk);
    281285                            $requestBody = json_encode(
     
    292296                                    $countRemaining = $countRemainingIfSuccessful;
    293297                                    $fromDateTime = $lastCommentFromDateTime;
    294                                     $lastSendDate = $fromDateTime;
    295298                                    $this->setSettingValue('fastcomments_stream_last_send_timestamp', $fromDateTime);
     299                                    $this->setSettingValue('fastcomments_stream_last_send_id', $lastComment['externalId']);
    296300                                    if ($countRemaining <= 0) {
    297301                                        $this->setSetupDone();
    298 //                                        break;
    299302                                    }
    300303                                }
     
    311314            } else {
    312315                $this->setSetupDone();
    313 //                    break;
    314316            }
    315317        } else {
    316318            $status = $getCommentsResponse['status'];
    317319            $comments = $getCommentsResponse['comments'];
    318             $debugHasMore = $getCommentsResponse['hasMore'];
    319             $this->log('error', "Failed to get comments to send: status=[$status] comments=[$comments] hasMore=[$debugHasMore]}");
    320 //                break;
    321         }
    322 //        }
     320            $this->log('error', "Failed to get comments to send: status=[$status] comments=[$comments]");
     321        }
     322        $this->clearLock("commandSendComments");
    323323        $this->log('debug', 'Done sending comments');
    324324    }
  • fastcomments/trunk/core/FastCommentsWordPressIntegration.php

    r2586216 r2588650  
    7474        delete_option('fastcomments_stream_last_fetch_timestamp');
    7575        delete_option('fastcomments_stream_last_send_timestamp');
     76        delete_option('fastcomments_stream_last_send_id');
    7677        delete_option('fastcomments_comment_sent_count');
    7778
     
    326327                        if ($wp_comment) {
    327328                            wp_update_comment($wp_comment);
    328                             add_comment_meta($wp_comment->comment_ID, 'fastcomments_id', $eventData->comment->_id, true);
     329                            add_comment_meta($wp_comment['comment_ID'], 'fastcomments_id', $eventData->comment->_id, true);
    329330                        } else {
    330331                            $this->log('debug', "Skipping sync of $fcId - is not from the WP integration.");
     
    373374    }
    374375
    375     public function getCommentCount($startFromDateTime) {
    376 //        if (isset($startFromDateTime)) {
    377 //            $args = array(
    378 //                'date_query' => array(
    379 //                    'after' => date('c', $startFromDateTime ? $startFromDateTime / 1000 : 0)
    380 //                ),
    381 //                'count' => true
    382 //            );
    383 //            $wp_comments_count = get_comments($args);
    384 //            return $wp_comments_count;
    385 //        } else {
    386         $count_result = wp_count_comments();
    387         return $count_result ? $count_result->total_comments : 0;
    388 //        }
    389     }
    390 
    391     public function getComments($startFromDateTime) {
    392 //        $limit = 100;
    393         $args = array(
    394 //            'number' => $limit + 1,
    395             'date_query' => array(
    396                 'after' => date('c', $startFromDateTime ? $startFromDateTime / 1000 : 0),
    397                 'inclusive' => true
    398             ),
    399             'orderby' => array('comment_date', 'comment_ID'),
    400             'order' => 'ASC'
    401         );
    402         $wp_comments = get_comments($args);
    403 //        $has_more = count($wp_comments) > $limit;
    404         $has_more = false;
     376    private function getCommentQueryWhere($afterId) {
     377        // This query ensures a stable sort for pagination and allows us to paginate while not seeing the same comment twice.
     378        return "comment_ID > $afterId";
     379    }
     380
     381    public function getCommentCount($afterId) {
     382        $where = $this->getCommentQueryWhere($afterId);
     383        global $wpdb;
     384        $sql = "SELECT count(*) FROM $wpdb->comments WHERE $where";
     385        return $wpdb->get_var($sql);
     386    }
     387
     388    public function getComments($afterId) {
     389        $where = $this->getCommentQueryWhere($afterId);
     390        global $wpdb;
     391        $sql = "SELECT * FROM $wpdb->comments WHERE $where ORDER BY comment_ID ASC LIMIT 100";
     392        $query_result = $wpdb->get_results($sql);
    405393        $fc_comments = array();
    406 //        for ($i = 0; $i < min(count($wp_comments), $limit); $i++) {
    407         for ($i = 0; $i < count($wp_comments); $i++) {
    408             array_push($fc_comments, $this->wp_to_fc_comment($wp_comments[$i]));
     394        foreach ($query_result as $wp_comment_row) {
     395            $wp_comment = get_comment($wp_comment_row);
     396            if ($wp_comment) {
     397                array_push($fc_comments, $this->wp_to_fc_comment($wp_comment));
     398            } else {
     399                $this->log('warn', "Comment $wp_comment_row->comment_ID was not found from WP after fetching from raw query.");
     400            }
    409401        }
    410402        return array(
    411403            "status" => "success",
    412             "comments" => $fc_comments,
    413             "hasMore" => $has_more
     404            "comments" => $fc_comments
    414405        );
    415406    }
    416407}
     408
  • fastcomments/trunk/fastcomments-wordpress-plugin.php

    r2586216 r2588650  
    44Plugin URI: https://fastcomments.com
    55Description: Live Comments, Fast. A comment system that will delight your users and developers.
    6 Version: 3.9.8
     6Version: 3.9.9
    77Author: winrid @ FastComments
    88License: GPL-2.0+
     
    1414}
    1515
    16 $FASTCOMMENTS_VERSION = 3.98;
     16$FASTCOMMENTS_VERSION = 3.99;
    1717
    1818require_once plugin_dir_path(__FILE__) . 'admin/fastcomments-admin.php';
  • fastcomments/trunk/public/fastcomments-public.php

    r2584331 r2588650  
    9595            $sso_user['avatar'] = get_avatar_url($wp_user->ID, 95);
    9696            $sso_user['optedInNotifications'] = true;
     97            // TODO pass isAdmin/isModerator flags
    9798        }
    9899
Note: See TracChangeset for help on using the changeset viewer.