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