-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix recurring schedules, e.g. Cron and Interval schedules
Recurring schedules issues, as outlined in #330. This patch fixes recurring schedules, and in the process, refactors all schedules to avoid duplicate code and ambiguity of properties and parameters. This patch introduces to important new base classes: * ActionScheduler_Abstract_Schedule and * ActionScheduler_Abstract_RecurringSchedule These are then used to implement all other schedule types - cron, recurring/interval and simple. This helps unify the data on each schedule. For example, schedules used to have different property names referring to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp was the same as ActionScheduler_SimpleSchedule::timestamp. Both of these timestamps actually referring to the date the action was scheduled, but due to ambiguity in naming, like 'start' or 'date' or 'first', the data was sometimes used to refer to the scheduled date, and sometimes the first instance of a recurring schedule. While unifying the properties, their nomenclature has also been changed to avoid ambiguity. Fixes #330 and associated issues - #256, #258, #291, #293 and #296.
- Loading branch information
Showing
26 changed files
with
471 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
classes/abstracts/ActionScheduler_Abstract_RecurringSchedule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/** | ||
* Class ActionScheduler_Abstract_RecurringSchedule | ||
*/ | ||
abstract class ActionScheduler_Abstract_RecurringSchedule extends ActionScheduler_Abstract_Schedule { | ||
|
||
/** | ||
* The date & time the first instance of this schedule was setup to run (which may not be this instance). | ||
* | ||
* Schedule objects are attached to an action object. Each schedule stores the run date for that | ||
* object as the start date - @see $this->start - and logic to calculate the next run date after | ||
* that - @see $this->calculate_next(). The $first_date property also keeps a record of when the very | ||
* first instance of this chain of schedules ran. | ||
* | ||
* @var DateTime | ||
*/ | ||
private $first_date = NULL; | ||
|
||
/** | ||
* Timestamp equivalent of @see $this->first_date | ||
* | ||
* @var int | ||
*/ | ||
protected $first_timestamp = 0; | ||
|
||
/** | ||
* The recurrance between each time an action is run using this schedule. | ||
* Used to calculate the start date & time. Can be a number of seconds, in the | ||
* case of ActionScheduler_IntervalSchedule, or a cron expression, as in the | ||
* case of ActionScheduler_CronSchedule. Or something else. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $recurrence; | ||
|
||
/** | ||
* @param DateTime $date The date & time to run the action. | ||
* @param mixed $recurrence The data used to determine the schedule's recurrance. | ||
* @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance. | ||
*/ | ||
public function __construct( DateTime $date, $recurrence, DateTime $first = null ) { | ||
parent::__construct( $date ); | ||
$this->first_date = empty( $first ) ? $date : $first; | ||
$this->recurrence = $recurrence; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function is_recurring() { | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the date & time of the first schedule in this recurring series. | ||
* | ||
* @return DateTime|null | ||
*/ | ||
public function get_first_date() { | ||
return clone $this->first_date; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function get_recurrence() { | ||
return $this->recurrence; | ||
} | ||
|
||
/** | ||
* For PHP 5.2 compat, since DateTime objects can't be serialized | ||
* @return array | ||
*/ | ||
public function __sleep() { | ||
$sleep_params = parent::__sleep(); | ||
$this->first_timestamp = $this->first_date->getTimestamp(); | ||
return array_merge( $sleep_params, array( | ||
'first_timestamp', | ||
'recurrence' | ||
) ); | ||
} | ||
|
||
public function __wakeup() { | ||
parent::__wakeup(); | ||
if ( $this->first_timestamp > 0 ) { | ||
$this->first_date = as_get_datetime_object( $this->first_timestamp ); | ||
} else { | ||
$this->first_date = $this->get_date(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* Class ActionScheduler_Abstract_Schedule | ||
*/ | ||
abstract class ActionScheduler_Abstract_Schedule extends ActionScheduler_Schedule_Deprecated { | ||
|
||
/** | ||
* The date & time the schedule is set to run. | ||
* | ||
* @var DateTime | ||
*/ | ||
private $scheduled_date = NULL; | ||
|
||
/** | ||
* Timestamp equivalent of @see $this->scheduled_date | ||
* | ||
* @var int | ||
*/ | ||
protected $scheduled_timestamp = 0; | ||
|
||
/** | ||
* @param DateTime $date The date & time to run the action. | ||
*/ | ||
public function __construct( DateTime $date ) { | ||
$this->scheduled_date = $date; | ||
} | ||
|
||
/** | ||
* Check if a schedule should recur. | ||
* | ||
* @return bool | ||
*/ | ||
abstract public function is_recurring(); | ||
|
||
/** | ||
* Calculate when the next instance of this schedule would run based on a given date & time. | ||
* | ||
* @param DateTime $after | ||
* @return DateTime | ||
*/ | ||
abstract protected function calculate_next( DateTime $after ); | ||
|
||
/** | ||
* Get the next date & time when this schedule should run after a given date & time. | ||
* | ||
* @param DateTime $after | ||
* @return DateTime|null | ||
*/ | ||
public function get_next( DateTime $after ) { | ||
$after = clone $after; | ||
if ( $after > $this->scheduled_date ) { | ||
$after = $this->calculate_next( $after ); | ||
return $after; | ||
} | ||
return clone $this->scheduled_date; | ||
} | ||
|
||
/** | ||
* Get the date & time the schedule is set to run. | ||
* | ||
* @return DateTime|null | ||
*/ | ||
public function get_date() { | ||
return $this->scheduled_date; | ||
} | ||
|
||
/** | ||
* For PHP 5.2 compat, since DateTime objects can't be serialized | ||
* @return array | ||
*/ | ||
public function __sleep() { | ||
$this->scheduled_timestamp = $this->scheduled_date->getTimestamp(); | ||
return array( | ||
'scheduled_timestamp', | ||
); | ||
} | ||
|
||
public function __wakeup() { | ||
$this->scheduled_date = as_get_datetime_object( $this->scheduled_timestamp ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.