commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Core / JobManager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 * $Id$
35 *
36 */
37 class CRM_Core_JobManager {
38
39 /**
40 * @var array ($id => CRM_Core_ScheduledJob)
41 */
42 var $jobs = NULL;
43
44 /**
45 * @var CRM_Core_ScheduledJob
46 */
47 var $currentJob = NULL;
48
49 var $singleRunParams = array();
50
51 var $_source = NULL;
52
53
54 /**
55 * Class constructor.
56 *
57 * @return void
58 */
59 public function __construct() {
60 $config = CRM_Core_Config::singleton();
61 $config->fatalErrorHandler = 'CRM_Core_JobManager_scheduledJobFatalErrorHandler';
62
63 $this->jobs = $this->_getJobs();
64 }
65
66 /**
67 * @param bool $auth
68 */
69 public function execute($auth = TRUE) {
70
71 $this->logEntry('Starting scheduled jobs execution');
72
73 if ($auth && !CRM_Utils_System::authenticateKey(TRUE)) {
74 $this->logEntry('Could not authenticate the site key.');
75 }
76 require_once 'api/api.php';
77
78 // it's not asynchronous at this stage
79 CRM_Utils_Hook::cron($this);
80 foreach ($this->jobs as $job) {
81 if ($job->is_active) {
82 if ($job->needsRunning()) {
83 $this->executeJob($job);
84 }
85 }
86 }
87 $this->logEntry('Finishing scheduled jobs execution.');
88 }
89
90 /**
91 * Class destructor.
92 */
93 public function __destruct() {
94 }
95
96 /**
97 * @param $entity
98 * @param $action
99 */
100 public function executeJobByAction($entity, $action) {
101 $job = $this->_getJob(NULL, $entity, $action);
102 $this->executeJob($job);
103 }
104
105 /**
106 * @param int $id
107 */
108 public function executeJobById($id) {
109 $job = $this->_getJob($id);
110 $this->executeJob($job);
111 }
112
113 /**
114 * @param CRM_Core_ScheduledJob $job
115 */
116 public function executeJob($job) {
117 $this->currentJob = $job;
118 $this->logEntry('Starting execution of ' . $job->name);
119 $job->saveLastRun();
120
121 $singleRunParamsKey = strtolower($job->api_entity . '_' . $job->api_action);
122
123 if (array_key_exists($singleRunParamsKey, $this->singleRunParams)) {
124 $params = $this->singleRunParams[$singleRunParamsKey];
125 }
126 else {
127 $params = $job->apiParams;
128 }
129
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 }
136 $this->logEntry('Finished execution of ' . $job->name . ' with result: ' . $this->_apiResultToMessage($result));
137 $this->currentJob = FALSE;
138 }
139
140 /**
141 * Retrieves the list of jobs from the database,
142 * populates class param.
143 *
144 * @return array
145 * ($id => CRM_Core_ScheduledJob)
146 */
147 private function _getJobs() {
148 $jobs = array();
149 $dao = new CRM_Core_DAO_Job();
150 $dao->orderBy('name');
151 $dao->domain_id = CRM_Core_Config::domainID();
152 $dao->find();
153 while ($dao->fetch()) {
154 $temp = array();
155 CRM_Core_DAO::storeValues($dao, $temp);
156 $jobs[$dao->id] = new CRM_Core_ScheduledJob($temp);
157 }
158 return $jobs;
159 }
160
161 /**
162 * Retrieves specific job from the database by id.
163 * and creates ScheduledJob object.
164 *
165 * @param int $id
166 * @param null $entity
167 * @param null $action
168 *
169 * @return CRM_Core_ScheduledJob
170 * @throws Exception
171 */
172 private function _getJob($id = NULL, $entity = NULL, $action = NULL) {
173 if (is_null($id) && is_null($action)) {
174 CRM_Core_Error::fatal('You need to provide either id or name to use this method');
175 }
176 $dao = new CRM_Core_DAO_Job();
177 $dao->id = $id;
178 $dao->api_entity = $entity;
179 $dao->api_action = $action;
180 $dao->find();
181 while ($dao->fetch()) {
182 CRM_Core_DAO::storeValues($dao, $temp);
183 $job = new CRM_Core_ScheduledJob($temp);
184 }
185 return $job;
186 }
187
188 /**
189 * @param $entity
190 * @param $job
191 * @param array $params
192 * @param null $source
193 */
194 public function setSingleRunParams($entity, $job, $params, $source = NULL) {
195 $this->_source = $source;
196 $key = strtolower($entity . '_' . $job);
197 $this->singleRunParams[$key] = $params;
198 $this->singleRunParams[$key]['version'] = 3;
199 }
200
201 /**
202 * @param string $message
203 *
204 * @return void
205 * collection of permissions, null if none
206 */
207 public function logEntry($message) {
208 $domainID = CRM_Core_Config::domainID();
209 $dao = new CRM_Core_DAO_JobLog();
210
211 $dao->domain_id = $domainID;
212 $dao->description = substr($message, 0, 235);
213 if (strlen($message) > 235) {
214 $dao->description .= " (...)";
215 }
216 if ($this->currentJob) {
217 $dao->job_id = $this->currentJob->id;
218 $dao->name = $this->currentJob->name;
219 $dao->command = ts("Entity:") . " " . $this->currentJob->api_entity . " " . ts("Action:") . " " . $this->currentJob->api_action;
220 $data = "";
221 if (!empty($this->currentJob->parameters)) {
222 $data .= "\n\nParameters raw (from db settings): \n" . $this->currentJob->parameters;
223 }
224 $singleRunParamsKey = strtolower($this->currentJob->api_entity . '_' . $this->currentJob->api_action);
225 if (array_key_exists($singleRunParamsKey, $this->singleRunParams)) {
226 $data .= "\n\nParameters raw (" . $this->_source . "): \n" . serialize($this->singleRunParams[$singleRunParamsKey]);
227 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->singleRunParams[$singleRunParamsKey]);
228 }
229 else {
230 $data .= "\n\nParameters parsed (and passed to API method): \n" . serialize($this->currentJob->apiParams);
231 }
232
233 $data .= "\n\nFull message: \n" . $message;
234
235 $dao->data = $data;
236 }
237 $dao->save();
238 }
239
240 /**
241 * @param $apiResult
242 *
243 * @return string
244 */
245 private function _apiResultToMessage($apiResult) {
246 $status = $apiResult['is_error'] ? ts('Failure') : ts('Success');
247 $msg = CRM_Utils_Array::value('error_message', $apiResult, 'empty error_message!');
248 $vals = CRM_Utils_Array::value('values', $apiResult, 'empty values!');
249 if (is_array($msg)) {
250 $msg = serialize($msg);
251 }
252 if (is_array($vals)) {
253 $vals = serialize($vals);
254 }
255 $message = $apiResult['is_error'] ? ', Error message: ' . $msg : " (" . $vals . ")";
256 return $status . $message;
257 }
258
259 }
260
261 /**
262 * @param $message
263 *
264 * @throws Exception
265 */
266 function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) {
267 throw new Exception("{$message['message']}: {$message['code']}");
268 }