{"id":3735,"date":"2021-03-10T17:10:22","date_gmt":"2021-03-10T17:10:22","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3735"},"modified":"2022-02-17T07:13:46","modified_gmt":"2022-02-17T07:13:46","slug":"5-min-cron-with-catchup-if-not-called-every-5-mins","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/cron-scheduled-tasks\/cron-examples\/5-min-cron-with-catchup-if-not-called-every-5-mins","title":{"rendered":"5 min cron with catchup if not called every 5 mins"},"content":{"rendered":"\n<p>This handler cron is intended to be called every 5 mins and will run 5 min tasks and 1 hour tasks, using a timestamp stored for each.  If it hasn&#8217;t been called every 5 mins it will loop the 5 min tasks to catch up<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  \/\/----- GET CRON LAST RUN TIME STAMPS -----\n  $CronLastRunDateTime = get_site_option('MYPLUGINNAME_CronLastRunDateTime');   \/\/Already stored as strtotime()\n  $TheDateTimeNow = strtotime(date(\"Y-m-d H:i:s\"));\n                              \n  if ($CronLastRunDateTime === False)\n    $CronLastRunSecondsAgo = (60 * 60);     \/\/Default to 1 hour ago\n  else\n    $CronLastRunSecondsAgo = $TheDateTimeNow - $CronLastRunDateTime;\n  \n  if ($CronLastRunSecondsAgo &lt; 0)\n    $CronLastRunSecondsAgo = (5*60);    \/\/Something bad has happened, set to run now once\n  \n  $CronLastRunHour = get_site_option('MYPLUGINNAME_CronLastRunHour', -1);\n  $TheHourNow = intval(date(\"H\"));\n  \n  \n  \/\/We want to ensure that we run our every 5 mins tasks multiple times if its taken longer than 5 mins to get here, e.g. on a site where cron isn't happening every 5 mins due to\n  \/\/low visitor numbers and no dedicated cron event setup.\n  if ($CronLastRunSecondsAgo > (60 * 60))   \/\/Limit to max 1 hour, if its more than this then discard additional 5 min events as we don't want to go crazy with this feature\n    $CronLastRunSecondsAgo = (60 * 60);\n  \n  while ($CronLastRunSecondsAgo >= ((5*60)-10) )      \/\/Give a little bit of error margin, 10 secs\n  {\n    $CronLastRunSecondsAgo -= (5 * 60);\n    \/\/--------------------------------------\n    \/\/--------------------------------------\n    \/\/----- DO ONCE EVERY 5 MINS TASKS -----\n    \/\/--------------------------------------\n    \/\/--------------------------------------\n    \n    \n    \n    \n    \n    \/\/All done - Store the time stamp\n    update_option('MYPLUGINNAME_CronLastRunDateTime', $TheDateTimeNow, True);\n  } \/\/while ($CronLastRunSecondsAgo >= ((5*60)-10) )\n  \n\n  if ($CronLastRunHour != $TheHourNow)\n  {\n    \/\/----------------------------------\n    \/\/----------------------------------\n    \/\/----- DO ONCE PER HOUR TASKS -----\n    \/\/----------------------------------\n    \/\/----------------------------------\n  \n\n    \n    \n    \/\/All don - Store the hour\n    update_option('MYPLUGINNAME_CronLastRunHour', $TheHourNow, True);\n  } \/\/if ($CronLastRunHour != $TheHourNow)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This handler cron is intended to be called every 5 mins and will run 5 min tasks and 1 hour tasks, using a timestamp stored for each. If it hasn&#8217;t been called every 5 mins it will loop the 5 min tasks to catch up<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301],"tags":[],"class_list":["post-3735","post","type-post","status-publish","format-standard","hentry","category-cron-examples"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3735"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3735\/revisions"}],"predecessor-version":[{"id":3738,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3735\/revisions\/3738"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}