ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilECSTaskScheduler.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
33{
34 const MAX_TASKS = 30;
35
36 private static $instances = array();
37
41 protected $log;
42
43 private $event_reader = null;
44
45 protected $settings = null;
46 protected $db;
47
48 private $mids = array();
49
56 private function __construct(ilECSSetting $setting)
57 {
58 global $DIC;
59
60 $ilDB = $DIC['ilDB'];
61 $ilLog = $DIC['ilLog'];
62
63 $this->db = $ilDB;
64
65 $this->log = $GLOBALS['DIC']->logger()->wsrv();
66
67 include_once('./Services/WebServices/ECS/classes/class.ilECSSetting.php');
68 $this->settings = $setting;
69 }
70
83 public static function _getInstanceByServerId($a_server_id)
84 {
85 if (self::$instances[$a_server_id]) {
86 return self::$instances[$a_server_id];
87 }
88 include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
89 return self::$instances[$a_server_id] =
92 );
93 }
94
98 public static function start()
99 {
100 include_once './Services/Context/classes/class.ilContext.php';
102 return;
103 }
104
105 include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
107 foreach ($servers->getServers() as $server) {
108 $sched = new ilECSTaskScheduler($server);
109 if ($sched->checkNextExecution()) {
110 $sched->initNextExecution();
111 }
112 }
113 }
114
118 public static function startExecution()
119 {
120 include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
122 foreach ($server->getServers() as $server) {
123 $sched = new ilECSTaskScheduler($server);
124 $sched->startTaskExecution();
125 }
126 }
127
132 public function getServer()
133 {
134 return $this->settings;
135 }
136
137
144 public function startTaskExecution()
145 {
146 global $DIC;
147
148 $ilLog = $DIC['ilLog'];
149
150 try {
151 $this->readMIDs();
152 $this->readEvents();
153 $this->handleEvents();
154
156 } catch (ilException $exc) {
157 $this->log->warning('Cannot start ecs task execution: ' . $exc->getMessage());
158 return false;
159 }
160 return true;
161 }
162
169 private function readEvents()
170 {
171 try {
172 include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
173 $this->event_reader = new ilECSEventQueueReader($this->getServer()->getServerId());
174 $this->event_reader->refresh();
175 } catch (ilException $exc) {
176 throw $exc;
177 }
178 }
179
186 private function handleEvents()
187 {
188 include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
189
190 for ($i = 0;$i < self::MAX_TASKS;$i++) {
191 if (!$event = $this->event_reader->shift()) {
192 $this->log->write(__METHOD__ . ': No more pending events found. DONE');
193 break;
194 }
195
196 $this->log->write(print_r($event, true));
197
198 // determine event handler
199
200 $event_ignored = false;
201 switch ($event['type']) {
210 include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
211 $handler = ilRemoteObjectBase::getInstanceByEventType($event['type']);
212 $this->log->write("got handler " . get_class($handler));
213 break;
214
216 $this->log->debug('Handling new cms tree event.');
217 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTreeCommandQueueHandler.php';
218 $handler = new ilECSCmsTreeCommandQueueHandler($this->getServer());
219 break;
220
222 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseCommandQueueHandler.php';
223 $handler = new ilECSCmsCourseCommandQueueHandler($this->getServer());
224 break;
225
227 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseMemberCommandQueueHandler.php';
228 $handler = new ilECSCmsCourseMemberCommandQueueHandler($this->getServer());
229 break;
230
232 $this->log->write(__METHOD__ . ': Ignoring event type in queue ' . $event['type']);
233 $event_ignored = true;
234 break;
235
237 include_once './Services/WebServices/ECS/classes/Connectors/class.ilECSEnrolmentStatusCommandQueueHandler.php';
238 $handler = new ilECSEnrolmentStatusCommandQueueHandler($this->getServer());
239 break;
240
241 default:
242
243 $this->log->warning('Unknown type in queue, raising new event handling event: ' . $event['type']);
244 $event_ignored = true;
245
246 $GLOBALS['DIC']['ilAppEventHandler']->raise(
247 'Services/WebServices/ECS',
248 'newEcsEvent',
249 array('event' => $event)
250 );
251 break;
252 }
253
254 if ($event_ignored) {
255 $this->event_reader->delete($event['event_id']);
256 continue;
257 }
258
259 $res = false;
260 switch ($event['op']) {
262 // DEPRECATED?
263 // $this->handleNewlyCreate($event['id']);
264 // $this->log->write(__METHOD__.': Handling new creation. DONE');
265 break;
266
268 $res = $handler->handleDelete($this->getServer(), $event['id'], $this->mids);
269 $this->log->write(__METHOD__ . ': Handling delete. DONE');
270 break;
271
273 $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
274 $this->log->write(__METHOD__ . ': Handling create. DONE');
275 break;
276
278 $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
279 $this->log->write(__METHOD__ . ': Handling update. DONE');
280 break;
281
282 default:
283 $this->log->write(__METHOD__ . ': Unknown event operation in queue ' . $event['op']);
284 break;
285 }
286 if ($res) {
287 $this->log->write(__METHOD__ . ': Processing of event done ' . $event['event_id']);
288 $this->event_reader->delete($event['event_id']);
289 } else {
290 $this->log->write(__METHOD__ . ': Processing of event failed ' . $event['event_id']);
291 }
292 }
293 }
294
301 private function handleDeprecatedAccounts()
302 {
303 global $DIC;
304
305 $ilDB = $DIC['ilDB'];
306
307 $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' " .
308 "AND time_limit_until < " . time() . " " .
309 "AND time_limit_unlimited = 0 " .
310 "AND (time_limit_until - time_limit_from) < 7200";
311 $res = $ilDB->query($query);
312 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
313 if ($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id, false)) {
314 $this->log->write(__METHOD__ . ': Deleting deprecated ECS user account ' . $user_obj->getLogin());
315 $user_obj->delete();
316 }
317 // only one user
318 break;
319 }
320 return true;
321 }
322
329 private function readMIDs()
330 {
331 try {
332 $this->mids = array();
333
334 include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
336 foreach ($reader->getCommunities() as $com) {
337 foreach ($com->getParticipants() as $part) {
338 if ($part->isSelf()) {
339 $this->mids[] = $part->getMID();
340 }
341 }
342 }
343 } catch (ilException $exc) {
344 throw $exc;
345 }
346 }
347
348
355 public function checkNextExecution()
356 {
357 global $DIC;
358
359 $ilDB = $DIC['ilDB'];
360
361
362 if (!$this->settings->isEnabled()) {
363 return false;
364 }
365
366 if (!$this->settings->checkImportId()) {
367 $this->log->warning('Import ID is deleted or not of type "category". Aborting');
368 return false;
369 }
370
371 // check next task excecution time:
372 // If it's greater than time() directly increase this value with the polling time
373 /* synchronized { */
374 $query = 'UPDATE settings SET ' .
375 'value = ' . $ilDB->quote(time() + $this->settings->getPollingTime(), 'text') . ' ' .
376 'WHERE module = ' . $ilDB->quote('ecs', 'text') . ' ' .
377 'AND keyword = ' . $ilDB->quote('next_execution_' . $this->settings->getServerId(), 'text') . ' ' .
378 'AND value < ' . $ilDB->quote(time(), 'text');
379 $affected_rows = $ilDB->manipulate($query);
380 /* } */
381
382
383 if (!$affected_rows) {
384 // Nothing to do
385 return false;
386 }
387 return true;
388 }
389
390
394 protected function initNextExecution()
395 {
396 global $DIC;
397
398 $ilLog = $DIC['ilLog'];
399
400 // Start task execution as backend process
401 include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
402
403 $soap_client = new ilSoapClient();
404 $soap_client->setResponseTimeout(1);
405 $soap_client->enableWSDL(true);
406
407 $new_session_id = ilSession::_duplicate($_COOKIE[session_name()]);
408 $client_id = $_COOKIE['ilClientId'];
409
410 if ($soap_client->init() and 0) {
411 $this->log->info('Calling soap handleECSTasks method...');
412 $res = $soap_client->call('handleECSTasks', array($new_session_id . '::' . $client_id,$this->settings->getServerId()));
413 } else {
414 $this->log->info('SOAP call failed. Calling clone method manually. ');
415 include_once('./webservice/soap/include/inc.soap_functions.php');
416 $res = ilSoapFunctions::handleECSTasks($new_session_id . '::' . $client_id, $this->settings->getServerId());
417 }
418 }
419}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
const CONTEXT_WEB
static getType()
Get context type.
static getInstanceByServerId($a_server_id)
Get instance by server id.
Reads ECS events and stores them in the database.
static getInstance()
Get singleton instance.
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
getServer()
Get server setting.
__construct(ilECSSetting $setting)
Singleton constructor.
initNextExecution()
Call next task scheduler run.
static _getInstanceByServerId($a_server_id)
get singleton instance Private access use ilECSTaskScheduler::start() or ilECSTaskScheduler::startTas...
static start()
Start task scheduler for each server instance.
handleDeprecatedAccounts()
Delete deprecate ECS accounts.
static startExecution()
Static version iterates over all active instances.
readMIDs()
Read MID's of this installation.
Base class for ILIAS Exception handling.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
static _duplicate($a_session_id)
Duplicate session.
static handleECSTasks($sid, $a_server_id)
$server
$client_id
$i
Definition: metadata.php:24
$query
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
global $ilDB
$DIC
Definition: xapitoken.php:46
$_COOKIE[session_name()]
Definition: xapitoken.php:39