ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 $ilDB,$ilLog;
59 
60  $this->db = $ilDB;
61 
62  $this->log = $GLOBALS['DIC']->logger()->wsrv();
63 
64  include_once('./Services/WebServices/ECS/classes/class.ilECSSetting.php');
65  $this->settings = $setting;
66  }
67 
80  public static function _getInstanceByServerId($a_server_id)
81  {
82  if(self::$instances[$a_server_id])
83  {
84  return self::$instances[$a_server_id];
85  }
86  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
87  return self::$instances[$a_server_id] =
90  );
91  }
92 
96  public static function start()
97  {
98  include_once './Services/Context/classes/class.ilContext.php';
100  {
101  return;
102  }
103 
104  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
106  foreach($servers->getServers() as $server)
107  {
108  $sched = new ilECSTaskScheduler($server);
109  if($sched->checkNextExecution())
110  {
111  $sched->initNextExecution();
112  }
113  }
114  }
115 
119  public static function startExecution()
120  {
121  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
123  foreach($server->getServers() as $server)
124  {
125  $sched = new ilECSTaskScheduler($server);
126  $sched->startTaskExecution();
127  }
128  }
129 
134  public function getServer()
135  {
136  return $this->settings;
137  }
138 
139 
146  public function startTaskExecution()
147  {
148  global $ilLog;
149 
150  try
151  {
152  $this->readMIDs();
153  $this->readEvents();
154  $this->handleEvents();
155 
156  $this->handleDeprecatedAccounts();
157  }
158  catch(ilException $exc)
159  {
160  $this->log->warning('Cannot start ecs task execution: ' . $exc->getMessage());
161  return false;
162  }
163  return true;
164  }
165 
172  private function readEvents()
173  {
174  try
175  {
176  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
177  $this->event_reader = new ilECSEventQueueReader($this->getServer()->getServerId());
178  $this->event_reader->refresh();
179  }
180  catch(ilException $exc)
181  {
182  throw $exc;
183  }
184  }
185 
192  private function handleEvents()
193  {
194  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
195 
196  for($i = 0;$i < self::MAX_TASKS;$i++)
197  {
198  if(!$event = $this->event_reader->shift())
199  {
200  $this->log->write(__METHOD__.': No more pending events found. DONE');
201  break;
202  }
203 
204  $this->log->write(print_r($event, true));
205 
206  // determine event handler
207 
208  $event_ignored = false;
209  switch($event['type'])
210  {
219  include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
220  $handler = ilRemoteObjectBase::getInstanceByEventType($event['type']);
221  $this->log->write("got handler ".get_class($handler));
222  break;
223 
225  $this->log->debug('Handling new cms tree event.');
226  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTreeCommandQueueHandler.php';
227  $handler = new ilECSCmsTreeCommandQueueHandler($this->getServer());
228  break;
229 
231  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseCommandQueueHandler.php';
232  $handler = new ilECSCmsCourseCommandQueueHandler($this->getServer());
233  break;
234 
236  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseMemberCommandQueueHandler.php';
237  $handler = new ilECSCmsCourseMemberCommandQueueHandler($this->getServer());
238  break;
239 
241  $this->log->write(__METHOD__.': Ignoring event type in queue '.$event['type']);
242  $event_ignored = true;
243  break;
244 
246  include_once './Services/WebServices/ECS/classes/Connectors/class.ilECSEnrolmentStatusCommandQueueHandler.php';
247  $handler = new ilECSEnrolmentStatusCommandQueueHandler($this->getServer());
248  break;
249 
250  default:
251 
252  $this->log->warning('Unknown type in queue, raising new event handling event: '. $event['type']);
253  $event_ignored = true;
254 
255  $GLOBALS['ilAppEventHandler']->raise(
256  'Services/WebServices/ECS',
257  'newEcsEvent',
258  array('event' => $event)
259  );
260  break;
261  }
262 
263  if($event_ignored)
264  {
265  $this->event_reader->delete($event['event_id']);
266  continue;
267  }
268 
269  $res = false;
270  switch($event['op'])
271  {
273  // DEPRECATED?
274  // $this->handleNewlyCreate($event['id']);
275  // $this->log->write(__METHOD__.': Handling new creation. DONE');
276  break;
277 
279  $res = $handler->handleDelete($this->getServer(), $event['id'],$this->mids);
280  $this->log->write(__METHOD__.': Handling delete. DONE');
281  break;
282 
283  case ilECSEvent::CREATED:
284  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
285  $this->log->write(__METHOD__.': Handling create. DONE');
286  break;
287 
288  case ilECSEvent::UPDATED:
289  $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
290  $this->log->write(__METHOD__.': Handling update. DONE');
291  break;
292 
293  default:
294  $this->log->write(__METHOD__.': Unknown event operation in queue '.$event['op']);
295  break;
296  }
297  if($res)
298  {
299  $this->log->write(__METHOD__.': Processing of event done '.$event['event_id']);
300  $this->event_reader->delete($event['event_id']);
301  }
302  else
303  {
304  $this->log->write(__METHOD__.': Processing of event failed '.$event['event_id']);
305  }
306  }
307  }
308 
315  private function handleDeprecatedAccounts()
316  {
317  global $ilDB;
318 
319  $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' ".
320  "AND time_limit_until < ".time()." ".
321  "AND time_limit_unlimited = 0 ".
322  "AND (time_limit_until - time_limit_from) < 7200";
323  $res = $ilDB->query($query);
324  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
325  {
326  if($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id,false))
327  {
328  $this->log->write(__METHOD__.': Deleting deprecated ECS user account '.$user_obj->getLogin());
329  $user_obj->delete();
330  }
331  // only one user
332  break;
333  }
334  return true;
335  }
336 
343  private function readMIDs()
344  {
345  try
346  {
347  $this->mids = array();
348 
349  include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
351  foreach($reader->getCommunities() as $com)
352  {
353  foreach($com->getParticipants() as $part)
354  {
355  if($part->isSelf())
356  {
357  $this->mids[] = $part->getMID();
358  }
359  }
360  }
361  }
362  catch(ilException $exc)
363  {
364  throw $exc;
365  }
366  }
367 
368 
375  public function checkNextExecution()
376  {
377  global $ilDB;
378 
379 
380  if(!$this->settings->isEnabled())
381  {
382  return false;
383  }
384 
385  if(!$this->settings->checkImportId())
386  {
387  $this->log->warning('Import ID is deleted or not of type "category". Aborting');
388  return false;
389  }
390 
391  // check next task excecution time:
392  // If it's greater than time() directly increase this value with the polling time
393  /* synchronized { */
394  $query = 'UPDATE settings SET '.
395  'value = '.$ilDB->quote(time() + $this->settings->getPollingTime(),'text').' '.
396  'WHERE module = '.$ilDB->quote('ecs','text').' '.
397  'AND keyword = '.$ilDB->quote('next_execution_'.$this->settings->getServerId(),'text').' '.
398  'AND value < '.$ilDB->quote(time(),'text');
399  $affected_rows = $ilDB->manipulate($query);
400  /* } */
401 
402 
403  if(!$affected_rows)
404  {
405  // Nothing to do
406  return false;
407  }
408  return true;
409  }
410 
411 
415  protected function initNextExecution()
416  {
417  global $ilLog;
418 
419  // Start task execution as backend process
420  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
421 
422  $soap_client = new ilSoapClient();
423  $soap_client->setResponseTimeout(1);
424  $soap_client->enableWSDL(true);
425 
426  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
427  $client_id = $_COOKIE['ilClientId'];
428 
429  if($soap_client->init() and 0)
430  {
431  $this->log->info('Calling soap handleECSTasks method...');
432  $res = $soap_client->call('handleECSTasks',array($new_session_id.'::'.$client_id,$this->settings->getServerId()));
433  }
434  else
435  {
436  $this->log->info('SOAP call failed. Calling clone method manually. ');
437  include_once('./webservice/soap/include/inc.soap_functions.php');
438  $res = ilSoapFunctions::handleECSTasks($new_session_id.'::'.$client_id,$this->settings->getServerId());
439  }
440  }
441 }
442 ?>
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.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static handleECSTasks($sid, $a_server_id)
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.
Reads ECS events and stores them in the database.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Create styles array
The data for the language used.
initNextExecution()
Call next task scheduler run.
static _getInstanceByServerId($a_server_id)
get singleton instance Private access use ilECSTaskScheduler::start() or ilECSTaskScheduler::startTas...
$server
settings()
Definition: settings.php:2
static _duplicate($a_session_id)
Duplicate session.
global $ilDB
$_COOKIE['ilClientId']
Definition: BPMN2Parser.php:15
__construct(ilECSSetting $setting)
Singleton constructor.
const CONTEXT_WEB
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getType()
Get context type.
$client_id