-
Notifications
You must be signed in to change notification settings - Fork 395
[Feature] NamedSetupObjective "Start BackgroundTaskWorker" #10825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fhelfer
wants to merge
3
commits into
ILIAS-eLearning:release_10
Choose a base branch
from
fhelfer:soap/nemdobjective/backgroundtaskworker
base: release_10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
215 changes: 215 additions & 0 deletions
215
components/ILIAS/BackgroundTasks_/classes/Setup/BackgoundWorkerObjective.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of ILIAS, a powerful learning management system | ||
| * published by ILIAS open source e-Learning e.V. | ||
| * | ||
| * ILIAS is licensed with the GPL-3.0, | ||
| * see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| * You should have received a copy of said license along with the | ||
| * source code, too. | ||
| * | ||
| * If this is not the case or you just want to try ILIAS, you'll find | ||
| * us at: | ||
| * https://www.ilias.de | ||
| * https://github.com/ILIAS-eLearning | ||
| * | ||
| *********************************************************************/ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ILIAS\BackgroundTasks\Setup; | ||
|
|
||
| use ilUtil; | ||
| use ilSetting; | ||
| use Throwable; | ||
| use SoapClient; | ||
| use ilProxySettings; | ||
| use ilCurlConnection; | ||
| use RuntimeException; | ||
| use ILIAS\DI\Container; | ||
| use ILIAS\Setup\Objective; | ||
| use ILIAS\Setup\Environment; | ||
| use ilCurlConnectionException; | ||
| use ILIAS\Setup\CLI\IOWrapper; | ||
| use ilIniFilesPopulatedObjective; | ||
| use ilDatabaseInitializedObjective; | ||
| use ilSettingsFactoryExistsObjective; | ||
| use ILIAS\Setup\Objective\ClientIdReadObjective; | ||
| use ILIAS\Setup\Objective\AdminConfirmedObjective; | ||
| use ilBackgroundTasksSetupConfig; | ||
|
|
||
| class BackgoundWorkerObjective extends AdminConfirmedObjective | ||
| { | ||
| private ?IOWrapper $io = null; | ||
| private ?Container $old_DIC = null; | ||
| private ?ilSetting $old_settings = null; | ||
|
|
||
| public function __construct() | ||
| { | ||
| parent::__construct( | ||
| "This will start a Background-Worker\n" . | ||
| 'Continue?' | ||
| ); | ||
| } | ||
|
|
||
| public function getHash(): string | ||
| { | ||
| return hash('sha256', self::class); | ||
| } | ||
|
|
||
| public function getLabel(): string | ||
| { | ||
| return 'Start a Background-Worker'; | ||
| } | ||
|
|
||
| public function isNotable(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @return Objective[] | ||
| */ | ||
| public function getPreconditions(Environment $environment): array | ||
| { | ||
| return [ | ||
| new ClientIdReadObjective(), | ||
| new ilIniFilesPopulatedObjective(), | ||
| new ilDatabaseInitializedObjective(), | ||
| new ilSettingsFactoryExistsObjective() | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function isApplicable(Environment $environment): bool | ||
| { | ||
| $ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI); | ||
|
|
||
| if (!$ini->groupExists('background_tasks')) { | ||
| return false; | ||
| } | ||
|
|
||
| return $ini->readVariable('background_tasks', 'concurrency') === ilBackgroundTasksSetupConfig::TYPE_ASYNCHRONOUS; | ||
| } | ||
|
|
||
| public function achieve(Environment $environment): Environment | ||
| { | ||
| $environment = parent::achieve($environment); | ||
| $io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION); | ||
| $settings_factory = $environment->getResource(Environment::RESOURCE_SETTINGS_FACTORY); | ||
| $ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI); | ||
|
|
||
| if ($io instanceof IOWrapper) { | ||
| $this->io = $io; | ||
| } | ||
|
|
||
| $settings = $settings_factory->settingsFor('common'); | ||
|
|
||
| try { | ||
| $internal_path = $settings->get('soap_internal_wsdl_path'); | ||
| if ($internal_path) { | ||
| $uri = (new \ILIAS\Data\URI($internal_path)); | ||
| parse_str($uri->getQuery() ?? '', $query); | ||
| $uri = (string) (isset($query['wsdl']) ? | ||
| $uri : | ||
| $uri->withQuery(http_build_query(array_merge($query, ['wsdl' => ''])))); | ||
| } elseif (trim($settings->get('soap_wsdl_path', '')) !== '') { | ||
| $uri = $settings->get('soap_wsdl_path', ''); | ||
| } else { | ||
| $uri = ilUtil::_getHttpPath() . '/public/soap/server.php?wsdl'; | ||
| } | ||
| $this->inform('Trying to call webserver by using WSDL: ' . $uri); | ||
| $soap_client = new SoapClient($uri, ['exceptions' => true, 'trace' => true, 'connection_timeout' => 10]); | ||
| $this->inform('SOAP client initialized'); | ||
|
|
||
| $client_id = defined('CLIENT_ID') ? CLIENT_ID : $environment->getResource(Environment::RESOURCE_CLIENT_ID); | ||
| if ($client_id === null) { | ||
| throw new RuntimeException('CLIENT_ID is not defined.'); | ||
| } | ||
|
|
||
| $url = $ini->readVariable('server', 'http_path'); | ||
| $session_url = rtrim($url, '/') . '/goto.php'; | ||
| $curl = $this->getCurlConnection($settings, $session_url); | ||
| $result = $curl->exec(); | ||
| $header_array = $curl->getResponseHeaderArray(); | ||
| $session_id = null; | ||
| if (isset($header_array['set-cookie'])) { | ||
| $set_cookie = $header_array['set-cookie']; | ||
| $cookies = is_array($set_cookie) ? $set_cookie : [$set_cookie]; | ||
| foreach ($cookies as $cookie) { | ||
| if (preg_match('/PHPSESSID=([^;]+)/i', $cookie, $matches)) { | ||
| $session_id = $matches[1]; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($session_id === null) { | ||
| throw new RuntimeException('Could not extract session ID from server response.'); | ||
| } | ||
|
|
||
| if (!$result) { | ||
| throw new RuntimeException('Could not retrieve session ID from server.'); | ||
| } | ||
|
|
||
| $this->inform('Obtained session ID: ' . substr($session_id, 0, 40)); | ||
| $session_string = $session_id ? ($session_id . '::' . $client_id) : ('::' . $client_id); | ||
| $this->inform('Calling startBackgroundTaskWorker with session: ' . ($session_id ? substr($session_id, 0, 40) : 'empty') . ', client: ' . $client_id); | ||
|
|
||
| $result = $soap_client->__call('startBackgroundTaskWorker', [ | ||
| $session_string, | ||
| ]); | ||
| if ($result) { | ||
| $this->inform('Calling webserver successful.'); | ||
| } else { | ||
| throw new RuntimeException('Calling webserver failed.'); | ||
| } | ||
| } catch (Throwable $t) { | ||
| $this->error($t->getMessage()); | ||
| $this->error('Calling webserver failed'); | ||
| $this->error('Stack trace: ' . $t->getTraceAsString()); | ||
| } | ||
|
|
||
| return $environment; | ||
| } | ||
|
|
||
| private function inform(string $text, bool $force = false): void | ||
| { | ||
| if ($this->io === null || (!$force && !$this->io->isVerbose())) { | ||
| return; | ||
| } | ||
|
|
||
| $this->io->inform($text); | ||
| } | ||
|
|
||
| private function error(string $text): void | ||
| { | ||
| if ($this->io === null) { | ||
| return; | ||
| } | ||
|
|
||
| $this->io->error($text); | ||
| } | ||
|
|
||
| /** | ||
| * @throws ilCurlConnectionException | ||
| */ | ||
| private function getCurlConnection(ilSetting $settings, string $url): ilCurlConnection | ||
| { | ||
| $curl = new ilCurlConnection( | ||
| $url, | ||
| new ilProxySettings($settings) | ||
| ); | ||
| $curl->init(); | ||
| $curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0); | ||
| $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 0); | ||
| $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); | ||
| $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1); | ||
| $curl->setOpt(CURLOPT_MAXREDIRS, 1); | ||
|
|
||
| return $curl; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.