@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 56 lines 1.9 kB view raw
1<?php 2 3/** 4 * Triggers a daily routine, like server backups. 5 * 6 * This clock triggers events every 24 hours, using UTC. It does not use a 7 * locale, and is intended for technical processes like backing up a server 8 * every night. 9 * 10 * Because UTC does not have daylight savings, the local hour when this event 11 * occurs will change over the course of the year. For example, from the 12 * perspective of a user in California, it might run backups at 3AM in the 13 * winter and 2AM in the summer. This is desirable for maintenance processes, 14 * but problematic for some human processes. Use a different clock if you're 15 * triggering a human-oriented event. 16 * 17 * The clock uses the time of day of the `start` epoch to calculate the time 18 * of day of the next event, so you can change the time of day when the event 19 * occurs by adjusting the `start` time of day. 20 */ 21final class PhabricatorDailyRoutineTriggerClock 22 extends PhabricatorTriggerClock { 23 24 public function validateProperties(array $properties) { 25 PhutilTypeSpec::checkMap( 26 $properties, 27 array( 28 'start' => 'int', 29 )); 30 } 31 32 public function getNextEventEpoch($last_epoch, $is_reschedule) { 33 $start_epoch = $this->getProperty('start'); 34 if (!$last_epoch) { 35 $last_epoch = $start_epoch; 36 } 37 38 $start = new DateTime('@'.$start_epoch); 39 $last = new DateTime('@'.$last_epoch); 40 41 // NOTE: We're choosing the date from the last event, but the time of day 42 // from the start event. This allows callers to change when the event 43 // occurs by updating the trigger's start parameter. 44 $ymd = $last->format('Y-m-d'); 45 $hms = $start->format('G:i:s'); 46 47 $next = new DateTime("{$ymd} {$hms} UTC"); 48 49 // Add a day. 50 // TODO: Use PHP's DateInterval instead which exists since PHP 5.3.0 51 $next->modify('+1 day'); 52 53 return (int)$next->format('U'); 54 } 55 56}