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

using tempdirectory from config/config.php #330

Closed
ernolf opened this issue Nov 28, 2022 · 6 comments
Closed

using tempdirectory from config/config.php #330

ernolf opened this issue Nov 28, 2022 · 6 comments

Comments

@ernolf
Copy link

ernolf commented Nov 28, 2022

The app does not use the temporary directory defined in the config/config.php nor the one defined in php.ini under 'sys_temp_dir'.
My normal /tmp dir is tmpfs and limited in space. The result after a occ preview:generate-all was a server down after the tmp directory was full, until I debugged the problem.

Edit: I would actually expect the temp files to be removed by the app as soon as they have done their job.

@st3iny
Copy link
Member

st3iny commented Dec 2, 2022

I gues it depends on the preview provider. Are you using a custom third party provider like e.g. OC\Preview\Movie?

Those external binaries might use separate mechanisms for determining a temp directory.

Please post a list of your enabled preview providers (config.php -> enabledPreviewProviders).

@ernolf
Copy link
Author

ernolf commented Dec 2, 2022

Those external binaries might use separate mechanisms for determining a temp directory.

Here the enabled preview providers on the machine in question:

  'enabledPreviewProviders' => 
  array (
    0 => 'OC\\Preview\\PNG',
    1 => 'OC\\Preview\\JPEG',
    2 => 'OC\\Preview\\GIF',
    3 => 'OC\\Preview\\HEIC',
    4 => 'OC\\Preview\\BMP',
    5 => 'OC\\Preview\\XBitmap',
    6 => 'OC\\Preview\\MP3',
    7 => 'OC\\Preview\\TXT',
    8 => 'OC\\Preview\\SVG',
    9 => 'OC\\Preview\\TIFF',
    10 => 'OC\\Preview\\Photoshop',
    11 => 'OC\\Preview\\Font',
    12 => 'OC\\Preview\\MarkDown',
    13 => 'OC\\Preview\\Illustrator',
    14 => 'OC\\Preview\\Movie',
  ),

What makes me ponder though is that the /tmp directory fill up only happens when I start this app and these entries all look the same and are not deleted afterwards. However, when I disable the app and the thumbnails/previews are created "on the fly" when opening a directory, including those for videos etc., /tmp is not used at all.
Everything runs via the tmp-dir defined in config/config.php (/tmp4www in my case), which I defined in both php.ini files (for cli as well as for fpm).
I hope this could help you. It should be easy to reproduce.

@st3iny
Copy link
Member

st3iny commented Feb 10, 2023

It is very likely that one of the more complex preview providers is responsible for that.

It would be awesome if you could pinpoint the exact file whose previews are causing unwanted files on /tmp. Otherwise, it would be very helpful if you could provide file names of those rogue temp files on /tmp.

@ernolf
Copy link
Author

ernolf commented Mar 18, 2023

Yesterday I have tried it again:
that is how it looks like, much of those directories are empty but not deleted.

image

Here two randomly selected ones more thoroughly and in detail:

~#: ls -lR /tmp/lu25273*.tmp
...
...
...
/tmp/lu25273mfl.tmp:
insgesamt 16
-rw-r--r-- 1 www-data www-data 14810 Mär 18 02:43 lu25273mfm.tmp
drwx------ 2 www-data www-data   100 Mär 18 02:43 lu25273mfn.tmp

/tmp/lu25273mfl.tmp/lu25273mfn.tmp:
insgesamt 68
-rw------- 1 www-data www-data 28672 Mär 18 02:43 cert9.db
-rw------- 1 www-data www-data 36864 Mär 18 02:43 key4.db
-rw------- 1 www-data www-data   454 Mär 18 02:43 pkcs11.txt
...
...
/tmp/lu25273mfr.tmp:
insgesamt 1620
-rw------- 1 www-data www-data       0 Mär 18 02:44 lu25273mfs.tmp
drwx------ 2 www-data www-data     100 Mär 18 02:44 lu25273mft.tmp
-rw-r--r-- 1 www-data www-data 1656352 Mär 18 02:44 lu25273mfu.tmp

/tmp/lu25273mfr.tmp/lu25273mft.tmp:
insgesamt 68
-rw------- 1 www-data www-data 28672 Mär 18 02:44 cert9.db
-rw------- 1 www-data www-data 36864 Mär 18 02:44 key4.db
-rw------- 1 www-data www-data   454 Mär 18 02:44 pkcs11.txt

Analizing those files:

:~# file -b --mime-type /tmp/lu25273mfl.tmp/lu25273mfm.tmp
application/vnd.oasis.opendocument.presentation

:~# file -b --mime-type /tmp/lu25273mfr.tmp/lu25273mfu.tmp
application/pdf

The strange thing is that for security reasons I disabled preview files to be created for document files. I removed them from the 'enabledPreviewProviders' array.
What could make the diference though, is, that I have this forgotten "zombie" lines in my config.php:

  'preview_libreoffice_path' => '/usr/bin/libreoffice',
  'preview_office_cl_parameters' => ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ',

@ernolf
Copy link
Author

ernolf commented Mar 19, 2023

Now I did this:
I changed my config/preview.config.php to:

<?php
$CONFIG = array (
//  'preview_libreoffice_path' => '/usr/bin/libreoffice',
//  'preview_office_cl_parameters' => ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ',
  'enabledPreviewProviders' => 
  'enabledPreviewProviders' => 
  array (
    0 => 'OC\\Preview\\PNG',
    1 => 'OC\\Preview\\JPEG',
    2 => 'OC\\Preview\\GIF',
    3 => 'OC\\Preview\\HEIC',
    4 => 'OC\\Preview\\BMP',
    5 => 'OC\\Preview\\XBitmap',
    6 => 'OC\\Preview\\Krita',
    7 => 'OC\\Preview\\WebP',
    8 => 'OC\\Preview\\SVG',
    9 => 'OC\\Preview\\TIFF',
    10 => 'OC\\Preview\\Photoshop',
    11=> 'OC\\Preview\\MP3',
    12 => 'OC\\Preview\\Movie',
  ),
  'preview_max_x' => 512,
  'preview_max_y' => 512,
);

Then I removed all previews as described in README.md under "I want to reset/regenerate all previews"

and started a fresh per user ./occ preview:generate-all, starting with a user which only have photos and some pdf and office files (from firstrunwizard/skeletondirectory). And again, while scanning those pdf- and office files, the same tmp-directory structures with exactly the same directory name pattern where created:

/tmp/lu[a-z0-9]{6,12}\.tmp

and again not deleted after processing.

Since I have some user accounts with thousends of pdf-files, partially on shared directories, causing the previews to be rescanned for each user with access to those directories, from their account's path selves, this results in a bloated /tmp directory which is (in my case) mounted on tmpfs, ending up in a frozen server.

As a workaround I wrote this wrapper script that cleans up everything older than 10 minutes.
It must be invoked as the webserver user and iterates through all available users. You can skip single users or give it the argument "-n | --no-interaction" to do the scan on all users one by one without questions. Furthermore you can give -q | --quiet | --nolog, -v, -vv, -vvv, --verbose as arguments. The output is into a logfile, since I allways run those never ending scripts (the last full scan took multiple days) in a screen session:

/usr/local/nc-preview_generate-all

#!/bin/bash
# change path and webserver-user accordingly:
nc_dir="/var/www/nextcloud"
log_dir="/var/log/nextcloud"
ht_user="www-data"

occ_call="php -f $nc_dir/occ"
if [ "`id -un`" != "$ht_user" ]; then
    echo "This script mus be run as the webserver user \"$ht_user\""
    exit 1
fi
cd $nc_dir

logpath="$log_dir/preview-generate-all_"

ask=true
nolog=false
v_flag="-v"
while (( "$#" )); do
    case "$1" in
        -n|--no-interaction)
            ask=false
            shift
            ;;
        -vvv|-vv|-v|--verbose)
            v_flag="$1"
            shift
            ;;
        -q|--quiet|--nolog)
            nolog=true
            shift
            ;;
        *)  echo "unknown argument $1"
            exit 1
            ;;
    esac
done

# define trap
kill_background_job() {
    echo -e "\nReceived INT signal (^C)"
    if [ -n "$BACKGROUND_PID" ]; then
        if kill -0 "$BACKGROUND_PID" >/dev/null 2>&1; then
            echo "Killing background job with PID $BACKGROUND_PID..."
            kill -9 "$BACKGROUND_PID"
        fi
    fi
    wait
    exit 1
}

cleanup() {
    if find /tmp -maxdepth 1 -user $ht_user -type d -regex ".*/lu[a-z0-9]+\.tmp$" 2>/dev/null | grep -q . ; then
        rm -fr /tmp/lu[a-z0-9]*.tmp
        echo "temporary files removed by trap"
    fi
}

trap "cleanup" EXIT
trap 'kill_background_job' INT

# Define the function to keep the temp directory clean
keep_tmp_clean() {
    PROCESS_PID=$1
    while ps -p $PROCESS_PID > /dev/null
    do
        find /tmp -maxdepth 1 -user $ht_user -type d -regex ".*/lu[a-z0-9]+\.tmp$" -mmin +10 -exec rm -rf {} \;
        sleep 60
    done

    # Once the process is done, delete the remaining temp directories
    rm -fr /tmp/lu[a-z0-9]*.tmp
}

# We need to take the detour via base64 to allow usernames with spaces
for user in $(echo $($occ_call user:list --output=json | jq -r 'keys_unsorted[] | @base64')); do
    user="$(echo "$user" | base64 --decode)"
    if $ask; then
        echo
        read -rp "Press ENTER to generate preview images for user \"$user\", or 's' to skip, or ^C to cancel: " input
        if [[ "$input" =~ ^[sS]$ ]]; then
            echo "Skipping user \"$user\""
            continue
        fi
    fi
    if $nolog; then
        echo "Processing user = \"$user\""
        $occ_call preview:generate-all --no-interaction "$user" &
        BACKGROUND_PID=$!
    else
        echo -e "User    = \"$user\"\nLogfile = \"$logpath$user.log\""
        $occ_call preview:generate-all --no-interaction "$v_flag" "$user" >> "$logpath$user.log" &
        BACKGROUND_PID=$!
    fi
    keep_tmp_clean $BACKGROUND_PID
    echo "Previews for user \"$user\" generated"
done

# Wait for all background processes to finish
wait
exit 0

@joshtrichards
Copy link
Member

/tmp/lu*.tmp files are from LibreOffice.

Do you have richdocuments (Nextcloud Office) installed?

Nextcloud Office registers its own preview providers. See nextcloud/richdocuments#1335

Related issues in this repo: #410, #419

This will need to get addressed in richdocuments / Nextcloud Office. So go upvote nextcloud/richdocuments#1335 I guess. ;-)

@joshtrichards joshtrichards closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants