| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 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 |
| 33 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 34 | */ |
| 35 | class CRM_Core_JobManager { |
| 36 | |
| 37 | /** |
| 38 | * Jobs. |
| 39 | * |
| 40 | * Format is ($id => CRM_Core_ScheduledJob). |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | public $jobs = NULL; |
| 45 | |
| 46 | /** |
| 47 | * @var CRM_Core_ScheduledJob |
| 48 | */ |
| 49 | public $currentJob = NULL; |
| 50 | |
| 51 | public $singleRunParams = []; |
| 52 | |
| 53 | public $_source = NULL; |
| 54 | |
| 55 | /** |
| 56 | * Class constructor. |
| 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 | |
| 65 | /** |
| 66 | * @param bool $auth |
| 67 | */ |
| 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.'); |
| 87 | |
| 88 | // Set last cron date for the status check |
| 89 | $statusPref = [ |
| 90 | 'name' => 'checkLastCron', |
| 91 | 'check_info' => gmdate('U'), |
| 92 | ]; |
| 93 | CRM_Core_BAO_StatusPreference::create($statusPref); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Class destructor. |
| 98 | */ |
| 99 | public function __destruct() { |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param $entity |
| 104 | * @param $action |
| 105 | */ |
| 106 | public function executeJobByAction($entity, $action) { |
| 107 | $job = $this->_getJob(NULL, $entity, $action); |
| 108 | $this->executeJob($job); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param int $id |
| 113 | */ |
| 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; |
| 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 | |
| 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 | |
| 147 | CRM_Utils_Hook::preJob($job, $params); |
| 148 | try { |
| 149 | $result = civicrm_api($job->api_entity, $job->api_action, $params); |
| 150 | } |
| 151 | catch (Exception$e) { |
| 152 | $this->logEntry('Error while executing ' . $job->name . ': ' . $e->getMessage()); |
| 153 | $result = $e; |
| 154 | } |
| 155 | CRM_Utils_Hook::postJob($job, $params, $result); |
| 156 | $this->logEntry('Finished execution of ' . $job->name . ' with result: ' . $this->_apiResultToMessage($result)); |
| 157 | $this->currentJob = FALSE; |
| 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'])) { |
| 162 | Civi::settings()->set('mailing_backend', ['outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED]); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Retrieves the list of jobs from the database, |
| 168 | * populates class param. |
| 169 | * |
| 170 | * @return array |
| 171 | * ($id => CRM_Core_ScheduledJob) |
| 172 | */ |
| 173 | private function _getJobs() { |
| 174 | $jobs = []; |
| 175 | $dao = new CRM_Core_DAO_Job(); |
| 176 | $dao->orderBy('name'); |
| 177 | $dao->domain_id = CRM_Core_Config::domainID(); |
| 178 | $dao->find(); |
| 179 | while ($dao->fetch()) { |
| 180 | $temp = []; |
| 181 | CRM_Core_DAO::storeValues($dao, $temp); |
| 182 | $jobs[$dao->id] = new CRM_Core_ScheduledJob($temp); |
| 183 | } |
| 184 | return $jobs; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Retrieves specific job from the database by id. |
| 189 | * and creates ScheduledJob object. |
| 190 | * |
| 191 | * @param int $id |
| 192 | * @param null $entity |
| 193 | * @param null $action |
| 194 | * |
| 195 | * @return CRM_Core_ScheduledJob |
| 196 | * @throws Exception |
| 197 | */ |
| 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 | } |
| 202 | $dao = new CRM_Core_DAO_Job(); |
| 203 | $dao->id = $id; |
| 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 | |
| 214 | /** |
| 215 | * @param $entity |
| 216 | * @param $job |
| 217 | * @param array $params |
| 218 | * @param null $source |
| 219 | */ |
| 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 | } |
| 226 | |
| 227 | /** |
| 228 | * @param string $message |
| 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; |
| 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; |
| 247 | } |
| 248 | $dao->description = $description; |
| 249 | |
| 250 | if ($this->currentJob) { |
| 251 | $dao->job_id = $this->currentJob->id; |
| 252 | $dao->name = $this->currentJob->name; |
| 253 | $dao->command = ts("Entity:") . " " . $this->currentJob->api_entity . " " . ts("Action:") . " " . $this->currentJob->api_action; |
| 254 | $data = ""; |
| 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 | |
| 274 | /** |
| 275 | * @param $apiResult |
| 276 | * |
| 277 | * @return string |
| 278 | */ |
| 279 | private function _apiResultToMessage($apiResult) { |
| 280 | $status = $apiResult['is_error'] ? ts('Failure') : ts('Success'); |
| 281 | $msg = CRM_Utils_Array::value('error_message', $apiResult, 'empty error_message!'); |
| 282 | $vals = CRM_Utils_Array::value('values', $apiResult, 'empty values!'); |
| 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 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @param $message |
| 297 | * |
| 298 | * @throws Exception |
| 299 | */ |
| 300 | function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) { |
| 301 | throw new Exception("{$message['message']}: {$message['code']}"); |
| 302 | } |