3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This interface defines methods that need to be implemented
14 * by every scheduled job (cron task) in CiviCRM.
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Core_ScheduledJob
{
25 public $apiParams = [];
30 * @param array $params
32 public function __construct($params) {
33 foreach ($params as $name => $param) {
34 $this->$name = $param;
37 // version is set to 3 by default - if different number
38 // defined in params, it's replaced later on, however,
39 // it's practically useles, since it seems none of api v2
40 // will work properly in cron job setup. It might become
41 // useful when/if api v4 starts to emerge and will need
42 // testing in the cron job setup. To permanenty require
43 // hardcoded api version, it's enough to move below line
44 // under following if block.
45 $this->apiParams
= ['version' => $this->version
];
47 if (!empty($this->parameters
)) {
48 $lines = explode("\n", $this->parameters
);
50 foreach ($lines as $line) {
51 $pair = explode("=", $line);
52 if ($pair === FALSE ||
count($pair) != 2 ||
trim($pair[0]) == '' ||
trim($pair[1]) == '') {
53 $this->remarks
[] .= 'Malformed parameters!';
56 $this->apiParams
[trim($pair[0])] = trim($pair[1]);
62 * Update the last_run date of this job
64 public function saveLastRun() {
65 $dao = new CRM_Core_DAO_Job();
67 $dao->last_run
= CRM_Utils_Date
::currentDBDate();
72 * Delete the scheduled_run_date from this job
74 public function clearScheduledRunDate() {
75 CRM_Core_DAO
::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1', [
76 '1' => [$this->id
, 'Integer'],
83 public function needsRunning() {
86 // check if the job has a specific scheduled date/time
87 if (!empty($this->scheduled_run_date
)) {
88 if (strtotime($this->scheduled_run_date
) <= time()) {
89 $this->clearScheduledRunDate();
97 // run if it was never run
98 if (empty($this->last_run
)) {
102 // run_frequency check
103 switch ($this->run_frequency
) {
113 $offset = '+3 months';
117 $offset = '+1 month';
133 $now = strtotime(CRM_Utils_Date
::currentDBDate());
134 $lastTime = strtotime($this->last_run
);
135 $nextTime = strtotime($offset, $lastTime);
137 return ($now >= $nextTime);
140 public function __destruct() {