3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 class CRM_Core_JobManager
{
40 * @var array ($id => CRM_Core_ScheduledJob)
45 * @var CRM_Core_ScheduledJob
47 var $currentJob = NULL;
49 var $singleRunParams = array();
64 public function __construct() {
65 $config = CRM_Core_Config
::singleton();
66 $config->fatalErrorHandler
= 'CRM_Core_JobManager_scheduledJobFatalErrorHandler';
68 $this->jobs
= $this->_getJobs();
80 public function execute($auth = TRUE) {
82 $this->logEntry('Starting scheduled jobs execution');
84 if ($auth && !CRM_Utils_System
::authenticateKey(TRUE)) {
85 $this->logEntry('Could not authenticate the site key.');
87 require_once 'api/api.php';
89 // it's not asynchronous at this stage
90 CRM_Utils_Hook
::cron($this);
91 foreach ($this->jobs
as $job) {
92 if ($job->is_active
) {
93 if ($job->needsRunning()) {
94 $this->executeJob($job);
98 $this->logEntry('Finishing scheduled jobs execution.');
108 public function __destruct() {}
114 public function executeJobByAction($entity, $action) {
115 $job = $this->_getJob(NULL, $entity, $action);
116 $this->executeJob($job);
122 public function executeJobById($id) {
123 $job = $this->_getJob($id);
124 $this->executeJob($job);
128 * @param CRM_Core_ScheduledJob $job
130 public function executeJob($job) {
131 $this->currentJob
= $job;
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);
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;
155 * Retrieves the list of jobs from the database,
156 * populates class param.
159 * @return array ($id => CRM_Core_ScheduledJob)
166 private function _getJobs() {
168 $dao = new CRM_Core_DAO_Job();
169 $dao->orderBy('name');
170 $dao->domain_id
= CRM_Core_Config
::domainID();
172 while ($dao->fetch()) {
174 CRM_Core_DAO
::storeValues($dao, $temp);
175 $jobs[$dao->id
] = new CRM_Core_ScheduledJob($temp);
181 * Retrieves specific job from the database by id
182 * and creates ScheduledJob object.
190 * @param null $entity
191 * @param null $action
193 * @return CRM_Core_ScheduledJob
196 private function _getJob($id = NULL, $entity = NULL, $action = NULL) {
197 if (is_null($id) && is_null($action)) {
198 CRM_Core_Error
::fatal('You need to provide either id or name to use this method');
200 $dao = new CRM_Core_DAO_Job();
202 $dao->api_entity
= $entity;
203 $dao->api_action
= $action;
205 while ($dao->fetch()) {
206 CRM_Core_DAO
::storeValues($dao, $temp);
207 $job = new CRM_Core_ScheduledJob($temp);
216 * @param null $source
218 public function setSingleRunParams($entity, $job, $params, $source = NULL) {
219 $this->_source
= $source;
220 $key = strtolower($entity . '_' . $job);
221 $this->singleRunParams
[$key] = $params;
222 $this->singleRunParams
[$key]['version'] = 3;
227 * @return array|null collection of permissions, null if none
234 public function logEntry($message) {
235 $domainID = CRM_Core_Config
::domainID();
236 $dao = new CRM_Core_DAO_JobLog();
238 $dao->domain_id
= $domainID;
239 $dao->description
= substr($message, 0, 235);
240 if (strlen($message) > 235) {
241 $dao->description
.= " (...)";
243 if ($this->currentJob
) {
244 $dao->job_id
= $this->currentJob
->id
;
245 $dao->name
= $this->currentJob
->name
;
246 $dao->command
= ts("Entity:") . " " +
$this->currentJob
->api_entity +
" " . ts("Action:") . " " +
$this->currentJob
->api_action
;
248 if (!empty($this->currentJob
->parameters
)) {
249 $data .= "\n\nParameters raw (from db settings): \n" . $this->currentJob
->parameters
;
251 $singleRunParamsKey = strtolower($this->currentJob
->api_entity
. '_' . $this->currentJob
->api_action
);
252 if (array_key_exists($singleRunParamsKey, $this->singleRunParams
)) {
253 $data .= "\n\nParameters raw (" . $this->_source
. "): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
254 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
257 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->currentJob
->apiParams
);
260 $data .= "\n\nFull message: \n" . $message;
272 private function _apiResultToMessage($apiResult) {
273 $status = $apiResult['is_error'] ?
ts('Failure') : ts('Success');
274 $msg = CRM_Utils_Array
::value('error_message', $apiResult, 'empty error_message!');
275 $vals = CRM_Utils_Array
::value('values', $apiResult, 'empty values!');
276 if (is_array($msg)) {
277 $msg = serialize($msg);
279 if (is_array($vals)) {
280 $vals = serialize($vals);
282 $message = $apiResult['is_error'] ?
', Error message: ' . $msg : " (" . $vals . ")";
283 return $status . $message;
292 function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) {
293 throw new Exception("{$message['message']}: {$message['code']}");