Skip to content

Commit ff30433

Browse files
Merge pull request #54196 from nextcloud/enh/noid/taskprocessing-lazy-config
[TaskProcessing] Load and store some config keys lazily
2 parents a3f1b07 + be7ef43 commit ff30433

File tree

7 files changed

+34
-40
lines changed

7 files changed

+34
-40
lines changed

apps/settings/lib/Controller/AISettingsController.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@
1212
use OCP\AppFramework\Controller;
1313
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
1414
use OCP\AppFramework\Http\DataResponse;
15-
use OCP\IConfig;
15+
use OCP\IAppConfig;
1616
use OCP\IRequest;
1717

1818
class AISettingsController extends Controller {
1919

20-
/**
21-
* @param string $appName
22-
* @param IRequest $request
23-
* @param IConfig $config
24-
*/
2520
public function __construct(
2621
$appName,
2722
IRequest $request,
28-
private IConfig $config,
23+
private IAppConfig $appConfig,
2924
) {
3025
parent::__construct($appName, $request);
3126
}
@@ -43,7 +38,7 @@ public function update($settings) {
4338
if (!isset($settings[$key])) {
4439
continue;
4540
}
46-
$this->config->setAppValue('core', $key, json_encode($settings[$key]));
41+
$this->appConfig->setValueString('core', $key, json_encode($settings[$key]), lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
4742
}
4843

4944
return new DataResponse();

apps/settings/lib/Settings/Admin/ArtificialIntelligence.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use OCP\AppFramework\Http\TemplateResponse;
1212
use OCP\AppFramework\Services\IInitialState;
13-
use OCP\IConfig;
13+
use OCP\IAppConfig;
1414
use OCP\IL10N;
1515
use OCP\Settings\IDelegatedSettings;
1616
use OCP\SpeechToText\ISpeechToTextManager;
@@ -28,7 +28,7 @@
2828

2929
class ArtificialIntelligence implements IDelegatedSettings {
3030
public function __construct(
31-
private IConfig $config,
31+
private IAppConfig $appConfig,
3232
private IL10N $l,
3333
private IInitialState $initialState,
3434
private ITranslationManager $translationManager,
@@ -145,7 +145,7 @@ public function getForm() {
145145
];
146146
foreach ($settings as $key => $defaultValue) {
147147
$value = $defaultValue;
148-
$json = $this->config->getAppValue('core', $key, '');
148+
$json = $this->appConfig->getValueString('core', $key, '', lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
149149
if ($json !== '') {
150150
try {
151151
$value = json_decode($json, true, flags: JSON_THROW_ON_ERROR);

build/psalm-baseline.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,11 +2018,6 @@
20182018
<code><![CDATA[getSettingsManager]]></code>
20192019
</UndefinedInterfaceMethod>
20202020
</file>
2021-
<file src="apps/settings/lib/Controller/AISettingsController.php">
2022-
<DeprecatedMethod>
2023-
<code><![CDATA[setAppValue]]></code>
2024-
</DeprecatedMethod>
2025-
</file>
20262021
<file src="apps/settings/lib/Controller/AppSettingsController.php">
20272022
<DeprecatedMethod>
20282023
<code><![CDATA[getAppValue]]></code>
@@ -2083,9 +2078,6 @@
20832078
continue;
20842079
}]]></code>
20852080
</DeprecatedInterface>
2086-
<DeprecatedMethod>
2087-
<code><![CDATA[getAppValue]]></code>
2088-
</DeprecatedMethod>
20892081
</file>
20902082
<file src="apps/settings/lib/Settings/Admin/Security.php">
20912083
<UndefinedInterfaceMethod>
@@ -3009,12 +3001,6 @@
30093001
<code><![CDATA[resetDelayForIP]]></code>
30103002
</DeprecatedMethod>
30113003
</file>
3012-
<file src="core/Command/TaskProcessing/EnabledCommand.php">
3013-
<DeprecatedMethod>
3014-
<code><![CDATA[getAppValue]]></code>
3015-
<code><![CDATA[setAppValue]]></code>
3016-
</DeprecatedMethod>
3017-
</file>
30183004
<file src="core/Command/Upgrade.php">
30193005
<DeprecatedMethod>
30203006
<code><![CDATA[listen]]></code>

core/Command/TaskProcessing/EnabledCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace OC\Core\Command\TaskProcessing;
88

99
use OC\Core\Command\Base;
10-
use OCP\IConfig;
10+
use OCP\IAppConfig;
1111
use OCP\TaskProcessing\IManager;
1212
use Symfony\Component\Console\Input\InputArgument;
1313
use Symfony\Component\Console\Input\InputInterface;
@@ -16,7 +16,7 @@
1616
class EnabledCommand extends Base {
1717
public function __construct(
1818
protected IManager $taskProcessingManager,
19-
private IConfig $config,
19+
private IAppConfig $appConfig,
2020
) {
2121
parent::__construct();
2222
}
@@ -41,7 +41,7 @@ protected function configure() {
4141
protected function execute(InputInterface $input, OutputInterface $output): int {
4242
$enabled = (bool)$input->getArgument('enabled');
4343
$taskType = $input->getArgument('task-type-id');
44-
$json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences');
44+
$json = $this->appConfig->getValueString('core', 'ai.taskprocessing_type_preferences', lazy: true);
4545
try {
4646
if ($json === '') {
4747
$taskTypeSettings = [];
@@ -51,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151

5252
$taskTypeSettings[$taskType] = $enabled;
5353

54-
$this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskTypeSettings));
54+
$this->appConfig->setValueString('core', 'ai.taskprocessing_type_preferences', json_encode($taskTypeSettings), lazy: true);
5555
$this->writeArrayInOutputFormat($input, $output, $taskTypeSettings);
5656
return 0;
5757
} catch (\JsonException $e) {

lib/private/TaskProcessing/Manager.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
use OCP\Files\NotPermittedException;
3232
use OCP\Files\SimpleFS\ISimpleFile;
3333
use OCP\Http\Client\IClientService;
34+
use OCP\IAppConfig;
3435
use OCP\ICache;
3536
use OCP\ICacheFactory;
36-
use OCP\IConfig;
3737
use OCP\IL10N;
3838
use OCP\IServerContainer;
3939
use OCP\IUserManager;
@@ -73,6 +73,11 @@ class Manager implements IManager {
7373
public const LEGACY_PREFIX_TEXTTOIMAGE = 'legacy:TextToImage:';
7474
public const LEGACY_PREFIX_SPEECHTOTEXT = 'legacy:SpeechToText:';
7575

76+
public const LAZY_CONFIG_KEYS = [
77+
'ai.taskprocessing_type_preferences',
78+
'ai.taskprocessing_provider_preferences',
79+
];
80+
7681
/** @var list<IProvider>|null */
7782
private ?array $providers = null;
7883

@@ -92,7 +97,7 @@ class Manager implements IManager {
9297
private ?GetTaskProcessingProvidersEvent $eventResult = null;
9398

9499
public function __construct(
95-
private IConfig $config,
100+
private IAppConfig $appConfig,
96101
private Coordinator $coordinator,
97102
private IServerContainer $serverContainer,
98103
private LoggerInterface $logger,
@@ -630,7 +635,7 @@ private function _getTaskTypes(): array {
630635
*/
631636
private function _getTaskTypeSettings(): array {
632637
try {
633-
$json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences', '');
638+
$json = $this->appConfig->getValueString('core', 'ai.taskprocessing_type_preferences', '', lazy: true);
634639
if ($json === '') {
635640
return [];
636641
}
@@ -788,7 +793,11 @@ public function getPreferredProvider(string $taskTypeId) {
788793
if ($this->preferences === null) {
789794
$this->preferences = $this->distributedCache->get('ai.taskprocessing_provider_preferences');
790795
if ($this->preferences === null) {
791-
$this->preferences = json_decode($this->config->getAppValue('core', 'ai.taskprocessing_provider_preferences', 'null'), associative: true, flags: JSON_THROW_ON_ERROR);
796+
$this->preferences = json_decode(
797+
$this->appConfig->getValueString('core', 'ai.taskprocessing_provider_preferences', 'null', lazy: true),
798+
associative: true,
799+
flags: JSON_THROW_ON_ERROR,
800+
);
792801
$this->distributedCache->set('ai.taskprocessing_provider_preferences', $this->preferences, 60 * 3);
793802
}
794803
}
@@ -889,7 +898,7 @@ private function checkGuestAccess(?string $userId = null): bool {
889898
$user = $this->userManager->get($userId);
890899
}
891900

892-
$guestsAllowed = $this->config->getAppValue('core', 'ai.taskprocessing_guests', 'false');
901+
$guestsAllowed = $this->appConfig->getValueString('core', 'ai.taskprocessing_guests', 'false');
893902
if ($guestsAllowed == 'true' || !class_exists(\OCA\Guests\UserBackend::class) || !($user->getBackend() instanceof \OCA\Guests\UserBackend)) {
894903
return true;
895904
}

lib/public/TaskProcessing/IManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @since 30.0.0
2727
*/
2828
interface IManager {
29+
2930
/**
3031
* @since 30.0.0
3132
*/

tests/lib/TaskProcessing/TaskProcessingTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OCP\Files\File;
2525
use OCP\Files\IRootFolder;
2626
use OCP\Http\Client\IClientService;
27+
use OCP\IAppConfig;
2728
use OCP\ICacheFactory;
2829
use OCP\IConfig;
2930
use OCP\IDBConnection;
@@ -535,6 +536,7 @@ class TaskProcessingTest extends \Test\TestCase {
535536
private IUserMountCache $userMountCache;
536537
private IRootFolder $rootFolder;
537538
private IConfig $config;
539+
private IAppConfig $appConfig;
538540

539541
public const TEST_USER = 'testuser';
540542

@@ -600,8 +602,9 @@ protected function setUp(): void {
600602

601603
$this->userMountCache = $this->createMock(IUserMountCache::class);
602604
$this->config = Server::get(IConfig::class);
605+
$this->appConfig = Server::get(IAppConfig::class);
603606
$this->manager = new Manager(
604-
$this->config,
607+
$this->appConfig,
605608
$this->coordinator,
606609
$this->serverContainer,
607610
Server::get(LoggerInterface::class),
@@ -641,7 +644,7 @@ public function testProviderShouldBeRegisteredAndTaskTypeDisabled(): void {
641644
$taskProcessingTypeSettings = [
642645
TextToText::ID => false,
643646
];
644-
$this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings));
647+
$this->appConfig->setValueString('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings), lazy: true);
645648
self::assertCount(0, $this->manager->getAvailableTaskTypes());
646649
self::assertCount(1, $this->manager->getAvailableTaskTypes(true));
647650
self::assertTrue($this->manager->hasProviders());
@@ -651,7 +654,7 @@ public function testProviderShouldBeRegisteredAndTaskTypeDisabled(): void {
651654

652655

653656
public function testProviderShouldBeRegisteredAndTaskFailValidation(): void {
654-
$this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', '');
657+
$this->appConfig->setValueString('core', 'ai.taskprocessing_type_preferences', '', lazy: true);
655658
$this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([
656659
new ServiceRegistration('test', BrokenSyncProvider::class)
657660
]);
@@ -797,7 +800,7 @@ public function testTaskTypeExplicitlyEnabled(): void {
797800
$taskProcessingTypeSettings = [
798801
TextToText::ID => true,
799802
];
800-
$this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings));
803+
$this->appConfig->setValueString('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings), lazy: true);
801804

802805
self::assertCount(1, $this->manager->getAvailableTaskTypes());
803806

@@ -1239,7 +1242,7 @@ public function testMergeTaskTypesLocalAndEvent() {
12391242

12401243
private function createManagerInstance(): Manager {
12411244
// Clear potentially cached config values if needed
1242-
$this->config->deleteAppValue('core', 'ai.taskprocessing_type_preferences');
1245+
$this->appConfig->deleteKey('core', 'ai.taskprocessing_type_preferences');
12431246

12441247
// Re-create Text2ImageManager if its state matters or mocks change
12451248
$text2imageManager = new \OC\TextToImage\Manager(
@@ -1253,7 +1256,7 @@ private function createManagerInstance(): Manager {
12531256
);
12541257

12551258
return new Manager(
1256-
$this->config,
1259+
$this->appConfig,
12571260
$this->coordinator,
12581261
$this->serverContainer,
12591262
Server::get(LoggerInterface::class),

0 commit comments

Comments
 (0)