ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
38  private $event_reader = null;
39 
40  protected $settings = null;
41  protected $log = null;
42  protected $db;
43 
44  private $mids = array();
45 
52  private function __construct(ilECSSetting $setting)
53  {
54  global $ilDB,$ilLog;
55 
56  $this->db = $ilDB;
57  $this->log = $ilLog;
58 
59  include_once('./Services/WebServices/ECS/classes/class.ilECSSetting.php');
60  $this->settings = $setting;
61  }
62 
75  public static function _getInstanceByServerId($a_server_id)
76  {
77  if(self::$instances[$a_server_id])
78  {
79  return self::$instances[$a_server_id];
80  }
81  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
82  return self::$instances[$a_server_id] =
85  );
86  }
87 
91  public static function start()
92  {
93  include_once './Services/Context/classes/class.ilContext.php';
95  {
96  return;
97  }
98 
99  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
101  foreach($servers->getServers() as $server)
102  {
103  $sched = new ilECSTaskScheduler($server);
104  if($sched->checkNextExecution())
105  {
106  $sched->initNextExecution();
107  }
108  }
109  }
110 
114  public static function startExecution()
115  {
116  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
118  foreach($server->getServers() as $server)
119  {
120  $sched = new ilECSTaskScheduler($server);
121  $sched->startTaskExecution();
122  }
123  }
124 
129  public function getServer()
130  {
131  return $this->settings;
132  }
133 
134 
141  public function startTaskExecution()
142  {
143  global $ilLog;
144 
145  try
146  {
147  $this->readMIDs();
148  $this->readEvents();
149  $this->handleEvents();
150 
151  $this->handleDeprecatedAccounts();
152  }
153  catch(ilException $exc)
154  {
155  $this->log->write(__METHOD__.': Caught exception: '.$exc->getMessage());
156  return false;
157  }
158  return true;
159  }
160 
167  private function readEvents()
168  {
169  try
170  {
171  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
172  $this->event_reader = new ilECSEventQueueReader($this->getServer()->getServerId());
173  $this->event_reader->refresh();
174  }
175  catch(ilException $exc)
176  {
177  throw $exc;
178  }
179  }
180 
187  private function handleEvents()
188  {
189  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
190 
191  for($i = 0;$i < self::MAX_TASKS;$i++)
192  {
193  if(!$event = $this->event_reader->shift())
194  {
195  $this->log->write(__METHOD__.': No more pending events found. DONE');
196  break;
197  }
198 
199  $this->log->write(print_r($event, true));
200 
201  // determine event handler
202 
203  $event_ignored = false;
204  switch($event['type'])
205  {
214  include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
215  $handler = ilRemoteObjectBase::getInstanceByEventType($event['type']);
216  $this->log->write("got handler ".get_class($handler));
217  break;
218 
220  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTreeCommandQueueHandler.php';
221  $handler = new ilECSCmsTreeCommandQueueHandler($this->getServer());
222  break;
223 
225  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseCommandQueueHandler.php';
226  $handler = new ilECSCmsCourseCommandQueueHandler($this->getServer());
227  break;
228 
230  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseMemberCommandQueueHandler.php';
231  $handler = new ilECSCmsCourseMemberCommandQueueHandler($this->getServer());
232  break;
233 
235  $this->log->write(__METHOD__.': Ignoring event type in queue '.$event['type']);
236  $event_ignored = true;
237  break;
238 
240  include_once './Services/WebServices/ECS/classes/Connectors/class.ilECSEnrolmentStatusCommandQueueHandler.php';
241  $handler = new ilECSEnrolmentStatusCommandQueueHandler($this->getServer());
242  break;
243 
244  default:
245  ilLoggerFactory::getRootLogger()->warning('Unknown type in queue, raising new event handling event: '. $event['type']);
246  $event_ignored = true;
247 
248  $GLOBALS['ilAppEventHandler']->raise(
249  'Services/WebServices/ECS',
250  'newEcsEvent',
251  array('event' => $event)
252  );
253  break;
254  }
255 
256  if($event_ignored)
257  {
258  $this->event_reader->delete($event['event_id']);
259  continue;
260  }
261 
262  $res = false;
263  switch($event['op'])
264  {
266  // DEPRECATED?
267  // $this->handleNewlyCreate($event['id']);
268  // $this->log->write(__METHOD__.': Handling new creation. DONE');
269  break;
270 
272  $res = $handler->handleDelete($this->getServer(), $event['id'],$this->mids);
273  $this->log->write(__METHOD__.': Handling delete. DONE');
274  break;
275 
276  case ilECSEvent::CREATED:
277  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
278  $this->log->write(__METHOD__.': Handling create. DONE');
279  break;
280 
281  case ilECSEvent::UPDATED:
282  $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
283  $this->log->write(__METHOD__.': Handling update. DONE');
284  break;
285 
286  default:
287  $this->log->write(__METHOD__.': Unknown event operation in queue '.$event['op']);
288  break;
289  }
290  if($res)
291  {
292  $this->log->write(__METHOD__.': Processing of event done '.$event['event_id']);
293  $this->event_reader->delete($event['event_id']);
294  }
295  else
296  {
297  $this->log->write(__METHOD__.': Processing of event failed '.$event['event_id']);
298  }
299  }
300  }
301 
308  private function handleDeprecatedAccounts()
309  {
310  global $ilDB;
311 
312  $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' ".
313  "AND time_limit_until < ".time()." ".
314  "AND time_limit_unlimited = 0 ".
315  "AND (time_limit_until - time_limit_from) < 7200";
316  $res = $ilDB->query($query);
317  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
318  {
319  if($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id,false))
320  {
321  $this->log->write(__METHOD__.': Deleting deprecated ECS user account '.$user_obj->getLogin());
322  $user_obj->delete();
323  }
324  // only one user
325  break;
326  }
327  return true;
328  }
329 
336  private function readMIDs()
337  {
338  try
339  {
340  $this->mids = array();
341 
342  include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
344  foreach($reader->getCommunities() as $com)
345  {
346  foreach($com->getParticipants() as $part)
347  {
348  if($part->isSelf())
349  {
350  $this->mids[] = $part->getMID();
351  #$this->log->write('Fetch MID: '.$part->getMID());
352  }
353  }
354  }
355  }
356  catch(ilException $exc)
357  {
358  throw $exc;
359  }
360  }
361 
362 
369  public function checkNextExecution()
370  {
371  global $ilLog, $ilDB;
372 
373 
374  if(!$this->settings->isEnabled())
375  {
376  return false;
377  }
378 
379  if(!$this->settings->checkImportId())
380  {
381  $this->log->write(__METHOD__.': Import ID is deleted or not of type "category". Aborting');
382  return false;
383  }
384 
385  // check next task excecution time:
386  // If it's greater than time() directly increase this value with the polling time
387  /* synchronized { */
388  $query = 'UPDATE settings SET '.
389  'value = '.$ilDB->quote(time() + $this->settings->getPollingTime(),'text').' '.
390  'WHERE module = '.$ilDB->quote('ecs','text').' '.
391  'AND keyword = '.$ilDB->quote('next_execution_'.$this->settings->getServerId(),'text').' '.
392  'AND value < '.$ilDB->quote(time(),'text');
393  $affected_rows = $ilDB->manipulate($query);
394  /* } */
395 
396 
397  if(!$affected_rows)
398  {
399  // Nothing to do
400  return false;
401  }
402  return true;
403  }
404 
405 
409  protected function initNextExecution()
410  {
411  global $ilLog;
412 
413  // Start task execution as backend process
414  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
415 
416  $soap_client = new ilSoapClient();
417  $soap_client->setResponseTimeout(1);
418  $soap_client->enableWSDL(true);
419 
420  #$ilLog->write(__METHOD__.': Trying to call Soap client...');
421  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
422  $client_id = $_COOKIE['ilClientId'];
423 
424  if($soap_client->init() and 0)
425  {
426  $ilLog->write(__METHOD__.': Calling soap handleECSTasks method...');
427  $res = $soap_client->call('handleECSTasks',array($new_session_id.'::'.$client_id,$this->settings->getServerId()));
428  }
429  else
430  {
431  $ilLog->write(__METHOD__.': SOAP call failed. Calling clone method manually. ');
432  include_once('./webservice/soap/include/inc.soap_functions.php');
433  $res = ilSoapFunctions::handleECSTasks($new_session_id.'::'.$client_id,$this->settings->getServerId());
434  }
435  }
436 }
437 ?>
static startExecution()
Static version iterates over all active instances.
Base class for ILIAS Exception handling.
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
readMIDs()
Read MID&#39;s of this installation.
getServer()
Get server setting.
static getInstance()
Get singleton instance.
$_COOKIE["ilClientId"]
Definition: cron.php:11
static handleECSTasks($sid, $a_server_id)
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
handleDeprecatedAccounts()
Delete deprecate ECS accounts.
static getInstanceByServerId($a_server_id)
Get instance by server id.
static start()
Start task scheduler for each server instance.
Reads ECS events and stores them in the database.
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...
$server
static _duplicate($a_session_id)
Duplicate session.
global $ilDB
__construct(ilECSSetting $setting)
Singleton constructor.
const CONTEXT_WEB
static getType()
Get context type.
$client_id
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
static getRootLogger()
The unique root logger has a fixed error level.