ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
155  $this->handleDeprecatedAccounts();
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';
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';
219  break;
220 
222  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseCommandQueueHandler.php';
224  break;
225 
227  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseMemberCommandQueueHandler.php';
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';
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 
272  case ilECSEvent::CREATED:
273  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
274  $this->log->write(__METHOD__ . ': Handling create. DONE');
275  break;
276 
277  case ilECSEvent::UPDATED:
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  if (!$this->settings->getPollingTime()) {
371  return false;
372  }
373 
374  // check next task excecution time:
375  // If it's greater than time() directly increase this value with the polling time
376  /* synchronized { */
377  $query = 'UPDATE settings SET ' .
378  'value = ' . $ilDB->quote(time() + $this->settings->getPollingTime(), 'text') . ' ' .
379  'WHERE module = ' . $ilDB->quote('ecs', 'text') . ' ' .
380  'AND keyword = ' . $ilDB->quote('next_execution_' . $this->settings->getServerId(), 'text') . ' ' .
381  'AND value < ' . $ilDB->quote(time(), 'text');
382  $affected_rows = $ilDB->manipulate($query);
383  /* } */
384 
385 
386  if (!$affected_rows) {
387  // Nothing to do
388  return false;
389  }
390  return true;
391  }
392 
393 
397  protected function initNextExecution()
398  {
399  global $DIC;
400 
401  $ilLog = $DIC['ilLog'];
402 
403  // Start task execution as backend process
404  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
405 
406  $soap_client = new ilSoapClient();
407  $soap_client->setResponseTimeout(1);
408  $soap_client->enableWSDL(true);
409 
410  $new_session_id = ilSession::_duplicate($_COOKIE[session_name()]);
411  $client_id = $_COOKIE['ilClientId'];
412 
413  if ($soap_client->init() and 0) {
414  $this->log->info('Calling soap handleECSTasks method...');
415  $res = $soap_client->call('handleECSTasks', array($new_session_id . '::' . $client_id,$this->settings->getServerId()));
416  } else {
417  $this->log->info('SOAP call failed. Calling clone method manually. ');
418  include_once('./webservice/soap/include/inc.soap_functions.php');
419  $res = ilSoapFunctions::handleECSTasks($new_session_id . '::' . $client_id, $this->settings->getServerId());
420  }
421  }
422 }
$_COOKIE['client_id']
Definition: server.php:9
static startExecution()
Static version iterates over all active instances.
settings()
Definition: settings.php:2
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
readMIDs()
Read MID&#39;s of this installation.
global $DIC
Definition: saml.php:7
getServer()
Get server setting.
static getInstance()
Get singleton instance.
static handleECSTasks($sid, $a_server_id)
$server
Definition: sabredav.php:48
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
handleDeprecatedAccounts()
Delete deprecate ECS accounts.
static getInstanceByServerId($a_server_id)
Get instance by server id.
static start()
Start task scheduler for each server instance.
foreach($_POST as $key=> $value) $res
Reads ECS events and stores them in the database.
$query
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
initNextExecution()
Call next task scheduler run.
static _getInstanceByServerId($a_server_id)
get singleton instance Private access use ilECSTaskScheduler::start() or ilECSTaskScheduler::startTas...
$row
static _duplicate($a_session_id)
Duplicate session.
global $ilDB
$i
Definition: disco.tpl.php:19
__construct(ilECSSetting $setting)
Singleton constructor.
const CONTEXT_WEB
static getType()
Get context type.
$client_id
$handler
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.