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
21 class CRM_Core_ScheduledJob
{
27 public $apiParams = [];
32 * @param array $params
34 public function __construct($params) {
35 foreach ($params as $name => $param) {
36 $this->$name = $param;
39 // version is set to 3 by default - if different number
40 // defined in params, it's replaced later on, however,
41 // it's practically useles, since it seems none of api v2
42 // will work properly in cron job setup. It might become
43 // useful when/if api v4 starts to emerge and will need
44 // testing in the cron job setup. To permanenty require
45 // hardcoded api version, it's enough to move below line
46 // under following if block.
47 $this->apiParams
= ['version' => $this->version
];
49 if (!empty($this->parameters
)) {
50 $lines = explode("\n", $this->parameters
);
52 foreach ($lines as $line) {
53 $pair = explode("=", $line);
54 if ($pair === FALSE ||
count($pair) != 2 ||
trim($pair[0]) == '' ||
trim($pair[1]) == '') {
55 $this->remarks
[] .= 'Malformed parameters!';
58 $this->apiParams
[trim($pair[0])] = trim($pair[1]);
66 public function saveLastRun($date = NULL) {
67 $dao = new CRM_Core_DAO_Job();
69 $dao->last_run
= ($date == NULL) ? CRM_Utils_Date
::currentDBDate() : CRM_Utils_Date
::currentDBDate($date);
76 public function clearScheduledRunDate() {
77 CRM_Core_DAO
::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1',
79 '1' => [$this->id
, 'Integer'],
86 public function needsRunning() {
89 // check if the job has a specific scheduled date/time
90 if (!empty($this->scheduled_run_date
)) {
91 if (strtotime($this->scheduled_run_date
) <= time()) {
92 $this->clearScheduledRunDate();
100 // run if it was never run
101 if (empty($this->last_run
)) {
105 // run_frequency check
106 switch ($this->run_frequency
) {
116 $offset = '+3 months';
120 $offset = '+1 month';
136 $now = strtotime(CRM_Utils_Date
::currentDBDate());
137 $lastTime = strtotime($this->last_run
);
138 $nextTime = strtotime($offset, $lastTime);
140 return ($now >= $nextTime);
143 public function __destruct() {