ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BasicPersistence.php
Go to the documentation of this file.
1<?php
2
20
31
33{
34 protected static BasicPersistence $instance;
35 protected static array $buckets = [];
36 protected \SplObjectStorage $bucketHashToObserverContainerId;
37 protected \SplObjectStorage $taskHashToTaskContainerId;
38 protected \SplObjectStorage $valueHashToValueContainerId;
39 protected ?\arConnector $connector = null;
40 protected static array $tasks = [];
41
43 {
44 if (!isset(self::$instance)) {
45 self::$instance = new BasicPersistence($db);
46 }
47
48 return self::$instance;
49 }
50
51 public function __construct(protected \ilDBInterface $db)
52 {
53 $this->valueHashToValueContainerId = new \SplObjectStorage();
54 $this->bucketHashToObserverContainerId = new \SplObjectStorage();
55 $this->taskHashToTaskContainerId = new \SplObjectStorage();
56 }
57
58 protected function gc(): void
59 {
60 $atom = $this->db->buildAtomQuery();
61
62 $atom->addTableLock('il_bt_bucket');
63 $atom->addTableLock('il_bt_task');
64 $atom->addTableLock('il_bt_value');
65 $atom->addTableLock('il_bt_value_to_task');
66 $atom->addQueryCallable(function (\ilDBInterface $db): void {
67 $this->db->manipulateF(
68 "DELETE FROM il_bt_bucket WHERE user_id = %s AND (state = %s OR state = %s) AND last_heartbeat < %s AND last_heartbeat > 0",
69 ['integer', 'integer', 'integer', 'integer'],
70 [
71 defined('ANONYMOUS_USER_ID') ? \ANONYMOUS_USER_ID : 13,
74 time() - 1 * 60 * 24 * 30
75 ]
76 );
77
78 // remove old finished buckets
79 $this->db->manipulateF(
80 "DELETE FROM il_bt_bucket WHERE state = %s AND last_heartbeat < %s AND last_heartbeat > 0",
81 ['integer', 'integer'],
82 [State::FINISHED, time() - 60 * 60 * 24 * 30] // older than 30 days
83 );
84
85 // remove old buckets with other states
86 $this->db->manipulateF(
87 "DELETE FROM il_bt_bucket WHERE state != %s AND last_heartbeat < %s AND last_heartbeat > 0",
88 ['integer', 'integer'],
89 [State::FINISHED, time() - 60 * 60 * 24 * 180] // older than 180 days
90 );
91
92 // remove tasks without a bucket
93 $this->db->manipulate(
94 "DELETE il_bt_task FROM il_bt_task LEFT JOIN il_bt_bucket ON il_bt_bucket.id = il_bt_task.bucket_id WHERE il_bt_bucket.id IS NULL;"
95 );
96
97 // remove value to bucket links without a bucket
98 $this->db->manipulate(
99 "DELETE il_bt_value_to_task FROM il_bt_value_to_task LEFT JOIN il_bt_bucket ON il_bt_bucket.id = il_bt_value_to_task.bucket_id WHERE il_bt_bucket.id IS NULL;"
100 );
101
102 // remove value to bucket links without a task
103 $this->db->manipulate(
104 "DELETE il_bt_value_to_task FROM il_bt_value_to_task LEFT JOIN il_bt_task ON il_bt_task.id = il_bt_value_to_task.task_id WHERE il_bt_task.id IS NULL;"
105 );
106
107 // remove values without a task
108 $this->db->manipulate(
109 "DELETE il_bt_value FROM il_bt_value LEFT JOIN il_bt_value_to_task ON il_bt_value_to_task.value_id = il_bt_value.id WHERE il_bt_value_to_task.id IS NULL;"
110 );
111 });
112 $atom->run();
113 }
114
115 public function setConnector(\arConnector $c): void
116 {
117 $this->connector = $c;
118 }
119
124 public function saveBucketAndItsTasks(Bucket $bucket): void
125 {
126 $bucket->checkIntegrity();
127
128 $this->saveObserver($bucket);
129 }
130
134 public function updateBucket(Bucket $bucket): void
135 {
136 $bucketContainer = new BucketContainer($this->getBucketContainerId($bucket), $this->connector);
137
138 // The basic information about the task.
139 $bucketContainer->setUserId($bucket->getUserId());
140 $bucketContainer->setState($bucket->getState());
141 $bucketContainer->setTotalNumberoftasks(count($bucket->getTask()->unfoldTask()));
142 $bucketContainer->setPercentage($bucket->getOverallPercentage());
143 $bucketContainer->setTitle($bucket->getTitle());
144 $bucketContainer->setLastHeartbeat($bucket->getLastHeartbeat());
145 $bucketContainer->setDescription($bucket->getDescription());
146 $bucketContainer->setCurrentTaskid($this->getTaskContainerId($bucket->getCurrentTask()));
147 $bucketContainer->setRootTaskid($this->getTaskContainerId($bucket->getTask()));
148
149 // Save and store the container to bucket instance.
150 $bucketContainer->update();
151 }
152
156 public function getBucketIdsOfUser(int $user_id, string $order_by = "id", string $order_direction = "ASC"): array
157 {
158 // Garbage Collection
159 $random = new \Random\Randomizer();
160
161 if ($random->getInt(1, 100) === 1) {
162 $this->gc();
163 }
164
165 return BucketContainer::where(['user_id' => $user_id])
166 ->orderBy($order_by, $order_direction)
167 ->getArray(null, 'id');
168 }
169
173 public function getBucketMetaOfUser(int $user_id): array
174 {
175 $buckets = BucketContainer::where(['user_id' => $user_id])->get();
176
177 return array_map(function (BucketContainer $bucketContainer): BasicBucketMeta {
178 $bucketMeta = new BasicBucketMeta();
179
180 $bucketMeta->setUserId($bucketContainer->getUserId());
181 $bucketMeta->setState($bucketContainer->getState());
182 $bucketMeta->setTitle($bucketContainer->getTitle());
183 $bucketMeta->setDescription($bucketContainer->getDescription());
184 $bucketMeta->setOverallPercentage($bucketContainer->getPercentage());
185
186 return $bucketMeta;
187 }, $buckets);
188 }
189
193 public function getBucketIdsByState(int $state): array
194 {
195 $buckets = BucketContainer::where(['state' => $state])->get();
196
197 return array_map(fn(BucketContainer $bucket_container): int => $bucket_container->getId(), $buckets);
198 }
199
204 protected function saveObserver(Bucket $bucket): void
205 {
206 // If the instance has a known container we use it, otherwise we create a new container.
207 if ($this->bucketHashToObserverContainerId->contains($bucket)) {
208 $bucketContainer = new BucketContainer($this->bucketHashToObserverContainerId[$bucket], $this->connector);
209 } else {
210 $bucketContainer = new BucketContainer(0, $this->connector);
211 }
212
213 // The basic information about the task.
214 $bucketContainer->setUserId($bucket->getUserId());
215 $bucketContainer->setState($bucket->getState());
216 $bucketContainer->setTitle($bucket->getTitle());
217 $bucketContainer->setDescription($bucket->getDescription());
218 $bucketContainer->setTotalNumberoftasks(count($bucket->getTask()->unfoldTask()));
219 $bucketContainer->setPercentage($bucket->getOverallPercentage());
220
221 // We want to store the bucket ID in every sub task and value. Thus we need to create an id if not available yet.
222 if (!$bucketContainer->getId()) {
223 $bucketContainer->create();
224 }
225
226 // The recursive part.
227 $this->saveTask($bucket->getTask(), $bucketContainer->getId());
228 if (!$bucket->hasCurrentTask()) {
229 $bucket->setCurrentTask($bucket->getTask());
230 }
231 $bucketContainer->setCurrentTaskid($this->getTaskContainerId($bucket->getCurrentTask()));
232 $bucketContainer->setRootTaskid($this->getTaskContainerId($bucket->getTask()));
233
234 // Save and store the container to bucket instance.
235 $bucketContainer->save();
236 $this->bucketHashToObserverContainerId[$bucket] = $bucketContainer->getId();
237 }
238
245 protected function saveTask(Task $task, int $bucketId): void
246 {
247 // If the instance has a known container we use it, otherwise we create a new container.
248 if ($this->taskHashToTaskContainerId->contains($task)) {
249 $taskContainer = new TaskContainer($this->taskHashToTaskContainerId[$task]);
250 } else {
251 $taskContainer = new TaskContainer(0);
252 }
253
254 // The basic information about the task.
255 $taskContainer->setType($task->getType());
256 $taskContainer->setBucketId($bucketId);
257 $reflection = new \ReflectionClass($task::class);
258 $taskContainer->setClassName($task::class);
259
260 // Recursivly save the inputs and link them to this task.
261 foreach ($task->getInput() as $k => $input) {
262 $this->saveValue($input, $bucketId, $k);
263 }
264 $this->saveValueToTask($task, $taskContainer, $bucketId);
265
266 // Save and store the container to the task instance.
267 $taskContainer->save();
268 $this->taskHashToTaskContainerId[$task] = $taskContainer->getId();
269 }
270
277 protected function saveValueToTask(Task $task, TaskContainer $taskContainer, int $bucketId): void
278 {
279 // If we have previous values to task associations we delete them.
280 if ($taskContainer->getId() !== 0) {
282 $olds = ValueToTaskContainer::where(['task_id' => $taskContainer->getId()])->get();
283 foreach ($olds as $old) {
284 $old->delete();
285 }
286 } else {
287 // We need a valid ID to link the inputs
288 $taskContainer->save();
289 }
290
291 // We create the new 1 to n relation.
292 foreach ($task->getInput() as $k => $inputValue) {
293 $v = new ValueToTaskContainer(0, $this->connector);
294 $v->setTaskId($taskContainer->getId());
295 $v->setPosition($k);
296 $v->setBucketId($bucketId);
297 $v->setValueId($this->getValueContainerId($inputValue));
298 $v->save();
299 }
300 }
301
308 protected function saveValue(Value $value, int $bucketId, int $position): void
309 {
310 // If we have previous values to task associations we delete them.
311 if ($this->valueHashToValueContainerId->contains($value)) {
312 $valueContainer = new ValueContainer($this->valueHashToValueContainerId[$value], $this->connector);
313 } else {
314 $valueContainer = new ValueContainer(0, $this->connector);
315 }
316 $valueContainer->setClassName($value::class);
317 // bugfix mantis 23503
318 // $absolute_class_path = $reflection->getFileName();
319 // $relative_class_path = str_replace(ILIAS_ABSOLUTE_PATH,".",$absolute_class_path);
320 // $valueContainer->setClassPath($relative_class_path);
321 $valueContainer->setType($value->getType());
322 $valueContainer->setHasParenttask($value->hasParentTask());
323 $valueContainer->setBucketId($bucketId);
324 $valueContainer->setPosition($position);
325 $valueContainer->setHash($value->getHash());
326 $valueContainer->setSerialized($value->serialize());
327
328 // If the value is a thunk value we also store its parent.
329 if ($value->hasParentTask()) {
330 $this->saveTask($value->getParentTask(), $bucketId);
331 $valueContainer->setParentTaskid($this->getTaskContainerId($value->getParentTask()));
332 }
333
334 // We save the container and store the instance to container association.
335 $valueContainer->save();
336 $this->valueHashToValueContainerId[$value] = $valueContainer->getId();
337 }
338
342 public function getBucketContainerId(Bucket $bucket): int
343 {
344 if (!$this->bucketHashToObserverContainerId->contains($bucket)) {
345 throw new SerializationException("Could not resolve container id of task: "
346 . print_r($bucket, true));
347 }
348
349 return (int) $this->bucketHashToObserverContainerId[$bucket];
350 }
351
356 protected function getTaskContainerId(Task $task): int
357 {
358 if (!$this->taskHashToTaskContainerId->contains($task)) {
359 throw new SerializationException("Could not resolve container id of task: "
360 . print_r($task, true));
361 }
362
363 return (int) $this->taskHashToTaskContainerId[$task];
364 }
365
369 protected function getValueContainerId(Value $value): int
370 {
371 if (!$this->valueHashToValueContainerId->contains($value)) {
372 throw new SerializationException("Could not resolve container id of value: "
373 . print_r($value, true));
374 }
375
376 return (int) $this->valueHashToValueContainerId[$value];
377 }
378
382 public function loadBucket(int $bucket_container_id): Bucket
383 {
384 if (isset(self::$buckets[$bucket_container_id])) {
385 return self::$buckets[$bucket_container_id];
386 }
388 $bucketContainer = BucketContainer::find($bucket_container_id);
389 if (!$bucketContainer) {
390 throw new BucketNotFoundException("The requested bucket with container id $bucket_container_id could not be found in the database.");
391 }
392 $bucket = new BasicBucket();
393
394 $bucket->setUserId($bucketContainer->getUserId());
395 $bucket->setState($bucketContainer->getState());
396 $bucket->setTitle($bucketContainer->getTitle());
397 $bucket->setDescription($bucketContainer->getDescription());
398 $bucket->setOverallPercentage($bucketContainer->getPercentage());
399 $bucket->setLastHeartbeat($bucketContainer->getLastHeartbeat());
400 $bucket->setTask($this->loadTask($bucketContainer->getRootTaskid(), $bucket, $bucketContainer));
401
402 $this->bucketHashToObserverContainerId[$bucket] = $bucket_container_id;
403
404 return $bucket;
405 }
406
415 private function loadTask(int $taskContainerId, Bucket $bucket, BucketContainer $bucketContainer): Task
416 {
417 global $DIC;
418 $factory = $DIC->backgroundTasks()->taskFactory();
420 $taskContainer = TaskContainer::find($taskContainerId);
423 $task = $factory->createTask($taskContainer->getClassName());
424
425 // Added additional orderBy for the id to ensure that the items are returned in the right order.
426 $valueToTasks = ValueToTaskContainer::where(['task_id' => $taskContainerId])
427 ->orderBy('task_id')
428 ->orderBy('position')
429 ->orderBy('id')
430 ->get();
431 $inputs = [];
432 foreach ($valueToTasks as $valueToTask) {
433 $inputs[] = $this->loadValue($valueToTask->getValueId(), $bucket, $bucketContainer);
434 }
435 $task->setInput($inputs);
436
437 if ($taskContainerId === $bucketContainer->getCurrentTaskid()) {
438 $bucket->setCurrentTask($task);
439 }
440
441 $this->taskHashToTaskContainerId[$task] = $taskContainerId;
442
443 return $task;
444 }
445
446 private function loadValue($valueContainerId, Bucket $bucket, BucketContainer $bucketContainer): Value
447 {
448 global $DIC;
449 $factory = $DIC->backgroundTasks()->injector();
450
452 $valueContainer = ValueContainer::find($valueContainerId);
456 $value = $factory->createInstance($valueContainer->getClassName());
457
458 $value->unserialize($valueContainer->getSerialized());
459 if ($valueContainer->getHasParenttask() !== 0) {
460 $value->setParentTask($this->loadTask($valueContainer->getParentTaskid(), $bucket, $bucketContainer));
461 }
462
463 $this->valueHashToValueContainerId[$value] = $valueContainerId;
464
465 return $value;
466 }
467
468 public function deleteBucketById(int $bucket_id): void
469 {
470 $buckets = BucketContainer::where(['id' => $bucket_id])->get();
471 array_map(function (BucketContainer $item): void {
472 $item->delete();
473 }, $buckets);
474
476 $tasks = TaskContainer::where(['bucket_id' => $bucket_id])->get();
477 array_map(function (TaskContainer $item): void {
478 $item->delete();
479 }, $tasks);
480
482 $values = ValueContainer::where(['bucket_id' => $bucket_id])->get();
483 array_map(function (ValueContainer $item): void {
484 $item->delete();
485 }, $values);
486
488 $valueToTasks = ValueToTaskContainer::where(['bucket_id' => $bucket_id])->get();
489 array_map(function (ValueToTaskContainer $item): void {
490 $item->delete();
491 }, $valueToTasks);
492 }
493
497 public function deleteBucket(Bucket $bucket): void
498 {
499 $id = $this->getBucketContainerId($bucket);
500 $this->deleteBucketById($id);
501 $this->bucketHashToObserverContainerId->detach($bucket);
502 }
503
507 public function loadBuckets(array $bucket_container_ids): array
508 {
509 $buckets = [];
510 foreach ($bucket_container_ids as $bucket_id) {
511 try {
512 $buckets[] = $this->loadBucket($bucket_id);
513 } catch (\Throwable) {
514 // there seem to be a problem with this container, we must delete it
515 $this->deleteBucketById($bucket_id);
516 }
517 }
518
519 return $buckets;
520 }
521}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static where($where, $operator=null)
saveBucketAndItsTasks(Bucket $bucket)
Fully updates or creates an Observer and all its tasks into the database.
getBucketIdsOfUser(int $user_id, string $order_by="id", string $order_direction="ASC")
\int[] Returns an array of bucket ids for the given user Id.
updateBucket(Bucket $bucket)
Updates only the bucket! Use this if e.g.
deleteBucket(Bucket $bucket)
Delete the bucket and all its stuff.
getBucketIdsByState(int $state)
int[] Returns a list of bucket ids for the given Observer State
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ANONYMOUS_USER_ID
Definition: constants.php:27
$c
Definition: deliver.php:25
getLastHeartbeat()
When was the last time that something happened on this bucket?
loadBucket(int $bucket_container_id)
deleteBucketById(int $bucket_id)
Deletes the Observer AND all its tasks and values.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Job.php:19
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26