Skip to content

Commit

Permalink
Merge pull request #3 from LibreSign/chore/move-enable-apps-for-group…
Browse files Browse the repository at this point in the history
…-to-job

chore: move the code to enable app for group to a job
  • Loading branch information
vitormattos authored Oct 10, 2024
2 parents 3e56b1c + e7961ff commit dc46e37
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 17 deletions.
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\Settings\Settings\Admin\Users;
use OCP\App\IAppManager;
Expand All @@ -16,6 +17,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 @@ -40,6 +42,7 @@ public function __construct(
protected IAppConfig $appConfig,
protected IEventDispatcher $eventDispatcher,
protected ISecureRandom $secureRandom,
protected IJobList $jobList,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -165,23 +168,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,
]);
}
}

0 comments on commit dc46e37

Please sign in to comment.