ILIAS  release_8 Revision v8.24
AsyncTaskManager.php
Go to the documentation of this file.
1<?php
2
20
26
28{
29 public const CMD_START_WORKER = 'startBackgroundTaskWorker';
30
35 public function run(Bucket $bucket): void
36 {
37 global $DIC;
38
39 $bucket->setState(State::SCHEDULED);
40 $bucket->setCurrentTask($bucket->getTask());
41 $DIC->backgroundTasks()->persistence()->saveBucketAndItsTasks($bucket);
42
43 $DIC->logger()->root()->info("[BT] Trying to call webserver");
44
45 // Call SOAP-Server
46 $soap_client = new \ilSoapClient();
47 $soap_client->setResponseTimeout(0);
48 $soap_client->enableWSDL(true);
49 $soap_client->init();
50 $session_id = session_id();
51 $client_id = $DIC->http()->wrapper()->cookie()->retrieve(
52 'ilClientId',
53 $DIC->refinery()->byTrying([
54 $DIC->refinery()->kindlyTo()->string(),
55 $DIC->refinery()->always(
56 defined('CLIENT_ID') ? CLIENT_ID : null
57 )
58 ])
59 );
60
61 try {
62 $soap_client->call(self::CMD_START_WORKER, array(
63 $session_id . '::' . $client_id,
64 ));
65 } catch (\Throwable $t) {
66 $DIC->logger()->root()->info("[BT] Calling Webserver failed, fallback to sync version");
67 $sync_manager = new SyncTaskManager($this->persistence);
68 $sync_manager->run($bucket);
69 } finally {
70 $DIC->logger()->root()->info("[BT] Calling webserver successful");
71 }
72 }
73
77 public function runAsync()
78 {
79 global $DIC, $ilIliasIniFile;
80
81 $n_of_tasks = $ilIliasIniFile->readVariable("background_tasks", "number_of_concurrent_tasks");
82 $n_of_tasks = $n_of_tasks ? $n_of_tasks : 5;
83
84 $DIC->logger()->root()->info("[BackgroundTask] Starting background job.");
85 $persistence = $DIC->backgroundTasks()->persistence();
86
87 // TODO search over all clients.
88 $MAX_PARALLEL_JOBS = $n_of_tasks;
89 if (count($persistence->getBucketIdsByState(State::RUNNING)) >= $MAX_PARALLEL_JOBS) {
90 $DIC->logger()->root()->info("[BT] Too many running jobs, worker going down.");
91
92 return;
93 }
94
95 while (true) {
97 if (count($ids) === 0) {
98 break;
99 }
100
101 $bucket = $persistence->loadBucket(array_shift($ids));
102 $observer = new PersistingObserver($bucket, $persistence);
103 $task = $bucket->getTask();
104
105 try {
106 $this->executeTask($task, $observer);
107 $bucket->setState(State::FINISHED);
108 $this->persistence->updateBucket($bucket);
110 $bucket->setState(State::FINISHED);
111 $this->persistence->deleteBucket($bucket);
113 // We're okay!
114 $this->persistence->saveBucketAndItsTasks($bucket);
115 } catch (\Exception $e) {
116 $persistence->deleteBucket($bucket);
117 $DIC->logger()->root()->info("[BT] Exception while async computing: "
118 . $e->getMessage());
119 $DIC->logger()->root()->info("[BT] Stack Trace: "
120 . $e->getTraceAsString());
121 }
122 }
123
124 $DIC->logger()->root()->info("[BT] One worker going down because there's nothing left to do.");
125
126 return true;
127 }
128}
run(Bucket $bucket)
This will add an Observer of the Task and start running the task.
string $client_id
Definition: class.ilias.php:22
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: feed.php:28
$ilIliasIniFile
Definition: imgupload.php:32
deleteBucket(Bucket $bucket)
Delete the bucket and all its stuff.
loadBucket(int $bucket_container_id)
executeTask(Task $task, Observer $observer)
Actually executes a task.
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: Option.php:19