Making WordPress.org

Changeset 10972


Ignore:
Timestamp:
05/14/2021 12:49:21 AM (6 years ago)
Author:
dd32
Message:

Trac: GitHub PRs: Increase API timeouts and add retries to the Trac API.

This is an attempt at avoiding API errors that occur when trac is unavailable, and where there's the potential that a Trac comment might not be added when required.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/trac/pr/class-trac.php

    r10000 r10972  
    4242
    4343                try {
     44                        // Retrying this is safe, as the `_ts` parameter must be set correctly for it to succeed.
    4445                        $this->api( 'ticket.update', [ $id, $comment, $attr, (bool) $notify, $author, $when ] );
    4546
    4647                        return true;
    4748                } catch( \Exception $e ) {
    48                         // Try once more.. `_ts` may have been outdated.
     49                        // `_ts` may have been outdated, update and try again.
    4950                        if ( isset( $get ) && $attr['_ts'] === $get['_ts'] ) {
    5051                                $get = $this->get( $id ); // refetch the ticket.
    51                                 $attr['_ts'] = $get['_ts'];
     52                                if ( $attr['_ts'] === $get['_ts'] ) {
     53                                        // Didn't change, api already retried, throw it.
     54                                        throw $e;
     55                                }
     56
    5257                                try {
     58                                        $attr['_ts'] = $get['_ts'];
    5359
    5460                                        $this->api( 'ticket.update', [ $id, $comment, $attr, (bool) $notify, $author, $when ] );
    … …  
    5864                                }
    5965                        }
     66
    6067                        return false;
    6168                }
    … …  
    8996         * Fetch/POST a Trac JSONRPC endpoint.
    9097         */
    91         public function api( $method, $params ) {
     98        public function api( $method, $params, $retry = 3 ) {
     99                $tries = 0;
     100                $retry = (int) $retry;
     101
     102                do {
     103                        try {
     104                                return $this->_api( $method, $params );
     105                        } catch( \Exception $e ) {
     106                                // Retry with a short delay
     107                                sleep( 1 );
     108                        }
     109                } while ( ++$tries < $retry );
     110
     111                // If we make it this far, all retries have failed, throw that exception.
     112                throw $e;
     113        }
     114
     115        protected function _api( $method, $params ) {
    92116                $context = stream_context_create( [ 'http' => [
    93117                        'method'        => 'POST',
    94118                        'user_agent'    => 'WordPress.org Trac; trac.WordPress.org',
    95119                        'max_redirects' => 0,
    96                         'timeout'       => 5,
     120                        'timeout'       => 30,
    97121                        'ignore_errors' => true,
    98122                        'header'        =>
    … …  
    140164                        'user_agent'    => 'WordPress.org Trac; trac.WordPress.org',
    141165                        'max_redirects' => 0,
    142                         'timeout'       => 5,
     166                        'timeout'       => 30,
    143167                        'ignore_errors' => true,
    144168                        'header'        =>  [
Note: See TracChangeset for help on using the changeset viewer.