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

Systemd unit file #23

Closed
sm8ps opened this issue Aug 18, 2022 · 4 comments
Closed

Systemd unit file #23

sm8ps opened this issue Aug 18, 2022 · 4 comments

Comments

@sm8ps
Copy link

sm8ps commented Aug 18, 2022

I have been struggling literally for years with the Nextcloud client not recognizing changes in external storage. The first and only useful information about a working solution I found in the Nextcloud forum but it is lacking a systemd unit file for non-Docker set-ups. I have cobbled one together that I want to share. It is functional in Nextcloud 24 on Ubuntu 20.04. This is my first try at such a thing and comments as well as suggestions are welcome. I include the full source code here in order to facilitate the discussion.

The start script launches files_external:notify for each mount ID present. It does not change fs.inotify.max_user_watches which might be necessary if the total number of folders being watched is greater than 8192. That could be scripted as well but I do not have need for it now. A manual work-around is to set that value in /etc/sysctl.conf.

/etc/systemd/system/nextcloud-files-inotify.service (-rw-r--r-- 1 root root)

[Unit]
Description=Files-inotify launcher for local external storage one Nextcloud
After=network.target mysql.service
Requires=mysql.service
[Service]
Type=forking
User=www-data
Group=www-data
WorkingDirectory=/var/www/nextcloud
ExecStart=/usr/local/bin/nextcloud-files-external-notify.start
ExecStop=/usr/local/bin/nextcloud-files-external-notify.stop
Nice=19
Restart=always
[Install]
WantedBy=multi-user.target

/usr/local/bin/nextcloud-files-external-notify.start (-rwxr--r-- 1 www-data www-data)

for i in $(/usr/bin/php /var/www/nextcloud/occ files_external:list --output=json | jq '.[].mount_id')
do
        /usr/bin/php /var/www/nextcloud/occ files_external:notify -n $i &  
done

/usr/local/bin/nextcloud-files-external-notify.stop (-rwxr--r-- 1 www-data www-data)

pkill -f '/usr/bin/php /var/www/nextcloud/occ files_external:notify -n [0-9]+'
@sm8ps
Copy link
Author

sm8ps commented Aug 24, 2022

I am having issues with this set-up. Eventually one of the processes started in the background (one for each mount ID) crashes.

Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: An unhandled exception has been thrown:
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: Error: Call to undefined method OC\DB\ConnectionAdapter::isConnected() in /var/www/nextcloud/apps/files_external/lib/Command/Notif>
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: Stack trace:
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #0 /var/www/nextcloud/apps/files_external/lib/Command/Notify.php(225): OCA\Files_External\Command\Notify->reconnectToDatabase()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #1 /var/www/nextcloud/apps/files_external/lib/Command/Notify.php(205): OCA\Files_External\Command\Notify->markParentAsOutdated()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #2 /var/www/nextcloud/apps/files_inotify/lib/Storage/NotifyHandler.php(238): OCA\Files_External\Command\Notify->OCA\Files_External>
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #3 /var/www/nextcloud/apps/files_external/lib/Command/Notify.php(206): OCA\Files_INotify\Storage\NotifyHandler->listen()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #4 /var/www/nextcloud/3rdparty/symfony/console/Command/Command.php(255): OCA\Files_External\Command\Notify->execute()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #5 /var/www/nextcloud/core/Command/Base.php(168): Symfony\Component\Console\Command\Command->run()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #6 /var/www/nextcloud/3rdparty/symfony/console/Application.php(1009): OC\Core\Command\Base->run()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #7 /var/www/nextcloud/3rdparty/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #8 /var/www/nextcloud/3rdparty/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #9 /var/www/nextcloud/lib/private/Console/Application.php(211): Symfony\Component\Console\Application->run()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #10 /var/www/nextcloud/console.php(100): OC\Console\Application->run()
Aug 24 10:08:33 lxcNextCloud201806 nextcloud-files-external-notify.start[63557]: #11 /var/www/nextcloud/occ(11): require_once('/var/www/nextcl...')

When using the start script as above, this would happen regularly immediately after launching the script. I then added sleep 1 after the invocation of files_external:notify -n which cured the symptom for about 20 hours but then the error happened. Unfortunately, Systemd does not seem to notice it as failure and does not restart the service. -- Any suggestions much appreciated!

@sm8ps
Copy link
Author

sm8ps commented Sep 8, 2022

In case anyone cares, my work-around to the above issue consists in calling '/bin/systemctl restart' hourly on the service file from /etc/contab. No crashes have been observed as by journalctl.

@sm8ps
Copy link
Author

sm8ps commented Sep 14, 2022

I just wanted to confirm that this work-around seems to be working, indeed! No crash has been observed over the 25 days that it has been in action.

@nursoda
Copy link

nursoda commented Aug 26, 2023

Here's my first poor man's version of a systemd file on my Arch Linux server:

[Unit]
Description=files_inotify watcher for ONE_SPECIFIC_DIRECTORY on MY_SERVER
After=network.target mariadb.service nginx.service php-fpm.service redis.service
Requires=mariadb.service nginx.service php-fpm.service redis.service

[Service]
User=http
WorkingDirectory=/PATH_TO_MY_CLOUD
ExecCondition=/usr/bin/php -f /PATH_TO_MY_CLOUD/occ status -e
ExecStart=/usr/bin/php -f PATH_TO_MY_CLOUD/occ files_external:notify -v ID_OF_THE_EXTERNAL_STORAGE
Nice=19
Restart=always

[Install]
WantedBy=multi-user.target

I expect this to be more robust due to Restart=always. It should be possible to use systemd Unit templates, and some script to actually derive service instances for each external ID from that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants