diff --git a/README.md b/README.md index 7290780..04b66f4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/appinfo/info.xml b/appinfo/info.xml index 8f3a2dc..1f5bc5e 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -23,4 +23,7 @@ Manage routines associates to admin groups]]> + + OCA\AdminGroupManager\BackgroundJob\EnableAppsForGroup + diff --git a/lib/BackgroundJob/EnableAppsForGroup.php b/lib/BackgroundJob/EnableAppsForGroup.php new file mode 100644 index 0000000..801d5c7 --- /dev/null +++ b/lib/BackgroundJob/EnableAppsForGroup.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/lib/Controller/AdminGroupController.php b/lib/Controller/AdminGroupController.php index a827b86..065bdfc 100644 --- a/lib/Controller/AdminGroupController.php +++ b/lib/Controller/AdminGroupController.php @@ -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; @@ -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; @@ -40,6 +42,7 @@ public function __construct( protected IAppConfig $appConfig, protected IEventDispatcher $eventDispatcher, protected ISecureRandom $secureRandom, + protected IJobList $jobList, ) { parent::__construct($appName, $request); } @@ -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, + ]); } }