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