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