ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
AsyncTaskManager.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
10 {
11  const CMD_START_WORKER = 'startBackgroundTaskWorker';
12 
13 
23  public function run(Bucket $bucket)
24  {
25  global $DIC;
26 
27  $bucket->setState(State::SCHEDULED);
28  $bucket->setCurrentTask($bucket->getTask());
29  $DIC->backgroundTasks()->persistence()->saveBucketAndItsTasks($bucket);
30 
31  $DIC->logger()->root()->info("[BackgroundTasks] Trying to call webserver");
32 
33  // Call SOAP-Server
34  $soap_client = new \ilSoapClient();
35  $soap_client->setResponseTimeout(0);
36  $soap_client->enableWSDL(true);
37  $soap_client->init();
38  $session_id = session_id();
39  $ilClientId = $_COOKIE['ilClientId'];
40  $call = $soap_client->call(self::CMD_START_WORKER, array(
41  $session_id . '::' . $ilClientId,
42  ));
43  $DIC->logger()->root()->info(var_export($call, true));
44 
45  $this->runAsync(); // FIX https://mantis.ilias.de/view.php?id=24151
46  }
47 
48 
49  public function runAsync()
50  {
51  global $DIC, $ilIliasIniFile;
52 
53  $n_of_tasks = $ilIliasIniFile->readVariable("background_tasks", "number_of_concurrent_tasks");
54  $n_of_tasks = $n_of_tasks ? $n_of_tasks : 5;
55 
56  $DIC->logger()->root()->info("[BackgroundTask] Starting background job.");
57  $persistence = $DIC->backgroundTasks()->persistence();
58 
59  // TODO search over all clients.
60  $MAX_PARALLEL_JOBS = $n_of_tasks;
61  if (count($persistence->getBucketIdsByState(State::RUNNING)) >= $MAX_PARALLEL_JOBS) {
62  $DIC->logger()->root()->info("[BackgroundTask] Too many running jobs, worker going down.");
63 
64  return;
65  }
66 
67  while (true) {
68  $ids = $persistence->getBucketIdsByState(State::SCHEDULED);
69  if (!count($ids)) {
70  break;
71  }
72 
73  $bucket = $persistence->loadBucket(array_shift($ids));
74  $observer = new PersistingObserver($bucket, $persistence);
75  $task = $bucket->getTask();
76 
77  try {
78  $this->executeTask($task, $observer);
79  $bucket->setState(State::FINISHED);
80  $this->persistence->updateBucket($bucket);
82  // We're okay!
83  $this->persistence->saveBucketAndItsTasks($bucket);
84  } catch (\Exception $e) {
85  $persistence->deleteBucket($bucket);
86  $DIC->logger()->root()->info("[BackgroundTasks] Exception while async computing: "
87  . $e->getMessage());
88  $DIC->logger()->root()->info("[BackgroundTasks] Stack Trace: "
89  . $e->getTraceAsString());
90  }
91  }
92 
93  $DIC->logger()->root()->info("[BackgroundTasks] One worker going down because there's nothing left to do.");
94 
95  return true;
96  }
97 }
$_COOKIE['client_id']
Definition: server.php:9
executeTask(Task $task, Observer $observer)
Actually executes a task.
global $DIC
Definition: saml.php:7
Create styles array
The data for the language used.
$ilIliasIniFile
run(Bucket $bucket)
This will add an Observer of the Task and start running the task.