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