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