ILIAS  Release_5_0_x_branch Revision 61816
 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 
240  include_once './Services/WebServices/ECS/classes/Connectors/class.ilECSEnrolmentStatusCommandQueueHandler.php';
241  $handler = new ilECSEnrolmentStatusCommandQueueHandler($this->getServer());
242  break;
243 
244  default:
245  $this->log->write(__METHOD__.': Unknown event type in queue '.$event['type']);
246  $event_ignored = true;
247  break;
248  }
249 
250  if($event_ignored)
251  {
252  $this->event_reader->delete($event['event_id']);
253  continue;
254  }
255 
256  $res = false;
257  switch($event['op'])
258  {
260  // DEPRECATED?
261  // $this->handleNewlyCreate($event['id']);
262  // $this->log->write(__METHOD__.': Handling new creation. DONE');
263  break;
264 
266  $res = $handler->handleDelete($this->getServer(), $event['id'],$this->mids);
267  $this->log->write(__METHOD__.': Handling delete. DONE');
268  break;
269 
270  case ilECSEvent::CREATED:
271  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
272  $this->log->write(__METHOD__.': Handling create. DONE');
273  break;
274 
275  case ilECSEvent::UPDATED:
276  $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
277  $this->log->write(__METHOD__.': Handling update. DONE');
278  break;
279 
280  default:
281  $this->log->write(__METHOD__.': Unknown event operation in queue '.$event['op']);
282  break;
283  }
284  if($res)
285  {
286  $this->log->write(__METHOD__.': Processing of event done '.$event['event_id']);
287  $this->event_reader->delete($event['event_id']);
288  }
289  else
290  {
291  $this->log->write(__METHOD__.': Processing of event failed '.$event['event_id']);
292  }
293  }
294  }
295 
302  private function handleDeprecatedAccounts()
303  {
304  global $ilDB;
305 
306  $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' ".
307  "AND time_limit_until < ".time()." ".
308  "AND time_limit_unlimited = 0 ".
309  "AND (time_limit_until - time_limit_from) < 7200";
310  $res = $ilDB->query($query);
311  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
312  {
313  if($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id,false))
314  {
315  $this->log->write(__METHOD__.': Deleting deprecated ECS user account '.$user_obj->getLogin());
316  $user_obj->delete();
317  }
318  // only one user
319  break;
320  }
321  return true;
322  }
323 
330  private function readMIDs()
331  {
332  try
333  {
334  $this->mids = array();
335 
336  include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
338  foreach($reader->getCommunities() as $com)
339  {
340  foreach($com->getParticipants() as $part)
341  {
342  if($part->isSelf())
343  {
344  $this->mids[] = $part->getMID();
345  #$this->log->write('Fetch MID: '.$part->getMID());
346  }
347  }
348  }
349  }
350  catch(ilException $exc)
351  {
352  throw $exc;
353  }
354  }
355 
356 
363  public function checkNextExecution()
364  {
365  global $ilLog, $ilDB;
366 
367 
368  if(!$this->settings->isEnabled())
369  {
370  return false;
371  }
372 
373  if(!$this->settings->checkImportId())
374  {
375  $this->log->write(__METHOD__.': Import ID is deleted or not of type "category". Aborting');
376  return false;
377  }
378 
379  // check next task excecution time:
380  // If it's greater than time() directly increase this value with the polling time
381  /* synchronized { */
382  $query = 'UPDATE settings SET '.
383  'value = '.$ilDB->quote(time() + $this->settings->getPollingTime(),'text').' '.
384  'WHERE module = '.$ilDB->quote('ecs','text').' '.
385  'AND keyword = '.$ilDB->quote('next_execution_'.$this->settings->getServerId(),'text').' '.
386  'AND value < '.$ilDB->quote(time(),'text');
387  $affected_rows = $ilDB->manipulate($query);
388  /* } */
389 
390 
391  if(!$affected_rows)
392  {
393  // Nothing to do
394  return false;
395  }
396  return true;
397  }
398 
399 
403  protected function initNextExecution()
404  {
405  global $ilLog;
406 
407  // Start task execution as backend process
408  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
409 
410  $soap_client = new ilSoapClient();
411  $soap_client->setResponseTimeout(1);
412  $soap_client->enableWSDL(true);
413 
414  #$ilLog->write(__METHOD__.': Trying to call Soap client...');
415  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
416  $client_id = $_COOKIE['ilClientId'];
417 
418  if($soap_client->init() and 0)
419  {
420  $ilLog->write(__METHOD__.': Calling soap handleECSTasks method...');
421  $res = $soap_client->call('handleECSTasks',array($new_session_id.'::'.$client_id,$this->settings->getServerId()));
422  }
423  else
424  {
425  $ilLog->write(__METHOD__.': SOAP call failed. Calling clone method manually. ');
426  include_once('./webservice/soap/include/inc.soap_functions.php');
427  $res = ilSoapFunctions::handleECSTasks($new_session_id.'::'.$client_id,$this->settings->getServerId());
428  }
429  }
430 }
431 ?>