44 if (!isset(self::$instance)) {
48 return self::$instance;
53 $this->valueHashToValueContainerId = new \SplObjectStorage();
54 $this->bucketHashToObserverContainerId = new \SplObjectStorage();
55 $this->taskHashToTaskContainerId = new \SplObjectStorage();
58 protected function gc(): void
60 $atom = $this->db->buildAtomQuery();
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');
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'],
74 time() - 1 * 60 * 24 * 30
79 $this->db->manipulateF(
80 "DELETE FROM il_bt_bucket WHERE state = %s AND last_heartbeat < %s AND last_heartbeat > 0",
81 [
'integer',
'integer'],
86 $this->db->manipulateF(
87 "DELETE FROM il_bt_bucket WHERE state != %s AND last_heartbeat < %s AND last_heartbeat > 0",
88 [
'integer',
'integer'],
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;" 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;" 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;" 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;" 117 $this->connector =
$c;
139 $bucketContainer->setUserId($bucket->
getUserId());
140 $bucketContainer->setState($bucket->
getState());
141 $bucketContainer->setTotalNumberoftasks(count($bucket->
getTask()->unfoldTask()));
143 $bucketContainer->setTitle($bucket->
getTitle());
150 $bucketContainer->update();
159 $random = new \Random\Randomizer();
161 if($random->getInt(1, 100) === 1) {
166 ->orderBy($order_by, $order_direction)
167 ->getArray(
null,
'id');
180 $bucketMeta->setUserId($bucketContainer->
getUserId());
181 $bucketMeta->setState($bucketContainer->
getState());
182 $bucketMeta->setTitle($bucketContainer->
getTitle());
184 $bucketMeta->setOverallPercentage($bucketContainer->
getPercentage());
207 if ($this->bucketHashToObserverContainerId->contains($bucket)) {
208 $bucketContainer =
new BucketContainer($this->bucketHashToObserverContainerId[$bucket], $this->connector);
214 $bucketContainer->setUserId($bucket->
getUserId());
215 $bucketContainer->setState($bucket->
getState());
216 $bucketContainer->setTitle($bucket->
getTitle());
218 $bucketContainer->setTotalNumberoftasks(count($bucket->
getTask()->unfoldTask()));
222 if (!$bucketContainer->getId()) {
223 $bucketContainer->create();
235 $bucketContainer->save();
236 $this->bucketHashToObserverContainerId[$bucket] = $bucketContainer->getId();
248 if ($this->taskHashToTaskContainerId->contains($task)) {
249 $taskContainer =
new TaskContainer($this->taskHashToTaskContainerId[$task]);
255 $taskContainer->setType($task->
getType());
256 $taskContainer->setBucketId($bucketId);
257 $reflection = new \ReflectionClass($task::class);
258 $taskContainer->setClassName($task::class);
261 foreach ($task->
getInput() as $k => $input) {
264 $this->saveValueToTask($task, $taskContainer, $bucketId);
267 $taskContainer->save();
268 $this->taskHashToTaskContainerId[$task] = $taskContainer->getId();
277 protected function saveValueToTask(
Task $task,
TaskContainer $taskContainer,
int $bucketId):
void 280 if ($taskContainer->
getId() !== 0) {
283 foreach ($olds as $old) {
288 $taskContainer->
save();
292 foreach ($task->
getInput() as $k => $inputValue) {
294 $v->setTaskId($taskContainer->
getId());
296 $v->setBucketId($bucketId);
311 if ($this->valueHashToValueContainerId->contains($value)) {
312 $valueContainer =
new ValueContainer($this->valueHashToValueContainerId[$value], $this->connector);
316 $valueContainer->setClassName($value::class);
321 $valueContainer->setType($value->
getType());
323 $valueContainer->setBucketId($bucketId);
324 $valueContainer->setPosition($position);
325 $valueContainer->setHash($value->
getHash());
326 $valueContainer->setSerialized($value->serialize());
335 $valueContainer->save();
336 $this->valueHashToValueContainerId[$value] = $valueContainer->getId();
344 if (!$this->bucketHashToObserverContainerId->contains($bucket)) {
346 . print_r($bucket,
true));
349 return (
int) $this->bucketHashToObserverContainerId[$bucket];
358 if (!$this->taskHashToTaskContainerId->contains($task)) {
360 . print_r($task,
true));
363 return (
int) $this->taskHashToTaskContainerId[$task];
371 if (!$this->valueHashToValueContainerId->contains($value)) {
373 . print_r($value,
true));
376 return (
int) $this->valueHashToValueContainerId[$value];
384 if (isset(self::$buckets[$bucket_container_id])) {
385 return self::$buckets[$bucket_container_id];
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.");
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));
402 $this->bucketHashToObserverContainerId[$bucket] = $bucket_container_id;
418 $factory = $DIC->backgroundTasks()->taskFactory();
420 $taskContainer = TaskContainer::find($taskContainerId);
423 $task = $factory->createTask($taskContainer->
getClassName());
428 ->orderBy(
'position')
432 foreach ($valueToTasks as $valueToTask) {
433 $inputs[] = $this->loadValue($valueToTask->getValueId(), $bucket, $bucketContainer);
441 $this->taskHashToTaskContainerId[$task] = $taskContainerId;
449 $factory = $DIC->backgroundTasks()->injector();
452 $valueContainer = ValueContainer::find($valueContainerId);
456 $value = $factory->createInstance($valueContainer->getClassName());
458 $value->unserialize($valueContainer->getSerialized());
459 if ($valueContainer->getHasParenttask() !== 0) {
460 $value->setParentTask($this->loadTask($valueContainer->getParentTaskid(), $bucket, $bucketContainer));
463 $this->valueHashToValueContainerId[$value] = $valueContainerId;
501 $this->bucketHashToObserverContainerId->detach($bucket);
510 foreach ($bucket_container_ids as $bucket_id) {
saveObserver(Bucket $bucket)
saveValue(Value $value, int $bucketId, int $position)
static instance(\ilDBInterface $db)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
loadBucket(int $bucket_container_id)
SplObjectStorage $bucketHashToObserverContainerId
Interface Observer Contains several chained tasks and infos about them.
SplObjectStorage $taskHashToTaskContainerId
setConnector(\arConnector $c)
__construct(protected \ilDBInterface $db)
getBucketIdsOfUser(int $user_id, string $order_by="id", string $order_direction="ASC")
[] Returns an array of bucket ids for the given user Id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static BasicPersistence $instance
static where($where, $operator=null)
setCurrentTask(Task $task)
getBucketMetaOfUser(int $user_id)
getBucketContainerId(Bucket $bucket)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
loadBuckets(array $bucket_container_ids)
getValueContainerId(Value $value)
updateBucket(Bucket $bucket)
Updates only the bucket! Use this if e.g.
getLastHeartbeat()
When was the last time that something happened on this bucket?
getBucketIdsByState(int $state)
int[] Returns a list of bucket ids for the given Observer State
SplObjectStorage $valueHashToValueContainerId
saveBucketAndItsTasks(Bucket $bucket)
Fully updates or creates an Observer and all its tasks into the database.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
deleteBucket(Bucket $bucket)
Delete the bucket and all its stuff.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
getTaskContainerId(Task $task)
saveTask(Task $task, int $bucketId)
deleteBucketById(int $bucket_id)
Deletes the Observer AND all its tasks and values.