Merge branch 4.5 into master
[civicrm-core.git] / CRM / Core / JobManager.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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
06b69b18 33 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
34 * $Id$
35 *
36 */
37class 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
d424ffde 54 /**
6a488035 55 * Class constructor
cbb7c7e0 56 *
bed98343 57 * @return void
6a488035
TO
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
a0ee3941
EM
66 /**
67 * @param bool $auth
68 */
6a488035
TO
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
d424ffde 90 /**
6a488035 91 * Class destructor
6a488035 92 */
6ea503d4
TO
93 public function __destruct() {
94 }
6a488035 95
a0ee3941
EM
96 /**
97 * @param $entity
98 * @param $action
99 */
6a488035
TO
100 public function executeJobByAction($entity, $action) {
101 $job = $this->_getJob(NULL, $entity, $action);
102 $this->executeJob($job);
103 }
104
a0ee3941 105 /**
100fef9d 106 * @param int $id
a0ee3941 107 */
6a488035
TO
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 }
353ffa53 133 catch (Exception$e) {
6a488035
TO
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
d424ffde 140 /**
6a488035
TO
141 * Retrieves the list of jobs from the database,
142 * populates class param.
cbb7c7e0 143 *
a6c01b45
CW
144 * @return array
145 * ($id => CRM_Core_ScheduledJob)
6a488035
TO
146 */
147 private function _getJobs() {
148 $jobs = array();
149 $dao = new CRM_Core_DAO_Job();
150 $dao->orderBy('name');
2f7a6dd7 151 $dao->domain_id = CRM_Core_Config::domainID();
6a488035
TO
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
d424ffde 161 /**
6a488035
TO
162 * Retrieves specific job from the database by id
163 * and creates ScheduledJob object.
cbb7c7e0 164 *
100fef9d 165 * @param int $id
a0ee3941
EM
166 * @param null $entity
167 * @param null $action
168 *
169 * @return CRM_Core_ScheduledJob
170 * @throws Exception
171 */
6a488035
TO
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 }
353ffa53
TO
176 $dao = new CRM_Core_DAO_Job();
177 $dao->id = $id;
6a488035
TO
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
a0ee3941
EM
188 /**
189 * @param $entity
190 * @param $job
c490a46a 191 * @param array $params
a0ee3941
EM
192 * @param null $source
193 */
6a488035
TO
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 }
cbb7c7e0 200
d424ffde
CW
201 /**
202 * @param string $message
6a488035 203 *
bed98343 204 * @return void
72b3a70c 205 * collection of permissions, null if none
6a488035
TO
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) {
353ffa53
TO
217 $dao->job_id = $this->currentJob->id;
218 $dao->name = $this->currentJob->name;
fe901a30 219 $dao->command = ts("Entity:") . " " . $this->currentJob->api_entity . " " . ts("Action:") . " " . $this->currentJob->api_action;
353ffa53 220 $data = "";
6a488035
TO
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
a0ee3941
EM
240 /**
241 * @param $apiResult
242 *
243 * @return string
244 */
6a488035
TO
245 private function _apiResultToMessage($apiResult) {
246 $status = $apiResult['is_error'] ? ts('Failure') : ts('Success');
353ffa53
TO
247 $msg = CRM_Utils_Array::value('error_message', $apiResult, 'empty error_message!');
248 $vals = CRM_Utils_Array::value('values', $apiResult, 'empty values!');
6a488035
TO
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 }
96025800 258
6a488035
TO
259}
260
a0ee3941
EM
261/**
262 * @param $message
263 *
264 * @throws Exception
265 */
6a488035
TO
266function CRM_Core_JobManager_scheduledJobFatalErrorHandler($message) {
267 throw new Exception("{$message['message']}: {$message['code']}");
268}