ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
239  default:
240  $this->log->write(__METHOD__.': Unknown event type in queue '.$event['type']);
241  $event_ignored = true;
242  break;
243  }
244 
245  if($event_ignored)
246  {
247  $this->event_reader->delete($event['event_id']);
248  continue;
249  }
250 
251  $res = false;
252  switch($event['op'])
253  {
255  // DEPRECATED?
256  // $this->handleNewlyCreate($event['id']);
257  // $this->log->write(__METHOD__.': Handling new creation. DONE');
258  break;
259 
261  $res = $handler->handleDelete($this->getServer(), $event['id'],$this->mids);
262  $this->log->write(__METHOD__.': Handling delete. DONE');
263  break;
264 
265  case ilECSEvent::CREATED:
266  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
267  $this->log->write(__METHOD__.': Handling create. DONE');
268  break;
269 
270  case ilECSEvent::UPDATED:
271  $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
272  $this->log->write(__METHOD__.': Handling update. DONE');
273  break;
274 
275  default:
276  $this->log->write(__METHOD__.': Unknown event operation in queue '.$event['op']);
277  break;
278  }
279  if($res)
280  {
281  $this->log->write(__METHOD__.': Processing of event done '.$event['event_id']);
282  $this->event_reader->delete($event['event_id']);
283  }
284  else
285  {
286  $this->log->write(__METHOD__.': Processing of event failed '.$event['event_id']);
287  }
288  }
289  }
290 
297  private function handleDeprecatedAccounts()
298  {
299  global $ilDB;
300 
301  $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' ".
302  "AND time_limit_until < ".time()." ".
303  "AND time_limit_unlimited = 0 ".
304  "AND (time_limit_until - time_limit_from) < 7200";
305  $res = $ilDB->query($query);
306  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
307  {
308  if($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id,false))
309  {
310  $this->log->write(__METHOD__.': Deleting deprecated ECS user account '.$user_obj->getLogin());
311  $user_obj->delete();
312  }
313  // only one user
314  break;
315  }
316  return true;
317  }
318 
325  private function readMIDs()
326  {
327  try
328  {
329  $this->mids = array();
330 
331  include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
333  foreach($reader->getCommunities() as $com)
334  {
335  foreach($com->getParticipants() as $part)
336  {
337  if($part->isSelf())
338  {
339  $this->mids[] = $part->getMID();
340  #$this->log->write('Fetch MID: '.$part->getMID());
341  }
342  }
343  }
344  }
345  catch(ilException $exc)
346  {
347  throw $exc;
348  }
349  }
350 
351 
358  public function checkNextExecution()
359  {
360  global $ilLog, $ilDB;
361 
362 
363  if(!$this->settings->isEnabled())
364  {
365  return false;
366  }
367 
368  if(!$this->settings->checkImportId())
369  {
370  $this->log->write(__METHOD__.': Import ID is deleted or not of type "category". Aborting');
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  {
388  // Nothing to do
389  return false;
390  }
391  return true;
392  }
393 
394 
398  protected function initNextExecution()
399  {
400  global $ilLog;
401 
402  // Start task execution as backend process
403  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
404 
405  $soap_client = new ilSoapClient();
406  $soap_client->setResponseTimeout(1);
407  $soap_client->enableWSDL(true);
408 
409  #$ilLog->write(__METHOD__.': Trying to call Soap client...');
410  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
411  $client_id = $_COOKIE['ilClientId'];
412 
413  if($soap_client->init() and 0)
414  {
415  $ilLog->write(__METHOD__.': Calling soap handleECSTasks method...');
416  $res = $soap_client->call('handleECSTasks',array($new_session_id.'::'.$client_id,$this->settings->getServerId()));
417  }
418  else
419  {
420  $ilLog->write(__METHOD__.': SOAP call failed. Calling clone method manually. ');
421  include_once('./webservice/soap/include/inc.soap_functions.php');
422  $res = ilSoapFunctions::handleECSTasks($new_session_id.'::'.$client_id,$this->settings->getServerId());
423  }
424  }
425 }
426 ?>