getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.23k stars 166 forks source link

Image thumbs: persisting jobs files #6511

Open nilshoerrmann opened 6 days ago

nilshoerrmann commented 6 days ago

Description

On a website with many images we noticed a lot of image job files persisted even after the thumbnails had been created.

Expected behavior
Jobs should be removed after processing.

Screenshots

grafik

Your setup

Kirby Version
Kirby 4.2

afbora commented 6 days ago

I've done few tests but I couldn't reproduce the issue. May be race condition? I think, it could be reproducible with multiple concurrent same requests.

nilshoerrmann commented 6 days ago

I've noticed this on different servers (production and localhost) and with different image sizes (small and large). We have this custom code to generate small image thumbs we use as placeholders:

    public function previewUri()
    {
        try {
            [$width, $height] = $this->dimensions();
            $height = round((65 / $width) * $height);
            $width = 65;

            if ($this->crop) {
                $options = null;

                if ($this->crop !== 'focus') {
                    $options = [
                        'crop' => $this->crop
                    ];
                }

                $thumb = $this->file->crop($width, $height, $options);
            } else {
                $thumb = $this->file->thumb([
                    'width' => $width,
                    'height' => $height
                ]);
            }

            if (!$thumb->exists()) {
                $thumb->save();
            }

            return F::uri($thumb->root());
        } catch (Exception $e) {
            return '';
        }
    }

Not sure if this might be a source of this issue, too. Still, the persistent job files do not only affect these thumbs but other images as well.

nilshoerrmann commented 5 days ago

I also noticed that sometimes there are just unchanged copies of the original image in the media folder (same file size, same dimensions):

grafik

We are also seeing high CPU usage and failures. Although we are using GD for image creation, this might be related to #5328.

afbora commented 3 days ago

I also noticed that sometimes there are just unchanged copies of the original image in the media folder (same file size, same dimensions):

I'm pretty sure that you already know this, but I think you may have overlooked it. This behavior is possible if the original image is used in the content or a original url is provided for the lightbox (as in the example below). Of course, I'm making an assumption since I don't know your case. In the example below, the original image is not copied to the media folder unless the lightbox is running to enlarge the image.

<a href="<?= $cover->url() ?>" data-lightbox class="img">
  <img src="<?= $cover->crop(1200, 600)->url() ?>" alt="<?= $cover->alt()->esc() ?>">
</a>
nilshoerrmann commented 2 days ago

@afbora You are right about the directly linked sources (and you are totally right, that I overlooked that, too). So this is just fine 👍

We are still left with the persisting job files. My guess is something did not work as expected while generating thumbs. Let this either be a memory or CPU limit or some kind of other fault on the server. So do coexisting job and thumb files imply that something is wrong with the generated thumb? Let's say the thumb is only partly rendered. This is an important question because if this is the case, the thumb should be regenerated on next call, discarding the existing thumb. But this could cause more trouble if the server was already at its limits forcing recursive image generations. If – on the other hand – an existing thumb predicates that the image generation finished correctly (because it was only moved from tmp to the media folder after generation, I don't know), then the persisting job file could savely be removed on second call.

The question is which is right and which is wrong: the existing job or image files?