Skip to content
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

chore: move the code to enable app for group to a job #3

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,46 @@ Available features:
occ app:enable groupquota
```

## Performance improving
Systemd service

- Create a systemd service file in /etc/systemd/system/admin-group-manager.service with the following content:
```ini
[Unit]
Description=Admin Group Manager worker
After=network.target

[Service]
ExecStart=/opt/admin-group-manager/taskprocessing.sh
Restart=always

[Install]
WantedBy=multi-user.target
```


- Create a shell script in /opt/admin-group-manager/taskprocessing.sh with the following content and make sure to make it executable:

```bash
#!/bin/sh
echo "Starting Admin Group Manager worker $1"
cd /path/to/nextcloud
sudo -u www-data php occ background-job:worker 'OCA\AdminGroupManager\BackgroundJob\EnableAppsForGroup'
```

- Enable and start the service:
```bash
systemctl enable --now admin-group-manager.service
```
- Check if is working fine:
```bash
systemctl list-units --type=service | grep admin-group-manager
```
- Check execution log:
```bash
journalctl -xeu nextcloud-ai-worker.service -f
```

## How to use

Check the available endpoints installing the app `ocs_api_viewer` and going to Nextcloud web interface at ocs_api_viewer app to verify the endpoints of this app.
3 changes: 3 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ Manage routines associates to admin groups]]></description>
<dependencies>
<nextcloud min-version="30" max-version="31"/>
</dependencies>
<background-jobs>
<job>OCA\AdminGroupManager\BackgroundJob\EnableAppsForGroup</job>
</background-jobs>
</info>
59 changes: 59 additions & 0 deletions lib/BackgroundJob/EnableAppsForGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AdminGroupManager\BackgroundJob;

use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\IAppConfig;

class EnableAppsForGroup extends QueuedJob {
private array $appIds;
private string $groupId;

public function __construct(
private IAppManager $appManager,
protected IAppConfig $appConfig,
protected ITimeFactory $time,
) {
parent::__construct($time);
}
protected function run($argument): void {
$this->setAllowParallelRuns(false);
if (!$this->validateAndProccessArguments($argument)) {
return;
}
foreach ($this->appIds as $appId) {
$appId = $this->appManager->cleanAppId($appId);
$enabled = $this->appConfig->getValueArray($appId, 'enabled', []);
if (!in_array($this->groupId, $enabled)) {
$enabled[] = $this->groupId;
$this->appManager->enableAppForGroups($appId, $enabled);
}
}
}

private function validateAndProccessArguments($argument): bool {
if (!isset($argument['groupId'])) {
return false;
}
if (!isset($argument['appIds'])) {
return false;
}
if (!is_array($argument['appIds'])) {
return false;
}
if (!count($argument['appIds'])) {
return false;
}
$this->appIds = $argument['appIds'];
$this->groupId = $argument['groupId'];
return true;
}
}
24 changes: 7 additions & 17 deletions lib/Controller/AdminGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\AdminGroupManager\Controller;

use OCA\AdminGroupManager\BackgroundJob\EnableAppsForGroup;
use OCA\AdminGroupManager\Controller\Attribute\RestrictIp;
use OCA\Provisioning_API\Controller\AUserData;
use OCA\Settings\Settings\Admin\Users;
Expand All @@ -17,6 +18,7 @@
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\ISubAdmin;
use OCP\IAppConfig;
Expand All @@ -41,6 +43,7 @@ public function __construct(
protected IAppConfig $appConfig,
protected IEventDispatcher $eventDispatcher,
protected ISecureRandom $secureRandom,
protected IJobList $jobList,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -134,23 +137,10 @@ private function setGroupQuota(string $groupId, string $quota): void {
$this->appConfig->setValueString('groupquota', 'quota_' . $groupId, (string)$quota);
}

/**
* TODO: Identify a best approach, the list of apps enabled to a group is a
* json field at appsettings table, could have problems with simultaneous
* update and also could be a very big array.
*
* @param array $appIds
* @param string $groupId
* @return void
*/
private function enableApps(array $appIds, string $groupId): void {
foreach ($appIds as $appId) {
$appId = $this->appManager->cleanAppId($appId);
$enabled = $this->appConfig->getValueArray($appId, 'enabled', []);
if (!in_array($groupId, $enabled)) {
$enabled[] = $groupId;
$this->appManager->enableAppForGroups($appId, $enabled);
}
}
$this->jobList->add(EnableAppsForGroup::class, [
'groupId' => $groupId,
'appIds' => $appIds,
]);
}
}