ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AsyncTaskManager.php
Go to the documentation of this file.
1 <?php
2 
20 
25 
27 {
28  public const CMD_START_WORKER = 'startBackgroundTaskWorker';
29 
34  public function run(Bucket $bucket): void
35  {
36  global $DIC;
37 
38  // check this before saving the bucket state to prevent an orphaned entry with 0%
39  if (!$DIC->settings()->get('soap_user_administration')) {
40  $DIC->logger()->bgtk()->warning("SOAP not enabled, fallback to sync version");
41  $sync_manager = new SyncTaskManager($this->persistence);
42  $sync_manager->run($bucket);
43  return;
44  }
45 
46  $bucket->setState(State::SCHEDULED);
47  $bucket->setCurrentTask($bucket->getTask());
48  $DIC->backgroundTasks()->persistence()->saveBucketAndItsTasks($bucket);
49 
50  $DIC->logger()->bgtk()->info("Trying to call webserver");
51 
52  // Call SOAP-Server
53  $soap_client = new \ilSoapClient();
54  $soap_client->setResponseTimeout(0);
55  $soap_client->enableWSDL(true);
56  $soap_client->init();
57  $session_id = session_id();
58  $client_id = $DIC->http()->wrapper()->cookie()->retrieve(
59  'ilClientId',
60  $DIC->refinery()->byTrying([
61  $DIC->refinery()->kindlyTo()->string(),
62  $DIC->refinery()->always(
63  defined('CLIENT_ID') ? CLIENT_ID : null
64  )
65  ])
66  );
67 
68  try {
69  $soap_client->call(self::CMD_START_WORKER, [
70  $session_id . '::' . $client_id,
71  ]);
72  } catch (\Throwable $t) {
73  $DIC->logger()->bgtk()->warning($t->getMessage());
74  $DIC->logger()->bgtk()->warning("Calling webserver failed, fallback to sync version");
75  $sync_manager = new SyncTaskManager($this->persistence);
76  $sync_manager->run($bucket);
77  return;
78  }
79  $DIC->logger()->bgtk()->info("Calling webserver successful");
80  }
81 
85  public function runAsync(): ?bool
86  {
87  global $DIC, $ilIliasIniFile;
88 
89  $n_of_tasks = $ilIliasIniFile->readVariable("background_tasks", "number_of_concurrent_tasks");
90  $n_of_tasks = $n_of_tasks ?: 5;
91 
92  $DIC->logger()->bgtk()->info("Starting background job.");
93  $persistence = $DIC->backgroundTasks()->persistence();
94 
95  // TODO search over all clients.
96  $MAX_PARALLEL_JOBS = $n_of_tasks;
97  if (count($persistence->getBucketIdsByState(State::RUNNING)) >= $MAX_PARALLEL_JOBS) {
98  $DIC->logger()->bgtk()->info("Too many running jobs, worker going down.");
99 
100  return null;
101  }
102 
103  while (true) {
104  $ids = $persistence->getBucketIdsByState(State::SCHEDULED);
105  if (count($ids) === 0) {
106  break;
107  }
108 
109  $bucket = $persistence->loadBucket(array_shift($ids));
110  $observer = new PersistingObserver($bucket, $persistence);
111  $task = $bucket->getTask();
112 
113  try {
114  $this->executeTask($task, $observer);
115  $bucket->setState(State::FINISHED);
116  $this->persistence->updateBucket($bucket);
118  $bucket->setState(State::FINISHED);
119  $this->persistence->deleteBucket($bucket);
121  // We're okay!
122  $this->persistence->saveBucketAndItsTasks($bucket);
123  } catch (\Exception $e) {
124  $persistence->deleteBucket($bucket);
125  $DIC->logger()->bgtk()->info("Exception while async computing: "
126  . $e->getMessage());
127  $DIC->logger()->bgtk()->info("Stack Trace: "
128  . $e->getTraceAsString());
129  }
130  }
131 
132  $DIC->logger()->bgtk()->info("One worker going down because there's nothing left to do.");
133 
134  return true;
135  }
136 }
executeTask(Task $task, Observer $observer)
Actually executes a task.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ilIliasIniFile
Definition: server.php:35
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:22
string $client_id
Definition: class.ilias.php:36
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
run(Bucket $bucket)
This will add an Observer of the Task and start running the task.