Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 33 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
34 | */ |
35 | class CRM_Core_JobManager { | |
36 | ||
37 | /** | |
38 | * @var array ($id => CRM_Core_ScheduledJob) | |
39 | */ | |
40 | var $jobs = NULL; | |
41 | ||
42 | /** | |
43 | * @var CRM_Core_ScheduledJob | |
44 | */ | |
45 | var $currentJob = NULL; | |
46 | ||
47 | var $singleRunParams = array(); | |
48 | ||
49 | var $_source = NULL; | |
50 | ||
51 | ||
d424ffde | 52 | /** |
d09edf64 | 53 | * Class constructor. |
6a488035 TO |
54 | */ |
55 | public function __construct() { | |
56 | $config = CRM_Core_Config::singleton(); | |
57 | $config->fatalErrorHandler = 'CRM_Core_JobManager_scheduledJobFatalErrorHandler'; | |
58 | ||
59 | $this->jobs = $this->_getJobs(); | |
60 | } | |
61 | ||
a0ee3941 EM |
62 | /** |
63 | * @param bool $auth | |
64 | */ | |
6a488035 TO |
65 | public function execute($auth = TRUE) { |
66 | ||
67 | $this->logEntry('Starting scheduled jobs execution'); | |
68 | ||
69 | if ($auth && !CRM_Utils_System::authenticateKey(TRUE)) { | |
70 | $this->logEntry('Could not authenticate the site key.'); | |
71 | } | |
72 | require_once 'api/api.php'; | |
73 | ||
74 | // it's not asynchronous at this stage | |
75 | CRM_Utils_Hook::cron($this); | |
76 | foreach ($this->jobs as $job) { | |
77 | if ($job->is_active) { | |
78 | if ($job->needsRunning()) { | |
79 | $this->executeJob($job); | |
80 | } | |
81 | } | |
82 | } | |
83 | $this->logEntry('Finishing scheduled jobs execution.'); | |
aa96ce62 AH |
84 | |
85 | // Set last cron date for the status check | |
86 | $statusPref = array( | |
87 | 'name' => 'checkLastCron', | |
88 | 'check_info' => gmdate('U'), | |
89 | ); | |
90 | CRM_Core_BAO_StatusPreference::create($statusPref); | |
6a488035 TO |
91 | } |
92 | ||
d424ffde | 93 | /** |
d09edf64 | 94 | * Class destructor. |
6a488035 | 95 | */ |
6ea503d4 TO |
96 | public function __destruct() { |
97 | } | |
6a488035 | 98 | |
a0ee3941 EM |
99 | /** |
100 | * @param $entity | |
101 | * @param $action | |
102 | */ | |
6a488035 TO |
103 | public function executeJobByAction($entity, $action) { |
104 | $job = $this->_getJob(NULL, $entity, $action); | |
105 | $this->executeJob($job); | |
106 | } | |
107 | ||
a0ee3941 | 108 | /** |
100fef9d | 109 | * @param int $id |
a0ee3941 | 110 | */ |
6a488035 TO |
111 | public function executeJobById($id) { |
112 | $job = $this->_getJob($id); | |
113 | $this->executeJob($job); | |
114 | } | |
115 | ||
116 | /** | |
117 | * @param CRM_Core_ScheduledJob $job | |
118 | */ | |
119 | public function executeJob($job) { | |
120 | $this->currentJob = $job; | |
f008885c E |
121 | |
122 | // CRM-18231 check if non-production environment. | |
123 | try { | |
124 | CRM_Core_BAO_Setting::isAPIJobAllowedToRun($job->apiParams); | |
125 | } | |
126 | catch (Exception $e) { | |
127 | $this->logEntry('Error while executing ' . $job->name . ': ' . $e->getMessage()); | |
128 | $this->currentJob = FALSE; | |
129 | return FALSE; | |
130 | } | |
131 | ||
6a488035 TO |
132 | $this->logEntry('Starting execution of ' . $job->name); |
133 | $job->saveLastRun(); | |
134 | ||
135 | $singleRunParamsKey = strtolower($job->api_entity . '_' . $job->api_action); | |
136 | ||
137 | if (array_key_exists($singleRunParamsKey, $this->singleRunParams)) { | |
138 | $params = $this->singleRunParams[$singleRunParamsKey]; | |
139 | } | |
140 | else { | |
141 | $params = $job->apiParams; | |
142 | } | |
143 | ||
144 | try { | |
145 | $result = civicrm_api($job->api_entity, $job->api_action, $params); | |
146 | } | |
353ffa53 | 147 | catch (Exception$e) { |
6a488035 TO |
148 | $this->logEntry('Error while executing ' . $job->name . ': ' . $e->getMessage()); |
149 | } | |
150 | $this->logEntry('Finished execution of ' . $job->name . ' with result: ' . $this->_apiResultToMessage($result)); | |
151 | $this->currentJob = FALSE; | |
152 | } | |
153 | ||
d424ffde | 154 | /** |
6a488035 TO |
155 | * Retrieves the list of jobs from the database, |
156 | * populates class param. | |
cbb7c7e0 | 157 | * |
a6c01b45 CW |
158 | * @return array |
159 | * ($id => CRM_Core_ScheduledJob) | |
6a488035 TO |
160 | */ |
161 | private function _getJobs() { | |
162 | $jobs = array(); | |
163 | $dao = new CRM_Core_DAO_Job(); | |
164 | $dao->orderBy('name'); | |
2f7a6dd7 | 165 | $dao->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
166 | $dao->find(); |
167 | while ($dao->fetch()) { | |
168 | $temp = array(); | |
169 | CRM_Core_DAO::storeValues($dao, $temp); | |
170 | $jobs[$dao->id] = new CRM_Core_ScheduledJob($temp); | |
171 | } | |
172 | return $jobs; | |
173 | } | |
174 | ||
d424ffde | 175 | /** |
d09edf64 | 176 | * Retrieves specific job from the database by id. |
6a488035 | 177 | * and creates ScheduledJob object. |
cbb7c7e0 | 178 | * |
100fef9d | 179 | * @param int $id |
a0ee3941 EM |
180 | * @param null $entity |
181 | * @param null $action | |
182 | * | |
183 | * @return CRM_Core_ScheduledJob | |
184 | * @throws Exception | |
185 | */ | |
6a488035 TO |
186 | private function _getJob($id = NULL, $entity = NULL, $action = NULL) { |
187 | if (is_null($id) && is_null($action)) { | |
188 | CRM_Core_Error::fatal('You need to provide either id or name to use this method'); | |
189 | } | |
353ffa53 TO |
190 | $dao = new CRM_Core_DAO_Job(); |
191 | $dao->id = $id; | |
6a488035 TO |
192 | $dao->api_entity = $entity; |
193 | $dao->api_action = $action; | |
194 | $dao->find(); | |
195 | while ($dao->fetch()) { | |
196 | CRM_Core_DAO::storeValues($dao, $temp); | |
197 | $job = new CRM_Core_ScheduledJob($temp); | |
198 | } | |
199 | return $job; | |
200 | } | |
201 | ||
a0ee3941 EM |
202 | /** |
203 | * @param $entity | |
204 | * @param $job | |
c490a46a | 205 | * @param array $params |
a0ee3941 EM |
206 | * @param null $source |
207 | */ | |
6a488035 TO |
208 | public function setSingleRunParams($entity, $job, $params, $source = NULL) { |
209 | $this->_source = $source; | |
210 | $key = strtolower($entity . '_' . $job); | |
211 | $this->singleRunParams[$key] = $params; | |
212 | $this->singleRunParams[$key]['version'] = 3; | |
213 | } | |
cbb7c7e0 | 214 | |
d424ffde CW |
215 | /** |
216 | * @param string $message | |
6a488035 TO |
217 | */ |
218 | public function logEntry($message) { | |
219 | $domainID = CRM_Core_Config::domainID(); | |
220 | $dao = new CRM_Core_DAO_JobLog(); | |
221 | ||
222 | $dao->domain_id = $domainID; | |
223 | $dao->description = substr($message, 0, 235); | |
224 | if (strlen($message) > 235) { | |
225 | $dao->description .= " (...)"; | |
226 | } | |
227 | if ($this->currentJob) { | |
353ffa53 TO |
228 | $dao->job_id = $this->currentJob->id; |
229 | $dao->name = $this->currentJob->name; | |
fe901a30 | 230 | $dao->command = ts("Entity:") . " " . $this->currentJob->api_entity . " " . ts("Action:") . " " . $this->currentJob->api_action; |
353ffa53 | 231 | $data = ""; |
6a488035 TO |
232 | if (!empty($this->currentJob->parameters)) { |
233 | $data .= "\n\nParameters raw (from db settings): \n" . $this->currentJob->parameters; | |
234 | } | |
235 | $singleRunParamsKey = strtolower($this->currentJob->api_entity . '_' . $this->currentJob->api_action); | |
236 | if (array_key_exists($singleRunParamsKey, $this->singleRunParams)) { | |
237 | $data .= "\n\nParameters raw (" . $this->_source . "): \n" . serialize($this->singleRunParams[$singleRunParamsKey]); | |
238 | $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->singleRunParams[$singleRunParamsKey]); | |
239 | } | |
240 | else { | |
241 | $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->currentJob->apiParams); | |
242 | } | |
243 | ||
244 | $data .= "\n\nFull message: \n" . $message; | |
245 | ||
246 | $dao->data = $data; | |
247 | } | |
248 | $dao->save(); | |
249 | } | |
250 | ||
a0ee3941 EM |
251 | /** |
252 | * @param $apiResult | |
253 | * | |
254 | * @return string | |
255 | */ | |
6a488035 TO |
256 | private function _apiResultToMessage($apiResult) { |
257 | $status = $apiResult['is_error'] ? ts('Failure') : ts('Success'); | |
353ffa53 TO |
258 | $msg = CRM_Utils_Array::value('error_message', $apiResult, 'empty error_message!'); |
259 | $vals = CRM_Utils_Array::value('values', $apiResult, 'empty values!'); | |
6a488035 TO |
260 | if (is_array($msg)) { |
261 | $msg = serialize($msg); | |
262 | } | |
263 | if (is_array($vals)) { | |
264 | $vals = serialize($vals); | |
265 | } | |
266 | $message = $apiResult['is_error'] ? ', Error message: ' . $msg : " (" . $vals . ")"; | |
267 | return $status . $message; | |
268 | } | |
96025800 | 269 | |
6a488035 TO |
270 | } |
271 | ||
a0ee3941 EM |
272 | /** |
273 | * @param $message | |
274 | * | |
275 | * @throws Exception | |
276 | */ | |
6a488035 TO |
277 | function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) { |
278 | throw new Exception("{$message['message']}: {$message['code']}"); | |
279 | } |