ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  return self::$instances[$a_server_id];
84  }
85  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
86  return self::$instances[$a_server_id] =
89  );
90  }
91 
95  public static function start()
96  {
97  include_once './Services/Context/classes/class.ilContext.php';
99  return;
100  }
101 
102  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
104  foreach ($servers->getServers() as $server) {
105  $sched = new ilECSTaskScheduler($server);
106  if ($sched->checkNextExecution()) {
107  $sched->initNextExecution();
108  }
109  }
110  }
111 
115  public static function startExecution()
116  {
117  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
119  foreach ($server->getServers() as $server) {
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  $this->readMIDs();
147  $this->readEvents();
148  $this->handleEvents();
149 
150  $this->handleDeprecatedAccounts();
151  } catch (ilException $exc) {
152  $this->log->warning('Cannot start ecs task execution: ' . $exc->getMessage());
153  return false;
154  }
155  return true;
156  }
157 
164  private function readEvents()
165  {
166  try {
167  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
168  $this->event_reader = new ilECSEventQueueReader($this->getServer()->getServerId());
169  $this->event_reader->refresh();
170  } catch (ilException $exc) {
171  throw $exc;
172  }
173  }
174 
181  private function handleEvents()
182  {
183  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
184 
185  for ($i = 0;$i < self::MAX_TASKS;$i++) {
186  if (!$event = $this->event_reader->shift()) {
187  $this->log->write(__METHOD__ . ': No more pending events found. DONE');
188  break;
189  }
190 
191  $this->log->write(print_r($event, true));
192 
193  // determine event handler
194 
195  $event_ignored = false;
196  switch ($event['type']) {
205  include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
207  $this->log->write("got handler " . get_class($handler));
208  break;
209 
211  $this->log->debug('Handling new cms tree event.');
212  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTreeCommandQueueHandler.php';
214  break;
215 
217  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseCommandQueueHandler.php';
219  break;
220 
222  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCmsCourseMemberCommandQueueHandler.php';
224  break;
225 
227  $this->log->write(__METHOD__ . ': Ignoring event type in queue ' . $event['type']);
228  $event_ignored = true;
229  break;
230 
232  include_once './Services/WebServices/ECS/classes/Connectors/class.ilECSEnrolmentStatusCommandQueueHandler.php';
234  break;
235 
236  default:
237 
238  $this->log->warning('Unknown type in queue, raising new event handling event: ' . $event['type']);
239  $event_ignored = true;
240 
241  $GLOBALS['ilAppEventHandler']->raise(
242  'Services/WebServices/ECS',
243  'newEcsEvent',
244  array('event' => $event)
245  );
246  break;
247  }
248 
249  if ($event_ignored) {
250  $this->event_reader->delete($event['event_id']);
251  continue;
252  }
253 
254  $res = false;
255  switch ($event['op']) {
257  // DEPRECATED?
258  // $this->handleNewlyCreate($event['id']);
259  // $this->log->write(__METHOD__.': Handling new creation. DONE');
260  break;
261 
263  $res = $handler->handleDelete($this->getServer(), $event['id'], $this->mids);
264  $this->log->write(__METHOD__ . ': Handling delete. DONE');
265  break;
266 
267  case ilECSEvent::CREATED:
268  $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
269  $this->log->write(__METHOD__ . ': Handling create. DONE');
270  break;
271 
272  case ilECSEvent::UPDATED:
273  $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
274  $this->log->write(__METHOD__ . ': Handling update. DONE');
275  break;
276 
277  default:
278  $this->log->write(__METHOD__ . ': Unknown event operation in queue ' . $event['op']);
279  break;
280  }
281  if ($res) {
282  $this->log->write(__METHOD__ . ': Processing of event done ' . $event['event_id']);
283  $this->event_reader->delete($event['event_id']);
284  } else {
285  $this->log->write(__METHOD__ . ': Processing of event failed ' . $event['event_id']);
286  }
287  }
288  }
289 
296  private function handleDeprecatedAccounts()
297  {
298  global $ilDB;
299 
300  $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' " .
301  "AND time_limit_until < " . time() . " " .
302  "AND time_limit_unlimited = 0 " .
303  "AND (time_limit_until - time_limit_from) < 7200";
304  $res = $ilDB->query($query);
305  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
306  if ($user_obj = ilObjectFactory::getInstanceByObjId($row->usr_id, false)) {
307  $this->log->write(__METHOD__ . ': Deleting deprecated ECS user account ' . $user_obj->getLogin());
308  $user_obj->delete();
309  }
310  // only one user
311  break;
312  }
313  return true;
314  }
315 
322  private function readMIDs()
323  {
324  try {
325  $this->mids = array();
326 
327  include_once('./Services/WebServices/ECS/classes/class.ilECSCommunityReader.php');
329  foreach ($reader->getCommunities() as $com) {
330  foreach ($com->getParticipants() as $part) {
331  if ($part->isSelf()) {
332  $this->mids[] = $part->getMID();
333  }
334  }
335  }
336  } catch (ilException $exc) {
337  throw $exc;
338  }
339  }
340 
341 
348  public function checkNextExecution()
349  {
350  global $ilDB;
351 
352 
353  if (!$this->settings->isEnabled()) {
354  return false;
355  }
356 
357  if (!$this->settings->checkImportId()) {
358  $this->log->warning('Import ID is deleted or not of type "category". Aborting');
359  return false;
360  }
361 
362  // check next task excecution time:
363  // If it's greater than time() directly increase this value with the polling time
364  /* synchronized { */
365  $query = 'UPDATE settings SET ' .
366  'value = ' . $ilDB->quote(time() + $this->settings->getPollingTime(), 'text') . ' ' .
367  'WHERE module = ' . $ilDB->quote('ecs', 'text') . ' ' .
368  'AND keyword = ' . $ilDB->quote('next_execution_' . $this->settings->getServerId(), 'text') . ' ' .
369  'AND value < ' . $ilDB->quote(time(), 'text');
370  $affected_rows = $ilDB->manipulate($query);
371  /* } */
372 
373 
374  if (!$affected_rows) {
375  // Nothing to do
376  return false;
377  }
378  return true;
379  }
380 
381 
385  protected function initNextExecution()
386  {
387  global $ilLog;
388 
389  // Start task execution as backend process
390  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
391 
392  $soap_client = new ilSoapClient();
393  $soap_client->setResponseTimeout(1);
394  $soap_client->enableWSDL(true);
395 
396  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
397  $client_id = $_COOKIE['ilClientId'];
398 
399  if ($soap_client->init() and 0) {
400  $this->log->info('Calling soap handleECSTasks method...');
401  $res = $soap_client->call('handleECSTasks', array($new_session_id . '::' . $client_id,$this->settings->getServerId()));
402  } else {
403  $this->log->info('SOAP call failed. Calling clone method manually. ');
404  include_once('./webservice/soap/include/inc.soap_functions.php');
405  $res = ilSoapFunctions::handleECSTasks($new_session_id . '::' . $client_id, $this->settings->getServerId());
406  }
407  }
408 }
$_COOKIE['client_id']
Definition: server.php:9
static startExecution()
Static version iterates over all active instances.
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.
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
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
Definition: getUserInfo.php:12
settings()
Definition: settings.php:2
static _duplicate($a_session_id)
Duplicate session.
global $ilDB
$i
Definition: disco.tpl.php:19
__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
$handler