3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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();
61 public function __construct() {
62 $config = CRM_Core_Config
::singleton();
63 $config->fatalErrorHandler
= 'CRM_Core_JobManager_scheduledJobFatalErrorHandler';
65 $this->jobs
= $this->_getJobs();
75 public function execute($auth = TRUE) {
77 $this->logEntry('Starting scheduled jobs execution');
79 if ($auth && !CRM_Utils_System
::authenticateKey(TRUE)) {
80 $this->logEntry('Could not authenticate the site key.');
82 require_once 'api/api.php';
84 // it's not asynchronous at this stage
85 CRM_Utils_Hook
::cron($this);
86 foreach ($this->jobs
as $job) {
87 if ($job->is_active
) {
88 if ($job->needsRunning()) {
89 $this->executeJob($job);
93 $this->logEntry('Finishing scheduled jobs execution.');
101 public function __destruct() {}
107 public function executeJobByAction($entity, $action) {
108 $job = $this->_getJob(NULL, $entity, $action);
109 $this->executeJob($job);
115 public function executeJobById($id) {
116 $job = $this->_getJob($id);
117 $this->executeJob($job);
121 * @param CRM_Core_ScheduledJob $job
123 public function executeJob($job) {
124 $this->currentJob
= $job;
125 $this->logEntry('Starting execution of ' . $job->name
);
128 $singleRunParamsKey = strtolower($job->api_entity
. '_' . $job->api_action
);
130 if (array_key_exists($singleRunParamsKey, $this->singleRunParams
)) {
131 $params = $this->singleRunParams
[$singleRunParamsKey];
134 $params = $job->apiParams
;
138 $result = civicrm_api($job->api_entity
, $job->api_action
, $params);
141 $this->logEntry('Error while executing ' . $job->name
. ': ' . $e->getMessage());
143 $this->logEntry('Finished execution of ' . $job->name
. ' with result: ' . $this->_apiResultToMessage($result));
144 $this->currentJob
= FALSE;
148 * Retrieves the list of jobs from the database,
149 * populates class param.
153 * ($id => CRM_Core_ScheduledJob)
158 private function _getJobs() {
160 $dao = new CRM_Core_DAO_Job();
161 $dao->orderBy('name');
162 $dao->domain_id
= CRM_Core_Config
::domainID();
164 while ($dao->fetch()) {
166 CRM_Core_DAO
::storeValues($dao, $temp);
167 $jobs[$dao->id
] = new CRM_Core_ScheduledJob($temp);
173 * Retrieves specific job from the database by id
174 * and creates ScheduledJob object.
180 * @param null $entity
181 * @param null $action
183 * @return CRM_Core_ScheduledJob
186 private function _getJob($id = NULL, $entity = NULL, $action = NULL) {
187 if (is_null($id) && is_null($action)) {
188 CRM_Core_Error
::fatal('You need to provide either id or name to use this method');
190 $dao = new CRM_Core_DAO_Job();
192 $dao->api_entity
= $entity;
193 $dao->api_action
= $action;
195 while ($dao->fetch()) {
196 CRM_Core_DAO
::storeValues($dao, $temp);
197 $job = new CRM_Core_ScheduledJob($temp);
205 * @param array $params
206 * @param null $source
208 public function setSingleRunParams($entity, $job, $params, $source = NULL) {
209 $this->_source
= $source;
210 $key = strtolower($entity . '_' . $job);
211 $this->singleRunParams
[$key] = $params;
212 $this->singleRunParams
[$key]['version'] = 3;
217 * @return array|null collection of permissions, null if none
222 public function logEntry($message) {
223 $domainID = CRM_Core_Config
::domainID();
224 $dao = new CRM_Core_DAO_JobLog();
226 $dao->domain_id
= $domainID;
227 $dao->description
= substr($message, 0, 235);
228 if (strlen($message) > 235) {
229 $dao->description
.= " (...)";
231 if ($this->currentJob
) {
232 $dao->job_id
= $this->currentJob
->id
;
233 $dao->name
= $this->currentJob
->name
;
234 $dao->command
= ts("Entity:") . " " . $this->currentJob
->api_entity
. " " . ts("Action:") . " " . $this->currentJob
->api_action
;
236 if (!empty($this->currentJob
->parameters
)) {
237 $data .= "\n\nParameters raw (from db settings): \n" . $this->currentJob
->parameters
;
239 $singleRunParamsKey = strtolower($this->currentJob
->api_entity
. '_' . $this->currentJob
->api_action
);
240 if (array_key_exists($singleRunParamsKey, $this->singleRunParams
)) {
241 $data .= "\n\nParameters raw (" . $this->_source
. "): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
242 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->singleRunParams
[$singleRunParamsKey]);
245 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->currentJob
->apiParams
);
248 $data .= "\n\nFull message: \n" . $message;
260 private function _apiResultToMessage($apiResult) {
261 $status = $apiResult['is_error'] ?
ts('Failure') : ts('Success');
262 $msg = CRM_Utils_Array
::value('error_message', $apiResult, 'empty error_message!');
263 $vals = CRM_Utils_Array
::value('values', $apiResult, 'empty values!');
264 if (is_array($msg)) {
265 $msg = serialize($msg);
267 if (is_array($vals)) {
268 $vals = serialize($vals);
270 $message = $apiResult['is_error'] ?
', Error message: ' . $msg : " (" . $vals . ")";
271 return $status . $message;
280 function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) {
281 throw new Exception("{$message['message']}: {$message['code']}");