ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
AsyncTaskManager.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
11 {
12  const CMD_START_WORKER = 'startBackgroundTaskWorker';
13 
14 
24  public function run(Bucket $bucket)
25  {
26  global $DIC;
27 
28  $bucket->setState(State::SCHEDULED);
29  $bucket->setCurrentTask($bucket->getTask());
30  $DIC->backgroundTasks()->persistence()->saveBucketAndItsTasks($bucket);
31 
32  $DIC->logger()->root()->info("[BackgroundTasks] Trying to call webserver");
33 
34  // Call SOAP-Server
35  $soap_client = new \ilSoapClient();
36  $soap_client->setResponseTimeout(1);
37  $soap_client->enableWSDL(true);
38  $soap_client->init();
39  $session_id = session_id();
40  $ilClientId = $_COOKIE['ilClientId'];
41  $call = $soap_client->call(self::CMD_START_WORKER, array(
42  $session_id . '::' . $ilClientId,
43  ));
44  $DIC->logger()->root()->info("[BackgroundTasks] After SOAP Call");
45  $DIC->logger()->root()->info(var_export($call, true));
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);
81  } catch (UserInteractionSkippedException $e) {
82  $bucket->setState(State::FINISHED);
83  $this->persistence->deleteBucket($bucket);
85  // We're okay!
86  $this->persistence->saveBucketAndItsTasks($bucket);
87  } catch (\Exception $e) {
88  $persistence->deleteBucket($bucket);
89  $DIC->logger()->root()->info("[BackgroundTasks] Exception while async computing: "
90  . $e->getMessage());
91  $DIC->logger()->root()->info("[BackgroundTasks] Stack Trace: "
92  . $e->getTraceAsString());
93  }
94  }
95 
96  $DIC->logger()->root()->info("[BackgroundTasks] One worker going down because there's nothing left to do.");
97 
98  return true;
99  }
100 }
$_COOKIE['client_id']
Definition: server.php:9
executeTask(Task $task, Observer $observer)
Actually executes a task.
global $DIC
Definition: saml.php:7
$ilIliasIniFile
run(Bucket $bucket)
This will add an Observer of the Task and start running the task.