Making WordPress.org

Changeset 5453


Ignore:
Timestamp:
05/01/2017 11:00:53 AM (8 years ago)
Author:
ocean90
Message:

Plugin Directory: Update a scheduled job if another i18n import is triggered during the 15 minutes timeframe.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-manager.php

    r5451 r5453  
    4343
    4444    /**
    45      * Returns the latest time that the scheduled hook will run
    46      *
    47      * @param string $hook The hook to look for.
    48      * @param string $when 'last' or 'next' for when the hook runs.
    49      * @return bool|int False on failure, The timestamp on success.
    50      */
     45     * Returns the latest time that the scheduled hook will run.
     46     *
     47     * @param string $hook The hook to look for.
     48     * @param string $when 'last' or 'next' for when the hook runs.
     49     * @return bool|int False on failure, The timestamp on success.
     50     */
    5151    public static function get_scheduled_time( $hook, $when = 'last' ) {
    5252
     
    6464            if ( isset( $cron[ $hook ] ) ) {
    6565                foreach ( $cron[ $hook ] as $key => $cron_item ) {
     66                    // Cavalcade should present this field, if not, bail.
     67                    if ( empty( $cron_item['_job'] ) ) {
     68                        continue;
     69                    }
     70
    6671                    if ( 'waiting' === $cron_item['_job']->status ) {
    6772                        $timestamps[] = $timestamp;
     
    8489
    8590    /**
    86      * Reschedules a cavalcade job.
    87      * This requires the usage of Cavalcade, and will fail without it.
    88      *
    89      * @param string $hook The Hook to reschedule.
    90      * @param int $new_timestamp The time to reschedule it to.
    91      * @param int $old_timestamp The specific job to schedule. Optional, will affect first job otherwise.
    92      */
     91     * Returns the current scheduled events of a hook.
     92     *
     93     * @param string   $hook           The hook to look for.
     94     * @param int|bool $next_timestamp Optional. Returns events for a specific timestamp.
     95     * @return array Scheduled events.
     96     */
     97    public static function get_scheduled_events( $hook, $next_timestamp = false ) {
     98
     99        // Flush the Cavalcade jobs cache, we need fresh data from the database.
     100        wp_cache_delete( 'jobs', 'cavalcade-jobs' );
     101
     102        $crons = _get_cron_array();
     103        if ( empty( $crons ) ) {
     104            return [];
     105        }
     106
     107        $events = [];
     108
     109        foreach ( $crons as $timestamp => $cron ) {
     110            if ( isset( $cron[ $hook ] ) ) {
     111                foreach ( $cron[ $hook ] as $key => $cron_item ) {
     112                    // Cavalcade should present this field, if not, bail.
     113                    if ( empty( $cron_item['_job'] ) ) {
     114                        continue;
     115                    }
     116
     117                    if ( 'waiting' !== $cron_item['_job']->status ) {
     118                        continue;
     119                    }
     120
     121                    if ( ! $next_timestamp || $next_timestamp === $timestamp ) {
     122                        $events[] = [
     123                            'hook'      => $cron_item['_job']->hook,
     124                            'args'      => $cron_item['_job']->args,
     125                            'nextrun'   => $timestamp,
     126                        ];
     127                    }
     128                }
     129            }
     130        }
     131
     132        return $events;
     133    }
     134
     135    /**
     136     * Updates a cavalcade job.
     137     *
     138     * This requires the usage of Cavalcade, and will fail without it.
     139     *
     140     * @param string $hook           The hook to update.
     141     * @param int    $next_timestamp The time of the schedule to update.
     142     * @param array  $data           The data to update.
     143     * @return bool True on success, false on error.
     144     */
     145    public static function update_scheduled_event( $hook, $next_timestamp, $data ) {
     146        // Flush the Cavalcade jobs cache, we need fresh data from the database
     147        wp_cache_delete( 'jobs', 'cavalcade-jobs' );
     148
     149        $crons = _get_cron_array();
     150        foreach ( $crons as $timestamp => $cron ) {
     151            if ( $next_timestamp !== $timestamp ) {
     152                continue;
     153            }
     154
     155            if ( isset( $cron[ $hook ] ) ) {
     156                foreach ( $cron[ $hook ] as $key => $event ) {
     157                    // Cavalcade should present this field, if not, bail.
     158                    if ( empty( $event['_job'] ) ) {
     159                        return false;
     160                    }
     161
     162                    if ( 'waiting' !== $event['_job']->status ) {
     163                        return false;
     164                    }
     165
     166                    $event['_job']->args = $data['args'];
     167                    $event['_job']->nextrun = $data['nextrun'];
     168                    $event['_job']->save();
     169
     170                    return true;
     171                }
     172            }
     173        }
     174
     175        return false;
     176    }
     177
     178    /**
     179     * Reschedules a cavalcade job.
     180     * This requires the usage of Cavalcade, and will fail without it.
     181     *
     182     * @param string   $hook          The hook to reschedule.
     183     * @param int|bool $new_timestamp The time to reschedule it to.
     184     * @param int|bool $old_timestamp The specific job to schedule. Optional, will affect first job otherwise.
     185     * @return bool True on success, false on error.
     186     */
    93187    public static function reschedule_event( $hook, $new_timestamp = false, $old_timestamp = false ) {
    94188        $new_timestamp = $new_timestamp ?: time();
     
    105199            if ( isset( $cron[ $hook ] ) ) {
    106200                foreach ( $cron[ $hook ] as $key => $event ) {
    107                     // Cavalcade should present this field,if not, bail.
     201                    // Cavalcade should present this field, if not, bail.
    108202                    if ( empty( $event['_job'] ) ) {
    109203                        return false;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-i18n-import.php

    r5449 r5453  
    1717     */
    1818    public static function queue( $plugin_slug, $plugin_data ) {
    19         // To avoid a situation where two imports run concurrently, if one is already scheduled, run it 1hr later (We'll trigger it after the current one finishes).
    2019        $when_to_run = time() + 15 * MINUTE_IN_SECONDS;
    21         if ( $next_scheuled = Manager::get_scheduled_time( "import_plugin_i18n:{$plugin_slug}", 'last' ) ) {
    22             $when_to_run = $next_scheuled + HOUR_IN_SECONDS;
     20        $next_scheduled = Manager::get_scheduled_time( "import_plugin_i18n:{$plugin_slug}", 'last' );
     21
     22        // Update a scheduled event if it doesn't run in the next minute.
     23        if ( $next_scheduled && $next_scheduled > time() + 1 * MINUTE_IN_SECONDS ) {
     24            $next_scheduled_events = Manager::get_scheduled_events( "import_plugin_i18n:{$plugin_slug}",$next_scheduled );
     25            if ( $next_scheduled_events ) {
     26                $next_scheduled_event = array_shift( $next_scheduled_events );
     27
     28                $next_scheduled_event['args'][0]['tags_touched'] = array_merge(
     29                    $next_scheduled_event['args'][0]['tags_touched'],
     30                    $plugin_data['tags_touched']
     31                );
     32
     33                if ( $plugin_data['readme_touched'] ) {
     34                    $next_scheduled_event['args'][0]['readme_touched'] = true;
     35                }
     36
     37                if ( $plugin_data['code_touched'] ) {
     38                    $next_scheduled_event['args'][0]['code_touched'] = true;
     39                }
     40
     41                if ( $plugin_data['assets_touched'] ) {
     42                    $next_scheduled_event['args'][0]['assets_touched'] = true;
     43                }
     44
     45                $next_scheduled_event['args'][0]['revisions'] = array_merge(
     46                    $next_scheduled_event['args'][0]['revisions'],
     47                    $plugin_data['revisions']
     48                );
     49
     50                $result = Manager::update_scheduled_event( "import_plugin_i18n:{$plugin_slug}", $next_scheduled, $next_scheduled_event );
     51                if ( $result ) {
     52                    return;
     53                }
     54            }
    2355        }
    2456
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-plugin-import.php

    r5449 r5453  
    1515        // To avoid a situation where two imports run concurrently, if one is already scheduled, run it 1hr later (We'll trigger it after the current one finishes).
    1616        $when_to_run = time();
    17         if ( $next_scheuled = Manager::get_scheduled_time( "import_plugin:{$plugin_slug}", 'last' ) ) {
    18             $when_to_run = $next_scheuled + HOUR_IN_SECONDS;
     17        if ( $next_scheduled = Manager::get_scheduled_time( "import_plugin:{$plugin_slug}", 'last' ) ) {
     18            $when_to_run = $next_scheduled + HOUR_IN_SECONDS;
    1919        }
    2020
Note: See TracChangeset for help on using the changeset viewer.