Laravel-medialibrary: Conversions folder not created and No query results for model

Created on 22 Oct 2018  路  4Comments  路  Source: spatie/laravel-medialibrary

Errors in the dispatch this job, Spatie\MediaLibrary\Jobs\PerformConversions.

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [Spatie\MediaLibrary\Models\Media]. in /var/www/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:414

My queue driver - redis.
I also use Laravel Horizon where I get this error.

The most interesting is that out of 8 images with such an error only 4 are completed, the remaining tasks are successfully completed (褋onversions are created)

My horizon.php config.

'environments' => [
        'production' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue'      => ['default'],
                'balance'    => false,
                'processes'  => 10,
                'tries'      => 3,
            ],
        ],
],

Most helpful comment

Solved a problem.

The problem was that the standard job PerformConversions uses SerializesModels.
Therefore, I did the same job but without SerializesModels.

And redefined it in the configuration in medialibrary.php.

 /*
     * Here you can override the class names of the jobs used by this package. Make sure
     * your custom jobs extend the ones provided by the package.
     */
    'jobs'                     => [
        'perform_conversions'        => App\Jobs\PerformConversions::class,
        // 'perform_conversions'        => Spatie\MediaLibrary\Jobs\PerformConversions::class,
        'generate_responsive_images' => Spatie\MediaLibrary\Jobs\GenerateResponsiveImages::class,
    ],

All 4 comments

Solved a problem.

The problem was that the standard job PerformConversions uses SerializesModels.
Therefore, I did the same job but without SerializesModels.

And redefined it in the configuration in medialibrary.php.

 /*
     * Here you can override the class names of the jobs used by this package. Make sure
     * your custom jobs extend the ones provided by the package.
     */
    'jobs'                     => [
        'perform_conversions'        => App\Jobs\PerformConversions::class,
        // 'perform_conversions'        => Spatie\MediaLibrary\Jobs\PerformConversions::class,
        'generate_responsive_images' => Spatie\MediaLibrary\Jobs\GenerateResponsiveImages::class,
    ],

Hi @batFormat, could you be more specific about what you changed in the PerformConversions job to make it work ?
I have the same problem with the very same configuration but I can't see any SerializesModels in this job.

Sorry, I get it, you mentionned the trait and I was searching a method.
The weird thing is that I have set up the same configuration in local with Laradock and everything is working fine with the redis queue...

I can confirm this error "No query results for model error"

We have an API running with docker, .env set to the following:
MEDIA_LIBRARY_IMAGE_DRIVER=imagick
QUEUE_CONNECTION=database

On any model
```public function registerMediaCollections()
{
$this
->addMediaCollection('avatar')
->singleFile()
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb_small')
->width(64)
->height(64)
->format(Manipulations::FORMAT_PNG);
});
}

And it throws that exception.

When we use `sync` as queue.driver, no errors!

To workaround that problem we followed your advice and extend job classes with custom ones:

'jobs' => [
'perform_conversions' => \App\JobsPerformConversions::class,
'generate_responsive_images' => \App\Jobs\GenerateResponsiveImages::class,
],

Inside these, just removed the SerializeModels Trait and the error is gone

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Spatie\MediaLibrary\Models\Media;
use Illuminate\Queue\InteractsWithQueue;
use Spatie\MediaLibrary\FileManipulator;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\MediaLibrary\Conversion\ConversionCollection;

class PerformConversions implements ShouldQueue
{
use InteractsWithQueue, Queueable;

/** @var \Spatie\MediaLibrary\Conversion\ConversionCollection */
protected $conversions;

/** @var \Spatie\MediaLibrary\Models\Media */
protected $media;

public $tries = 2;

public function __construct(ConversionCollection $conversions, Media $media)
{
    $this->conversions = $conversions;

    $this->media = $media;
}

public function handle(): bool
{
    app(FileManipulator::class)->performConversions($this->conversions, $this->media);

    return true;
}

}

## sidenote
We used the custom job classes for two reasons:

* Workaround the error mentioned above
* Workaround for a second error. Imagine this job fails and it get's automatically restarted. When using database as queuing driver it will throw another exception once it reaches more attempts than 255. This is, as the jobs table have an unsignedTinyInteger column for attempts (As per default).

## composer.json
"laravel/framework": "5.8.*",
"spatie/laravel-medialibrary": "7.6.0",

## running with
* mysql running on docker -> with mysql:8.0.3
* php -v -> PHP 7.2.15 (cli) (built: Feb  9 2019 03:27:34) ( NTS )


## exception as mentioned
From our failed_jobs table

| 4 | database | default | {"displayName":"App\Jobs\PerformConversions","job":"Illuminate\Queue\CallQueuedHandler@call","maxTries":2,"timeout":null,"timeoutAt":null,"......} | Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [Spatie\MediaLibrary\Models\Media]. in /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:452
Stack trace:

0 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Builder->firstOrFail()

1 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(55): App\JobsPerformConversions->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier))

2 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(45): App\JobsPerformConversions->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))

3 [internal function]: App\JobsPerformConversions->__wakeup()

4 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(42): unserialize('O:27:"App\Jobs\...')

5 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(86): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)

6 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(327): Illuminate\Queue\Jobs\Job->fire()

7 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))

8 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))

9 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))

10 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')

11 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()

12 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)

13 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container{closure}()

14 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))

15 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)

16 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array)

17 /usr/src/myapp/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

18 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

19 /usr/src/myapp/vendor/symfony/console/Application.php(908): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

20 /usr/src/myapp/vendor/symfony/console/Application.php(269): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

21 /usr/src/myapp/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

22 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

23 /usr/src/myapp/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

24 /usr/src/myapp/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

# But wait, there is more!
Imagine you performed the first conversion on any model and want to change the image afterwards (perform another conversion) it throws another exception:


2019-04-05 06:17:33] develop.ERROR: unlink(/usr/src/myapp/storage/app/public/29/conversions/IMAGE.png): Permission denied {"userId":"X","exception":"[object] (ErrorException(code: 0): unlink(/usr/src/myapp/storage/app/public/29/conversions/IMAGE.png): Permission denied at /usr/src/myapp/vendor/league/flysystem/src/Adapter/Local.php:421)

EDIT: Forget the "but wait, there is more!
It was our fault, supervisor was running queue with a wrong user. For anyone facing same issue be sure that running supervisor like shown below

[unix_http_server]
...
[supervisord]
...
user=root
...

[program:queue]
...
command=php /usr/src/myapp/artisan queue:work
user=www-data ; MATCH THIS TO YOUR ENV
```

Was this page helpful?
0 / 5 - 0 ratings