Merge pull request #14290 from MegaphoneJon/json-pp-logging
[civicrm-core.git] / CRM / Core / ScheduledJob.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * This interface defines methods that need to be implemented
30 * by every scheduled job (cron task) in CiviCRM.
31 *
32 * @package CRM
6b83d5bd 33 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
34 * $Id$
35 *
36 */
37class CRM_Core_ScheduledJob {
38
518fa0ee 39 public $version = 3;
6a488035 40
518fa0ee 41 public $name = NULL;
6a488035 42
518fa0ee 43 public $apiParams = [];
6a488035 44
518fa0ee 45 public $remarks = [];
6a488035 46
a0ee3941 47 /**
c490a46a 48 * @param array $params
a0ee3941 49 */
6a488035
TO
50 public function __construct($params) {
51 foreach ($params as $name => $param) {
52 $this->$name = $param;
53 }
54
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.
be2fb01f 63 $this->apiParams = ['version' => $this->version];
6a488035
TO
64
65 if (!empty($this->parameters)) {
66 $lines = explode("\n", $this->parameters);
67
68 foreach ($lines as $line) {
69 $pair = explode("=", $line);
94ca7fdb 70 if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
6a488035
TO
71 $this->remarks[] .= 'Malformed parameters!';
72 break;
73 }
74 $this->apiParams[trim($pair[0])] = trim($pair[1]);
75 }
76 }
77 }
78
a0ee3941
EM
79 /**
80 * @param null $date
81 */
6a488035 82 public function saveLastRun($date = NULL) {
353ffa53
TO
83 $dao = new CRM_Core_DAO_Job();
84 $dao->id = $this->id;
6a488035
TO
85 $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date);
86 $dao->save();
87 }
88
bda41fcb
DRJ
89 /**
90 * @return void
91 */
d7bace22
DRJ
92 public function clearScheduledRunDate() {
93 CRM_Core_DAO::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1',
be2fb01f
CW
94 [
95 '1' => [$this->id, 'Integer'],
96 ]);
bda41fcb
DRJ
97 }
98
a0ee3941
EM
99 /**
100 * @return bool
101 */
6a488035 102 public function needsRunning() {
bda41fcb
DRJ
103
104 // CRM-17686
105 // check if the job has a specific scheduled date/time
d7bace22
DRJ
106 if (!empty($this->scheduled_run_date)) {
107 if (strtotime($this->scheduled_run_date) <= time()) {
108 $this->clearScheduledRunDate();
bda41fcb
DRJ
109 return TRUE;
110 }
111 else {
112 return FALSE;
113 }
114 }
115
6a488035
TO
116 // run if it was never run
117 if (empty($this->last_run)) {
118 return TRUE;
119 }
120
121 // run_frequency check
122 switch ($this->run_frequency) {
123 case 'Always':
124 return TRUE;
125
bda41fcb
DRJ
126 // CRM-17669
127 case 'Yearly':
128 $offset = '+1 year';
129 break;
130
131 case 'Quarter':
132 $offset = '+3 months';
133 break;
134
135 case 'Monthly':
136 $offset = '+1 month';
137 break;
138
139 case 'Weekly':
140 $offset = '+1 week';
d0be1535 141 break;
6a488035
TO
142
143 case 'Daily':
bda41fcb 144 $offset = '+1 day';
d0be1535 145 break;
7fce7b7f 146
bda41fcb
DRJ
147 case 'Hourly':
148 $offset = '+1 hour';
149 break;
6a488035
TO
150 }
151
bda41fcb
DRJ
152 $now = strtotime(CRM_Utils_Date::currentDBDate());
153 $lastTime = strtotime($this->last_run);
154 $nextTime = strtotime($offset, $lastTime);
d0be1535 155
bda41fcb 156 return ($now >= $nextTime);
6a488035
TO
157 }
158
2aa397bc
TO
159 public function __destruct() {
160 }
96025800 161
6a488035 162}