Skip to content

Commit

Permalink
Fix #42183: Async background tasks fail silently
Browse files Browse the repository at this point in the history
Improve logging of async background task

Check enabled soap to prevent orphaned background task entries

Don't check the return value of the SOAP call

This will be false in case of time-consuming background tasks.
We have to keep the "fire and forget".
  • Loading branch information
fneumann authored and chfsx committed Dec 17, 2024
1 parent 43a53f4 commit 79dddb4
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ILIAS\BackgroundTasks\Implementation\Tasks\UserInteraction\UserInteractionRequiredException;
use ILIAS\BackgroundTasks\Implementation\Tasks\UserInteraction\UserInteractionSkippedException;
use ILIAS\BackgroundTasks\Task\UserInteraction;
use ILIAS\Export\ImportStatus\Exception\ilException;

class AsyncTaskManager extends BasicTaskManager
{
Expand All @@ -36,11 +37,19 @@ public function run(Bucket $bucket): void
{
global $DIC;

// check this before saving the bucket state to prevent an orphaned entry with 0%
if (!$DIC->settings()->get('soap_user_administration')) {
$DIC->logger()->bgtk()->warning("SOAP not enabled, fallback to sync version");
$sync_manager = new SyncTaskManager($this->persistence);
$sync_manager->run($bucket);
return;
}

$bucket->setState(State::SCHEDULED);
$bucket->setCurrentTask($bucket->getTask());
$DIC->backgroundTasks()->persistence()->saveBucketAndItsTasks($bucket);

$DIC->logger()->root()->info("[BT] Trying to call webserver");
$DIC->logger()->bgtk()->info("Trying to call webserver");

// Call SOAP-Server
$soap_client = new \ilSoapClient();
Expand All @@ -63,12 +72,13 @@ public function run(Bucket $bucket): void
$session_id . '::' . $client_id,
));
} catch (\Throwable $t) {
$DIC->logger()->root()->info("[BT] Calling Webserver failed, fallback to sync version");
$DIC->logger()->bgtk()->warning($t->getMessage());
$DIC->logger()->bgtk()->warning("Calling webserver failed, fallback to sync version");
$sync_manager = new SyncTaskManager($this->persistence);
$sync_manager->run($bucket);
} finally {
$DIC->logger()->root()->info("[BT] Calling webserver successful");
return;
}
$DIC->logger()->bgtk()->info("Calling webserver successful");
}

/**
Expand All @@ -81,13 +91,13 @@ public function runAsync()
$n_of_tasks = $ilIliasIniFile->readVariable("background_tasks", "number_of_concurrent_tasks");
$n_of_tasks = $n_of_tasks ? $n_of_tasks : 5;

$DIC->logger()->root()->info("[BackgroundTask] Starting background job.");
$DIC->logger()->bgtk()->info("Starting background job.");
$persistence = $DIC->backgroundTasks()->persistence();

// TODO search over all clients.
$MAX_PARALLEL_JOBS = $n_of_tasks;
if (count($persistence->getBucketIdsByState(State::RUNNING)) >= $MAX_PARALLEL_JOBS) {
$DIC->logger()->root()->info("[BT] Too many running jobs, worker going down.");
$DIC->logger()->bgtk()->info("Too many running jobs, worker going down.");

return;
}
Expand All @@ -114,14 +124,14 @@ public function runAsync()
$this->persistence->saveBucketAndItsTasks($bucket);
} catch (\Exception $e) {
$persistence->deleteBucket($bucket);
$DIC->logger()->root()->info("[BT] Exception while async computing: "
$DIC->logger()->bgtk()->info("Exception while async computing: "
. $e->getMessage());
$DIC->logger()->root()->info("[BT] Stack Trace: "
$DIC->logger()->bgtk()->info("Stack Trace: "
. $e->getTraceAsString());
}
}

$DIC->logger()->root()->info("[BT] One worker going down because there's nothing left to do.");
$DIC->logger()->bgtk()->info("One worker going down because there's nothing left to do.");

return true;
}
Expand Down

0 comments on commit 79dddb4

Please sign in to comment.