Merge pull request #21499 from seamuslee001/master
[civicrm-core.git] / CRM / Core / JobManager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
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
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19 class CRM_Core_JobManager {
20
21 /**
22 * Jobs.
23 *
24 * Format is ($id => CRM_Core_ScheduledJob).
25 *
26 * @var array
27 */
28 public $jobs = NULL;
29
30 /**
31 * @var CRM_Core_ScheduledJob
32 */
33 public $currentJob = NULL;
34
35 public $singleRunParams = [];
36
37 public $_source = NULL;
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 $this->jobs = $this->_getJobs();
44 }
45
46 /**
47 * @param bool $auth
48 */
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.');
68
69 // Set last cron date for the status check
70 $statusPref = [
71 'name' => 'checkLastCron',
72 'check_info' => gmdate('U'),
73 'prefs' => '',
74 ];
75 CRM_Core_BAO_StatusPreference::create($statusPref);
76 }
77
78 /**
79 * Class destructor.
80 */
81 public function __destruct() {
82 }
83
84 /**
85 * @param $entity
86 * @param $action
87 */
88 public function executeJobByAction($entity, $action) {
89 $job = $this->_getJob(NULL, $entity, $action);
90 $this->executeJob($job);
91 }
92
93 /**
94 * @param int $id
95 */
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;
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
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
129 CRM_Utils_Hook::preJob($job, $params);
130 try {
131 $result = civicrm_api($job->api_entity, $job->api_action, $params);
132 }
133 catch (Exception$e) {
134 $this->logEntry('Error while executing ' . $job->name . ': ' . $e->getMessage());
135 $result = $e;
136 }
137 CRM_Utils_Hook::postJob($job, $params, $result);
138 $this->logEntry('Finished execution of ' . $job->name . ' with result: ' . $this->_apiResultToMessage($result));
139 $this->currentJob = FALSE;
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'])) {
144 Civi::settings()->set('mailing_backend', ['outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED]);
145 }
146 }
147
148 /**
149 * Retrieves the list of jobs from the database,
150 * populates class param.
151 *
152 * @return array
153 * ($id => CRM_Core_ScheduledJob)
154 */
155 private function _getJobs() {
156 $jobs = [];
157 $dao = new CRM_Core_DAO_Job();
158 $dao->orderBy('name');
159 $dao->domain_id = CRM_Core_Config::domainID();
160 $dao->find();
161 while ($dao->fetch()) {
162 $temp = [];
163 CRM_Core_DAO::storeValues($dao, $temp);
164 $jobs[$dao->id] = new CRM_Core_ScheduledJob($temp);
165 }
166 return $jobs;
167 }
168
169 /**
170 * Retrieves specific job from the database by id.
171 * and creates ScheduledJob object.
172 *
173 * @param int $id
174 * @param null $entity
175 * @param null $action
176 *
177 * @return CRM_Core_ScheduledJob
178 * @throws Exception
179 */
180 private function _getJob($id = NULL, $entity = NULL, $action = NULL) {
181 if (is_null($id) && is_null($action)) {
182 throw new CRM_Core_Exception('You need to provide either id or name to use this method');
183 }
184 $dao = new CRM_Core_DAO_Job();
185 $dao->id = $id;
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
196 /**
197 * @param $entity
198 * @param $job
199 * @param array $params
200 * @param null $source
201 */
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 }
208
209 /**
210 * @param string $message
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;
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;
229 }
230 $dao->description = $description;
231
232 if ($this->currentJob) {
233 $dao->job_id = $this->currentJob->id;
234 $dao->name = $this->currentJob->name;
235 $dao->command = ts("Entity:") . " " . $this->currentJob->api_entity . " " . ts("Action:") . " " . $this->currentJob->api_action;
236 $data = "";
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
256 /**
257 * @param $apiResult
258 *
259 * @return string
260 */
261 private function _apiResultToMessage($apiResult) {
262 $status = $apiResult['is_error'] ? ts('Failure') : ts('Success');
263 $msg = CRM_Utils_Array::value('error_message', $apiResult, 'empty error_message!');
264 $vals = CRM_Utils_Array::value('values', $apiResult, 'empty values!');
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 }
274
275 }