[wp-trac] [WordPress Trac] #49693: Drop duplicate recurring cron events

WordPress Trac noreply at wordpress.org
Sun Mar 29 21:41:16 UTC 2020


#49693: Drop duplicate recurring cron events
----------------------------------------------------+---------------------
 Reporter:  aidvu                                   |       Owner:  (none)
     Type:  enhancement                             |      Status:  new
 Priority:  normal                                  |   Milestone:  5.5
Component:  Cron API                                |     Version:  trunk
 Severity:  normal                                  |  Resolution:
 Keywords:  has-patch has-unit-tests needs-testing  |     Focuses:
----------------------------------------------------+---------------------

Comment (by peterwilsoncc):

 Replying to [comment:6 aidvu]:
 > Isn't this the reason why we have custom intervals:
 https://developer.wordpress.org/plugins/cron/understanding-wp-cron-
 scheduling/
 >
 > It might be easier to schedule duplicates with different start times,
 but I think the right way for such schedules would be a custom interval.

 To be clearer, I was thinking of the same event that runs periodically but
 at asymmetrical intervals. To use the weather example, a news site
 downloading latest forecast for my home town might use two daily events
 with the hook `49693_bomau_weather_update [ [ 'product' => 'IDV10450' ] ]`
 running at 5:15am and 4:25pm which is when the local reports are updated.

 To protect against blowing up the table for single events, WP prevents
 identical events being scheduled within ten minutes of each other. I'm
 happy enough with doing something like that but the maths would be a bit
 messy. It could also be difficult for plugins using custom cron storage
 with the hooks added in WP 5.1

 As some rough (ie, not completely tested) code:

 {{{#!php
 <?php

 $now = time();
 $next = time() + 300; // 5 minutes from now.
 $later = time() + ( 60 * 60 * 24 ) + 360 ;// 1 day, 6 minutes.
 $even_later = time() + 60 * 60 * 24 * 1.5; // 36 hours.

 $interval = 60 * 60 * 24; // One day.

 $now_offset = $now % $interval;
 $next_offset = $next % $interval;
 $later_offset = $later % $interval;
 $even_later_offset = $even_later % $interval;

 var_dump ( [
     'now_offset' => $now_offset,
     'next_offset' => $next_offset,
     'later_offset' => $later_offset,
     'even_later_offset' => $even_later_offset,
     'next_run' => abs( $now_offset - $next_offset ) < 600 ? 'disallow' :
 'allow',
     'later_run' => abs( $now_offset - $later_offset ) < 600 ? 'disallow' :
 'allow',
     'even_later_run' => abs( $now_offset - $even_later_offset ) < 600 ?
 'disallow' : 'allow',
 ] );
 }}}



 {{{
 array(7) {
   ["now_offset"]=>
   int(77695)
   ["next_offset"]=>
   int(77995)
   ["later_offset"]=>
   int(78055)
   ["even_later_offset"]=>
   int(34495)
   ["next_run"]=>
   string(8) "disallow"
   ["later_run"]=>
   string(8) "disallow"
   ["even_later_run"]=>
   string(5) "allow"
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49693#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list