ILIAS  release_8 Revision v8.24
class.ilECSTaskScheduler.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
24{
25 public const MAX_TASKS = 30;
26
27 private static array $instances = array();
28
29 // Injected
30 private ilLogger $log;
33
34 // Local
37 private array $mids = array();
38
42 private function __construct(ilECSSetting $setting)
43 {
44 global $DIC;
45
46 $this->db = $DIC->database();
47 $this->log = $DIC->logger()->wsrv();
48 $this->eventHandler = $DIC->event();
49
50 $this->settings = $setting;
51 }
52
59 public static function _getInstanceByServerId($a_server_id): \ilECSTaskScheduler
60 {
61 return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] =
64 ));
65 }
66
71 public function getServer(): \ilECSSetting
72 {
73 return $this->settings;
74 }
75
76
80 public function startTaskExecution(): bool
81 {
82 try {
83 $this->readMIDs();
84 $this->readEvents();
85 $this->handleEvents();
86
88 } catch (ilException $exc) {
89 $this->log->warning('Cannot start ecs task execution: ' . $exc->getMessage());
90 return false;
91 }
92 return true;
93 }
94
98 private function readEvents(): void
99 {
100 $this->event_reader = new ilECSEventQueueReader($this->getServer());
101 $this->event_reader->refresh();
102 }
103
107 private function handleEvents(): void
108 {
109 for ($i = 0;$i < self::MAX_TASKS;$i++) {
110 if (!$event = $this->event_reader->shift()) {
111 $this->log->info(__METHOD__ . ': No more pending events found. DONE');
112 break;
113 }
114
115 $this->log->info("Eventdump" . print_r($event, true));
116
117 // determine event handler
118
119 $event_ignored = false;
120 switch ($event['type']) {
129 $handler = ilRemoteObjectBase::getInstanceByEventType($event['type']);
130 if ($handler) {
131 $this->log->debug("got handler " . get_class($handler));
132 } else {
133 $this->log->error("Could not get handler for :" . $event['type']);
134 }
135 break;
136
138 $this->log->debug('Handling new cms tree event.');
139 $handler = new ilECSCmsTreeCommandQueueHandler($this->getServer());
140 break;
141
143 $handler = new ilECSCmsCourseCommandQueueHandler($this->getServer());
144 break;
145
147 $handler = new ilECSCmsCourseMemberCommandQueueHandler($this->getServer());
148 break;
149
151 $this->log->info(__METHOD__ . ': Ignoring event type in queue ' . $event['type']);
152 $event_ignored = true;
153 break;
154
156 $handler = new ilECSEnrolmentStatusCommandQueueHandler($this->getServer());
157 break;
158
159 default:
160
161 $this->log->warning('Unknown type in queue, raising new event handling event: ' . $event['type']);
162 $event_ignored = true;
163
164 $this->eventHandler->raise(
165 'Services/WebServices/ECS',
166 'newEcsEvent',
167 array('event' => $event)
168 );
169 break;
170 }
171
172 if ($event_ignored) {
173 $this->event_reader->deleteEvent($event['event_id']);
174 continue;
175 }
176
177 $res = false;
178 if (isset($handler)) {
179 switch ($event['op']) {
181 // DEPRECATED?
182 // $this->handleNewlyCreate($event['id']);
183 // $this->log->write(__METHOD__.': Handling new creation. DONE');
184 break;
185
187 $res = $handler->handleDelete($this->getServer(), $event['id'], $this->mids);
188 $this->log->info(__METHOD__ . ': Handling delete. DONE');
189 break;
190
192 $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
193 $this->log->info(__METHOD__ . ': Handling create. DONE');
194 break;
195
197 $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
198 $this->log->info(__METHOD__ . ': Handling update. DONE');
199 break;
200
201 default:
202 $this->log->info(__METHOD__ . ': Unknown event operation in queue ' . $event['op']);
203 break;
204 }
205 }
206 if ($res) {
207 $this->log->info(__METHOD__ . ': Processing of event done ' . $event['event_id']);
208 $this->event_reader->deleteEvent($event['event_id']);
209 } else {
210 $this->log->info(__METHOD__ . ': Processing of event failed ' . $event['event_id']);
211 }
212 }
213 }
214
218 private function handleDeprecatedAccounts(): void
219 {
220 $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' " .
221 "AND time_limit_until < " . time() . " " .
222 "AND time_limit_unlimited = 0 " .
223 "AND (time_limit_until - time_limit_from) < 7200";
224 $res = $this->db->query($query);
225 if (($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) &&
226 $user_obj = ilObjectFactory::getInstanceByObjId((int) $row->usr_id, false)) {
227 $this->log->info(__METHOD__ . ': Deleting deprecated ECS user account ' . $user_obj->getLogin());
228 $user_obj->delete();
229 }
230 }
231
235 private function readMIDs(): void
236 {
237 $this->mids = array();
238
239 $reader = ilECSCommunityReader::getInstanceByServerId($this->getServer()->getServerId());
240 foreach ($reader->getCommunities() as $com) {
241 foreach ($com->getParticipants() as $part) {
242 if ($part->isSelf()) {
243 $this->mids[] = $part->getMID();
244 }
245 }
246 }
247 }
248}
Global event handler.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
Reads ECS events and stores them in the database.
static getInstanceByServerId(int $a_server_id)
Get singleton instance per server.
getServer()
Get server setting.
__construct(ilECSSetting $setting)
Singleton constructor.
ilAppEventHandler $eventHandler
static _getInstanceByServerId($a_server_id)
get singleton instance Private access use ilECSTaskScheduler::start() or ilECSTaskScheduler::startTas...
ilECSEventQueueReader $event_reader
handleDeprecatedAccounts()
Delete deprecate ECS accounts.
readMIDs()
Read MID's of this installation.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Component logger with individual log levels by component id.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static getInstanceByEventType(string $a_type)
Get instance by ilECSEvent(QueueReader) type.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$i
Definition: metadata.php:41
$query