3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This interface defines methods that need to be implemented
30 * by every scheduled job (cron task) in CiviCRM.
33 * @copyright CiviCRM LLC (c) 2004-2017
37 class CRM_Core_ScheduledJob
{
43 var $apiParams = array();
45 var $remarks = array();
48 * @param array $params
50 public function __construct($params) {
51 foreach ($params as $name => $param) {
52 $this->$name = $param;
55 // version is set to 3 by default - if different number
56 // defined in params, it's replaced later on, however,
57 // it's practically useles, since it seems none of api v2
58 // will work properly in cron job setup. It might become
59 // useful when/if api v4 starts to emerge and will need
60 // testing in the cron job setup. To permanenty require
61 // hardcoded api version, it's enough to move below line
62 // under following if block.
63 $this->apiParams
= array('version' => $this->version
);
65 if (!empty($this->parameters
)) {
66 $lines = explode("\n", $this->parameters
);
68 foreach ($lines as $line) {
69 $pair = explode("=", $line);
70 if ($pair === FALSE ||
count($pair) != 2 ||
trim($pair[0]) == '' ||
trim($pair[1]) == '') {
71 $this->remarks
[] .= 'Malformed parameters!';
74 $this->apiParams
[trim($pair[0])] = trim($pair[1]);
82 public function saveLastRun($date = NULL) {
83 $dao = new CRM_Core_DAO_Job();
85 $dao->last_run
= ($date == NULL) ? CRM_Utils_Date
::currentDBDate() : CRM_Utils_Date
::currentDBDate($date);
92 public function clearScheduledRunDate() {
93 CRM_Core_DAO
::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1',
95 '1' => array($this->id
, 'Integer'),
102 public function needsRunning() {
105 // check if the job has a specific scheduled date/time
106 if (!empty($this->scheduled_run_date
)) {
107 if (strtotime($this->scheduled_run_date
) <= time()) {
108 $this->clearScheduledRunDate();
116 // run if it was never run
117 if (empty($this->last_run
)) {
121 // run_frequency check
122 switch ($this->run_frequency
) {
132 $offset = '+3 months';
136 $offset = '+1 month';
152 $now = strtotime(CRM_Utils_Date
::currentDBDate());
153 $lastTime = strtotime($this->last_run
);
154 $nextTime = strtotime($offset, $lastTime);
156 return ($now >= $nextTime);
159 public function __destruct() {