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