ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilECSTaskScheduler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 public const MAX_TASKS = 30;
27
28 private static array $instances = array();
29
30 // Injected
31 private ilLogger $log;
34
35 // Local
38 private array $mids = array();
39
43 private function __construct(ilECSSetting $setting)
44 {
45 global $DIC;
46
47 $this->db = $DIC->database();
48 $this->log = $DIC->logger()->wsrv();
49 $this->eventHandler = $DIC->event();
50
51 $this->settings = $setting;
52 }
53
60 public static function _getInstanceByServerId($a_server_id): \ilECSTaskScheduler
61 {
62 return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] =
65 ));
66 }
67
72 public function getServer(): \ilECSSetting
73 {
74 return $this->settings;
75 }
76
77
81 public function startTaskExecution(): bool
82 {
83 try {
84 $this->readMIDs();
85 $this->readEvents();
86 $this->handleEvents();
87
89 } catch (ilException $exc) {
90 $this->log->warning('Cannot start ecs task execution: ' . $exc->getMessage());
91 return false;
92 }
93 return true;
94 }
95
99 private function readEvents(): void
100 {
101 $this->event_reader = new ilECSEventQueueReader($this->getServer());
102 $this->event_reader->refresh();
103 }
104
108 private function handleEvents(): void
109 {
110 for ($i = 0;$i < self::MAX_TASKS;$i++) {
111 if (!$event = $this->event_reader->shift()) {
112 $this->log->info(__METHOD__ . ': No more pending events found. DONE');
113 break;
114 }
115
116 $this->log->info("Eventdump" . print_r($event, true));
117
118 // determine event handler
119
120 $event_ignored = false;
121 switch ($event['type']) {
131 if ($handler) {
132 $this->log->debug("got handler " . get_class($handler));
133 } else {
134 $this->log->error("Could not get handler for :" . $event['type']);
135 }
136 break;
137
139 $this->log->debug('Handling new cms tree event.');
141 break;
142
145 break;
146
149 break;
150
152 $this->log->info(__METHOD__ . ': Ignoring event type in queue ' . $event['type']);
153 $event_ignored = true;
154 break;
155
158 break;
159
160 default:
161
162 $this->log->warning('Unknown type in queue, raising new event handling event: ' . $event['type']);
163 $event_ignored = true;
164
165 $this->eventHandler->raise(
166 'components/ILIAS/WebServices/ECS',
167 'newEcsEvent',
168 array('event' => $event)
169 );
170 break;
171 }
172
173 if ($event_ignored) {
174 $this->event_reader->deleteEvent($event['event_id']);
175 continue;
176 }
177
178 $res = false;
179 if (isset($handler)) {
180 switch ($event['op']) {
182 // DEPRECATED?
183 // $this->handleNewlyCreate($event['id']);
184 // $this->log->write(__METHOD__.': Handling new creation. DONE');
185 break;
186
188 $res = $handler->handleDelete($this->getServer(), $event['id'], $this->mids);
189 $this->log->info(__METHOD__ . ': Handling delete. DONE');
190 break;
191
193 $res = $handler->handleCreate($this->getServer(), $event['id'], $this->mids);
194 $this->log->info(__METHOD__ . ': Handling create. DONE');
195 break;
196
198 $res = $handler->handleUpdate($this->getServer(), $event['id'], $this->mids);
199 $this->log->info(__METHOD__ . ': Handling update. DONE');
200 break;
201
202 default:
203 $this->log->info(__METHOD__ . ': Unknown event operation in queue ' . $event['op']);
204 break;
205 }
206 }
207 if ($res) {
208 $this->log->info(__METHOD__ . ': Processing of event done ' . $event['event_id']);
209 $this->event_reader->deleteEvent($event['event_id']);
210 } else {
211 $this->log->info(__METHOD__ . ': Processing of event failed ' . $event['event_id']);
212 }
213 }
214 }
215
219 private function handleDeprecatedAccounts(): void
220 {
221 $query = "SELECT usr_id FROM usr_data WHERE auth_mode = 'ecs' " .
222 "AND time_limit_until < " . time() . " " .
223 "AND time_limit_unlimited = 0 " .
224 "AND (time_limit_until - time_limit_from) < 7200";
225 $res = $this->db->query($query);
226 if (($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) &&
227 $user_obj = ilObjectFactory::getInstanceByObjId((int) $row->usr_id, false)) {
228 $this->log->info(__METHOD__ . ': Deleting deprecated ECS user account ' . $user_obj->getLogin());
229 $user_obj->delete();
230 }
231 }
232
236 private function readMIDs(): void
237 {
238 $this->mids = array();
239
240 $reader = ilECSCommunityReader::getInstanceByServerId($this->getServer()->getServerId());
241 foreach ($reader->getCommunities() as $com) {
242 foreach ($com->getParticipants() as $part) {
243 if ($part->isSelf()) {
244 $this->mids[] = $part->getMID();
245 }
246 }
247 }
248 }
249}
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.
Base class for ILIAS Exception handling.
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.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$handler
Definition: oai.php:29
global $DIC
Definition: shib_login.php:26