Making WordPress.org


Ignore:
Timestamp:
02/28/2017 08:29:05 PM (9 years ago)
Author:
iandunn
Message:

Official WordPress Events: Add temporary logging to debug stuck cron jobs

Sometimes this cron job gets stuck in a running state in Cavalcade. It might be that the process is dying and the status never gets set to completed or failed. Hopefully this provides some clues.

See https://github.com/humanmade/Cavalcade/issues/31

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r4871 r5017  
    5656        global $wpdb;
    5757
     58        $this->log( 'started call #' . did_action( 'owpe_prime_events_cache' ) );
     59
     60        if ( did_action( 'owpe_prime_events_cache' ) > 1 ) {
     61            $this->log( 'Successive call detected, returning early' );
     62            return;
     63        }
     64
    5865        $events = $this->fetch_upcoming_events();
     66
     67        $this->log( sprintf( 'looping through %d events', count( $events ) ) );
    5968
    6069        foreach ( $events as $event ) {
     
    92101            $wpdb->replace( self::EVENTS_TABLE, $row_values );
    93102        }
     103
     104        $this->log( 'finished job' );
    94105    }
    95106
     
    246257        }
    247258
     259        $this->log( sprintf( 'returning %d events', count( $events ) ) );
     260
    248261        return $events;
    249262    }
     
    407420                $response = $this->remote_get( $request_url );
    408421                $body     = json_decode( wp_remote_retrieve_body( $response ) );
     422
     423                $this->log( 'fetching more events' );
    409424
    410425                if ( ! empty ( $body->results ) ) {
     
    458473        }
    459474
     475        $this->log( sprintf( 'returning %d events', count( $events ) ) );
     476
    460477        return $events;
    461478    }
     
    481498
    482499        while ( '' !== $request_url ) {
     500            $this->log( 'fetching more groups' );
     501
    483502            $response = $this->remote_get( $request_url );
    484503            $body     = json_decode( wp_remote_retrieve_body( $response ) );
     
    491510            $request_url = $body->meta->next;
    492511        }
     512
     513        $this->log( sprintf( 'returning %d groups', count( $group_ids ) ) );
    493514
    494515        return $group_ids;
     
    512533
    513534        if ( ! is_wp_error( $response ) ) {
     535            $this->log( 'geocode successful' );
     536
    514537            $body = json_decode( wp_remote_retrieve_body( $response ) );
    515538
     
    517540                $address = $body->results[0];
    518541            }
     542        }
     543        else {
     544            $this->log( 'geocode failed' );
    519545        }
    520546
     
    654680        }
    655681
     682        $this->log( 'sleeping to avoid api rate limit' );
    656683        sleep( $period );
     684    }
     685
     686    /**
     687     * Log messages to the database
     688     *
     689     * To avoid storing too much data, the log is reset during each run, and only $limit rows are stored
     690     *
     691     * @param string $message
     692     */
     693    protected function log( $message ) {
     694        $limit = 500;
     695
     696        if ( ! isset( $this->log ) ) {
     697            $this->log = array();
     698        }
     699
     700        if ( count( $this->log ) > $limit ) {
     701            wp_die('early return');
     702            return;
     703        }
     704
     705        $backtrace = debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, 2 );
     706
     707        $this->log[] = sprintf(
     708            '%s - %s MB - %s - %s',
     709            microtime( true ),
     710            number_format( memory_get_usage( true ) / 1024 / 2014, 2 ),
     711            $backtrace[1]['function'],
     712            $message
     713        );
     714
     715        if ( $limit === count( $this->log ) ) {
     716            $this->log[] = array( 'Reached log limit, assuming some kind of infinite loop. Will not log any more messages.' );
     717        }
     718
     719        update_option( 'owpe_log', $this->log, false );
    657720    }
    658721}
Note: See TracChangeset for help on using the changeset viewer.