Skip to content

Commit e6036fc

Browse files
authored
feat: use lazy app config values and small fixes (#191)
* feat: use lazy app config values and small fixes - use `isEnabledForAnyone` instead of `isInstalled` to check for app_api - type annotations for both OCP and the OCA IContentProvider - make `indexed_files_count` app config non-lazy Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> * feat: use lazy app config values and small fixes - use `isEnabledForAnyone` instead of `isInstalled` to check for app_api - type annotations for both OCP and the OCA IContentProvider - make `indexed_files_count` app config non-lazy Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> * address review comments Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> * migrate config values to be lazy Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> * use IAppConfig instead of DB query to make lazy configs Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> --------- Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent 0ccd24c commit e6036fc

26 files changed

Lines changed: 174 additions & 105 deletions

‎.github/workflows/integration-test.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ jobs:
573573
# Run cron in speed mode: Set interval to 0 minutes
574574
./occ config:app:set --value 30 --type integer context_chat indexing_job_interval # 30 seconds
575575
./occ config:app:set --value 10 --type integer context_chat crawl_job_interval # 10 seconds
576-
./occ config:app:set --value 10 --type integer context_chat action_job_interval # 10 seconds
577-
./occ config:app:set --value 10 --type integer context_chat fs_listener_job_interval # 10 seconds
576+
./occ config:app:set --value '10' --type string context_chat action_job_interval # 10 seconds
577+
./occ config:app:set --value '10' --type string context_chat fs_listener_job_interval # 10 seconds
578578
for i in {1..100}; do
579579
php cron.php & # Starting with stable31 we can use -v here for better visibility
580580
wait
@@ -822,8 +822,8 @@ jobs:
822822
# Run cron in speed mode
823823
./occ config:app:set --value 30 --type integer context_chat indexing_job_interval # 30 seconds
824824
./occ config:app:set --value 10 --type integer context_chat crawl_job_interval # 10 seconds
825-
./occ config:app:set --value 10 --type integer context_chat action_job_interval # 10 seconds
826-
./occ config:app:set --value 10 --type integer context_chat fs_listener_job_interval # 10 seconds
825+
./occ config:app:set --value '10' --type string context_chat action_job_interval # 10 seconds
826+
./occ config:app:set --value '10' --type string context_chat fs_listener_job_interval # 10 seconds
827827
# Run normal indexing jobs which will only pick up welcome.txt etc
828828
for i in {1..10}; do
829829
php cron.php & # Starting with stable31 we can use -v here for better visibility

‎appinfo/info.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Setup background job workers as described here: https://docs.nextcloud.com/serve
2222
Note:
2323
Refer to the [Context Chat Backend's readme](https://github.com/nextcloud/context_chat_backend/?tab=readme-ov-file) and the [AppAPI's documentation](https://cloud-py-api.github.io/app_api/) for help with setup of AppAPI's deploy daemon.
2424
]]></description>
25-
<version>5.0.0</version>
25+
<version>5.0.1</version>
2626
<licence>agpl</licence>
2727
<author>Julien Veyssier</author>
2828
<author>Anupam Kumar</author>

‎lib/AppInfo/Application.php‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use OCP\Files\Events\Node\NodeRenamedEvent;
2929
use OCP\Files\Events\Node\NodeWrittenEvent;
3030
use OCP\Files\Events\NodeRemovedFromCache;
31-
use OCP\IConfig;
3231
use OCP\Share\Events\ShareCreatedEvent;
3332
use OCP\Share\Events\ShareDeletedEvent;
3433
use OCP\User\Events\UserDeletedEvent;
@@ -64,13 +63,8 @@ class Application extends App implements IBootstrap {
6463
'text/org',
6564
];
6665

67-
private IConfig $config;
68-
6966
public function __construct(array $urlParams = []) {
7067
parent::__construct(self::APP_ID, $urlParams);
71-
72-
$container = $this->getContainer();
73-
$this->config = $container->get(IConfig::class);
7468
}
7569

7670
public function register(IRegistrationContext $context): void {

‎lib/BackgroundJobs/ActionJob.php‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
use OCA\ContextChat\Service\LangRopeService;
1616
use OCA\ContextChat\Type\ActionType;
1717
use OCP\App\IAppManager;
18+
use OCP\AppFramework\Services\IAppConfig;
1819
use OCP\AppFramework\Utility\ITimeFactory;
19-
use OCP\BackgroundJob\IJobList;
2020
use OCP\BackgroundJob\TimedJob;
2121
use OCP\DB\Exception;
22-
use OCP\IConfig;
2322

2423
class ActionJob extends TimedJob {
2524
private const BATCH_SIZE = 1000;
@@ -28,23 +27,22 @@ public function __construct(
2827
ITimeFactory $timeFactory,
2928
private LangRopeService $networkService,
3029
private QueueActionMapper $actionMapper,
31-
private IJobList $jobList,
3230
private Logger $logger,
3331
private DiagnosticService $diagnosticService,
3432
private IAppManager $appManager,
35-
private IConfig $config,
33+
private IAppConfig $appConfig,
3634
) {
3735
parent::__construct($timeFactory);
3836
$this->setAllowParallelRuns(false);
3937
$this->setInterval($this->getJobInterval());
4038
}
4139

4240
private function getJobInterval(): int {
43-
return intval($this->config->getAppValue('context_chat', 'action_job_interval', (string)(5 * 60))); // 5 minutes
41+
return intval($this->appConfig->getAppValueString('action_job_interval', (string)(5 * 60), lazy: true)); // 5 minutes
4442
}
4543

4644
protected function run($argument): void {
47-
if (!$this->appManager->isInstalled('app_api')) {
45+
if (!$this->appManager->isEnabledForAnyone('app_api')) {
4846
$this->logger->warning('ActionJob is skipped as app_api is disabled');
4947
return;
5048
}

‎lib/BackgroundJobs/FileSystemListenerJob.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use OCA\ContextChat\Service\FsEventService;
1717
use OCA\ContextChat\Type\FsEventType;
1818
use OCP\App\IAppManager;
19+
use OCP\AppFramework\Services\IAppConfig;
1920
use OCP\AppFramework\Utility\ITimeFactory;
2021
use OCP\BackgroundJob\TimedJob;
2122
use OCP\DB\Exception;
2223
use OCP\Files\IRootFolder;
23-
use OCP\IConfig;
2424

2525
class FileSystemListenerJob extends TimedJob {
2626
private const BATCH_SIZE = 500;
@@ -40,19 +40,19 @@ public function __construct(
4040
private IAppManager $appManager,
4141
private FsEventService $fsEventService,
4242
private IRootFolder $rootFolder,
43-
private IConfig $config,
43+
private IAppConfig $appConfig,
4444
) {
4545
parent::__construct($timeFactory);
4646
$this->allowParallelRuns = false;
4747
$this->setInterval($this->getJobInterval());
4848
}
4949

5050
private function getJobInterval(): int {
51-
return intval($this->config->getAppValue('context_chat', 'fs_listener_job_interval', (string)(5 * 60))); // 5 minutes
51+
return intval($this->appConfig->getAppValueString('fs_listener_job_interval', (string)(5 * 60), lazy: true)); // 5 minutes
5252
}
5353

5454
protected function run($argument): void {
55-
if (!$this->appManager->isInstalled('app_api')) {
55+
if (!$this->appManager->isEnabledForAnyone('app_api')) {
5656
$this->logger->warning('FileSystemListenerJob is skipped as app_api is disabled');
5757
return;
5858
}

‎lib/BackgroundJobs/IndexerJob.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public function __construct(
8484
* @throws \Throwable
8585
*/
8686
public function run($argument): void {
87-
if (!$this->appManager->isInstalled('app_api')) {
87+
if (!$this->appManager->isEnabledForAnyone('app_api')) {
8888
$this->logger->warning('IndexerJob is skipped as app_api is disabled');
8989
return;
9090
}
9191

9292
$this->storageId = $argument['storageId'];
9393
$this->rootId = $argument['rootId'];
94-
if ($this->appConfig->getAppValue('auto_indexing', 'true') === 'false') {
94+
if ($this->appConfig->getAppValueString('auto_indexing', 'true', lazy: true) === 'false') {
9595
return;
9696
}
9797
$this->diagnosticService->sendJobTrigger(static::class, $this->getId());
@@ -149,19 +149,19 @@ public function run($argument): void {
149149
}
150150

151151
protected function getBatchSize(): int {
152-
return $this->appConfig->getAppValueInt('indexing_batch_size', self::DEFAULT_BATCH_SIZE);
152+
return $this->appConfig->getAppValueInt('indexing_batch_size', self::DEFAULT_BATCH_SIZE, lazy: true);
153153
}
154154

155155
protected function getMaxIndexingTime(): int {
156-
return $this->appConfig->getAppValueInt('indexing_max_time', self::DEFAULT_MAX_INDEXING_TIME);
156+
return $this->appConfig->getAppValueInt('indexing_max_time', self::DEFAULT_MAX_INDEXING_TIME, lazy: true);
157157
}
158158

159159
protected function getJobInterval(): int {
160-
return $this->appConfig->getAppValueInt('indexing_job_interval', self::DEFAULT_JOB_INTERVAL);
160+
return $this->appConfig->getAppValueInt('indexing_job_interval', self::DEFAULT_JOB_INTERVAL, lazy: true);
161161
}
162162

163163
protected function getMaxSize(): float {
164-
return (float)$this->appConfig->getAppValueInt('indexing_max_size', Application::CC_MAX_SIZE);
164+
return (float)$this->appConfig->getAppValueInt('indexing_max_size', Application::CC_MAX_SIZE, lazy: true);
165165
}
166166

167167
/**
@@ -321,7 +321,7 @@ protected function index(array $files): void {
321321
}
322322

323323
private function setInitialIndexCompletion(): void {
324-
if ($this->appConfig->getAppValueInt('last_indexed_time', 0) !== 0) {
324+
if ($this->appConfig->getAppValueInt('last_indexed_time', 0, lazy: true) !== 0) {
325325
return;
326326
}
327327
try {
@@ -345,7 +345,7 @@ private function setInitialIndexCompletion(): void {
345345
}
346346

347347
$this->logger->info('Initial index completion detected, setting last indexed time');
348-
$this->appConfig->setAppValueInt('last_indexed_time', $this->timeFactory->getTime(), false);
348+
$this->appConfig->setAppValueInt('last_indexed_time', $this->timeFactory->getTime(), lazy: true);
349349
}
350350

351351
/**

‎lib/BackgroundJobs/InitialContentImportJob.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
namespace OCA\ContextChat\BackgroundJobs;
1111

1212
use OCA\ContextChat\Logger;
13-
use OCA\ContextChat\Public\IContentProvider;
13+
use OCA\ContextChat\Public\IContentProvider as OCAIContentProvider;
1414
use OCA\ContextChat\Service\ProviderConfigService;
1515
use OCP\App\IAppManager;
1616
use OCP\AppFramework\Utility\ITimeFactory;
1717
use OCP\BackgroundJob\QueuedJob;
18+
use OCP\ContextChat\IContentProvider as OCPIContentProvider;
1819
use OCP\IUserManager;
1920
use OCP\Server;
2021
use Psr\Container\ContainerExceptionInterface;
@@ -33,7 +34,7 @@ public function __construct(
3334
}
3435

3536
/**
36-
* @param class-string<IContentProvider> $argument Provider class name
37+
* @param class-string<OCAIContentProvider|OCPIContentProvider> $argument Provider class name
3738
* @return void
3839
*/
3940
protected function run($argument): void {
@@ -42,14 +43,15 @@ protected function run($argument): void {
4243
}
4344

4445
try {
45-
/** @var IContentProvider */
46+
/** @var OCAIContentProvider|OCPIContentProvider */
4647
$providerObj = Server::get($argument);
4748
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
4849
$this->logger->warning('[InitialContentImportJob] Could not run initial import for content provider', ['exception' => $e]);
4950
return;
5051
}
5152

52-
if (!$this->appManager->isEnabledForUser($providerObj->getAppId())) {
53+
$user = $this->userMan->get($this->userId ?? '');
54+
if (!$this->appManager->isEnabledForUser($providerObj->getAppId(), $user)) {
5355
$this->logger->info('[InitialContentImportJob] App is not enabled for user, skipping content import', ['appId' => $providerObj->getAppId()]);
5456
return;
5557
}

‎lib/BackgroundJobs/RotateLogsJob.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace OCA\ContextChat\BackgroundJobs;
99

10+
use OCP\AppFramework\Services\IAppConfig;
1011
use OCP\AppFramework\Utility\ITimeFactory;
1112
use OCP\BackgroundJob\TimedJob;
1213
use OCP\IConfig;
@@ -18,15 +19,16 @@ class RotateLogsJob extends TimedJob {
1819
public function __construct(
1920
ITimeFactory $time,
2021
private IConfig $config,
22+
private IAppConfig $appConfig,
2123
) {
2224
parent::__construct($time);
2325

24-
$this->setInterval(60 * 60 * 3);
26+
$this->setInterval(60 * 60 * 3); // every 3 hours
2527
}
2628

2729
protected function run($argument): void {
2830
$default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/context_chat.log';
29-
$this->filePath = $this->config->getAppValue('context_chat', 'logfile', $default);
31+
$this->filePath = $this->appConfig->getAppValueString('logfile', $default, lazy: true);
3032

3133
$this->maxSize = $this->config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
3234

‎lib/BackgroundJobs/SchedulerJob.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function __construct(
3434
* @throws Exception
3535
*/
3636
protected function run($argument): void {
37-
$this->appConfig->setAppValueString('indexed_files_count', (string)0);
38-
$this->appConfig->setAppValueInt('last_indexed_time', 0);
37+
$this->appConfig->setAppValueString('indexed_files_count', (string)0, lazy: true);
38+
$this->appConfig->setAppValueInt('last_indexed_time', 0, lazy: true);
3939
foreach ($this->storageService->getMounts() as $mount) {
4040
$this->logger->debug('Scheduling StorageCrawlJob storage_id=' . $mount['storage_id'] . ' root_id=' . $mount['root_id' ] . 'override_root=' . $mount['overridden_root']);
4141
$this->jobList->add(StorageCrawlJob::class, [

‎lib/BackgroundJobs/StorageCrawlJob.php‎

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

1111
namespace OCA\ContextChat\BackgroundJobs;
1212

13-
use OCA\ContextChat\AppInfo\Application;
1413
use OCA\ContextChat\Db\QueueFile;
1514
use OCA\ContextChat\Logger;
1615
use OCA\ContextChat\Service\DiagnosticService;
1716
use OCA\ContextChat\Service\QueueService;
1817
use OCA\ContextChat\Service\StorageService;
18+
use OCP\AppFramework\Services\IAppConfig;
1919
use OCP\AppFramework\Utility\ITimeFactory;
2020
use OCP\BackgroundJob\IJobList;
2121
use OCP\BackgroundJob\QueuedJob;
2222
use OCP\DB\Exception;
23-
use OCP\IAppConfig;
2423

2524
class StorageCrawlJob extends QueuedJob {
2625
public const BATCH_SIZE = 2000;
@@ -91,7 +90,7 @@ protected function run($argument): void {
9190

9291
if ($lastSuccessfulFileId !== -1) {
9392
// the last job to set this value will win
94-
$this->appConfig->setValueInt(Application::APP_ID, 'last_indexed_file_id', $lastSuccessfulFileId);
93+
$this->appConfig->setAppValueInt('last_indexed_file_id', $lastSuccessfulFileId, lazy: true);
9594
}
9695
}
9796
} finally {
@@ -100,6 +99,6 @@ protected function run($argument): void {
10099
}
101100

102101
protected function getJobInterval(): int {
103-
return $this->appConfig->getValueInt(Application::APP_ID, 'crawl_job_interval', self::DEFAULT_JOB_INTERVAL);
102+
return $this->appConfig->getAppValueInt('crawl_job_interval', self::DEFAULT_JOB_INTERVAL, lazy: true);
104103
}
105104
}

0 commit comments

Comments
 (0)