3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This interface defines methods that need to be implemented
30 * by every scheduled job (cron task) in CiviCRM.
33 * @copyright CiviCRM LLC (c) 2004-2018
35 class CRM_Core_JobManager
{
38 * @var array ($id => CRM_Core_ScheduledJob)
43 * @var CRM_Core_ScheduledJob
45 var $currentJob = NULL;
47 var $singleRunParams = array();
55 public function __construct() {
56 $config = CRM_Core_Config
::singleton();
57 $config->fatalErrorHandler
= 'CRM_Core_JobManager_scheduledJobFatalErrorHandler';
59 $this->jobs
= $this->_getJobs();
65 public function execute($auth = TRUE) {
67 $this->logEntry('Starting scheduled jobs execution');
69 if ($auth && !CRM_Utils_System
::authenticateKey(TRUE)) {
70 $this->logEntry('Could not authenticate the site key.');
72 require_once 'api/api.php';
74 // it's not asynchronous at this stage
75 CRM_Utils_Hook
::cron($this);
76 foreach ($this->jobs
as $job) {
77 if ($job->is_active
) {
78 if ($job->needsRunning()) {
79 $this->executeJob($job);
83 $this->logEntry('Finishing scheduled jobs execution.');
85 // Set last cron date for the status check
87 'name' => 'checkLastCron',
88 'check_info' => gmdate('U'),
90 CRM_Core_BAO_StatusPreference
::create($statusPref);
96 public function __destruct() {
103 public function executeJobByAction($entity, $action) {
104 $job = $this->_getJob(NULL, $entity, $action);
105 $this->executeJob($job);
111 public function executeJobById($id) {
112 $job = $this->_getJob($id);
113 $this->executeJob($job);
117 * @param CRM_Core_ScheduledJob $job
119 public function executeJob($job) {
120 $this->currentJob
= $job;
122 // CRM-18231 check if non-production environment.
124 CRM_Core_BAO_Setting
::isAPIJobAllowedToRun($job->apiParams
);
126 catch (Exception
$e) {
127 $this->logEntry('Error while executing ' . $job->name
. ': ' . $e->getMessage());
128 $this->currentJob
= FALSE;
132 $this->logEntry('Starting execution of ' . $job->name
);
135 $singleRunParamsKey = strtolower($job->api_entity
. '_' . $job->api_action
);
137 if (array_key_exists($singleRunParamsKey, $this->singleRunParams
)) {
138 $params = $this->singleRunParams
[$singleRunParamsKey];
141 $params = $job->apiParams
;
145 $result = civicrm_api($job->api_entity
, $job->api_action
, $params);
147 catch (Exception
$e) {
148 $this->logEntry('Error while executing ' . $job->name
. ': ' . $e->getMessage());
150 $this->logEntry('Finished execution of ' . $job->name
. ' with result: ' . $this->_apiResultToMessage($result));
151 $this->currentJob
= FALSE;
153 //Disable outBound option after executing the job.
154 $environment = CRM_Core_Config
::environment(NULL, TRUE);
155 if ($environment != 'Production' && !empty($job->apiParams
['runInNonProductionEnvironment'])) {
156 Civi
::settings()->set('mailing_backend', array('outBound_option' => CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
));
161 * Retrieves the list of jobs from the database,
162 * populates class param.
165 * ($id => CRM_Core_ScheduledJob)
167 private function _getJobs() {
169 $dao = new CRM_Core_DAO_Job();
170 $dao->orderBy('name');
171 $dao->domain_id
= CRM_Core_Config
::domainID();
173 while ($dao->fetch()) {
175 CRM_Core_DAO
::storeValues($dao, $temp);
176 $jobs[$dao->id
] = new CRM_Core_ScheduledJob($temp);
182 * Retrieves specific job from the database by id.
183 * and creates ScheduledJob object.
186 * @param null $entity
187 * @param null $action
189 * @return CRM_Core_ScheduledJob
192 private function _getJob($id = NULL, $entity = NULL, $action = NULL) {
193 if (is_null($id) && is_null($action)) {
194 CRM_Core_Error
::fatal('You need to provide either id or name to use this method');
196 $dao = new CRM_Core_DAO_Job();
198 $dao->api_entity
= $entity;
199 $dao->api_action
= $action;
201 while ($dao->fetch()) {
202 CRM_Core_DAO
::storeValues($dao, $temp);
203 $job = new CRM_Core_ScheduledJob($temp);
211 * @param array $params
212 * @param null $source
214 public function setSingleRunParams($entity, $job, $params, $source = NULL) {
215 $this->_source
= $source;
216 $key = strtolower($entity . '_' . $job);
217 $this->singleRunParams
[$key] = $params;
218 $this->singleRunParams
[$key]['version'] = 3;
222 * @param string $message
224 public function logEntry($message) {
225 $domainID = CRM_Core_Config
::domainID();
226 $dao = new CRM_Core_DAO_JobLog();
228 $dao->domain_id
= $domainID;
229 $dao->description
= substr($message, 0, 235);
230 if (strlen($message) > 235) {
231 $dao->description
.= " (...)";
233 if ($this->currentJob
) {
234 $dao->job_id
= $this->currentJob
->id
;
235 $dao->name
= $this->currentJob
->name
;
236 $dao->command
= ts("Entity:") . " " . $this->currentJob
->api_entity
. " " . ts("Action:") . " " . $this->currentJob
->api_action
;
238 if (!empty($this->currentJob
->parameters
)) {
239 $data .= "\n\nParameters raw (from db settings): \n" . $this->currentJob
->parameters
;
241 $singleRunParamsKey = strtolower($this->currentJob
->api_entity
. '_' . $this->currentJob
->api_action
);
242 if (array_key_exists($singleRunParamsKey, $this->singleRunParams
)) {
243 $data .= "\n\nParameters raw (" . $this->_source
. "): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
244 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
247 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->currentJob
->apiParams
);
250 $data .= "\n\nFull message: \n" . $message;
262 private function _apiResultToMessage($apiResult) {
263 $status = $apiResult['is_error'] ?
ts('Failure') : ts('Success');
264 $msg = CRM_Utils_Array
::value('error_message', $apiResult, 'empty error_message!');
265 $vals = CRM_Utils_Array
::value('values', $apiResult, 'empty values!');
266 if (is_array($msg)) {
267 $msg = serialize($msg);
269 if (is_array($vals)) {
270 $vals = serialize($vals);
272 $message = $apiResult['is_error'] ?
', Error message: ' . $msg : " (" . $vals . ")";
273 return $status . $message;
283 function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) {
284 throw new Exception("{$message['message']}: {$message['code']}");