Framework: Queued Job with Deleted Model

Created on 29 Aug 2016  路  5Comments  路  Source: laravel/framework

Imagine the situation. I have a delayed job to do something with the post model, but before the job process I delete the post.

Today I use the trait indicated in this discuss.

But now I'm thinking, this kind of exception should be treated in another place, for example in the failed method.

What do you think about this @taylorotwell and @GrahamCampbell?

namespace App\Jobs;

use App\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\{InteractsWithQueue, SerializesModels};
use Illuminate\Contracts\Queue\ShouldQueue;

class Example implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    public function handle()
    {
        Log::debug($post->title);
    }

    // The idea sketch
    public function foo(Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            // do something
        }
    }
}

Most helpful comment

That's what I do for sending all my mail for example. Makes it possible to send account deletion emails!

All 5 comments

There is another issue with a similar problem #9347, but with soft deletes.

Yeh, this is unfortunate, but is the expected behaviour.

If in your queue jobs you don't actually need to interact with the model, and just needed to read some properties, I'd suggest just passing through those properties instead of a model.

That's what I do for sending all my mail for example. Makes it possible to send account deletion emails!

Thanks @GrahamCampbell.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

kerbylav picture kerbylav  路  3Comments

fideloper picture fideloper  路  3Comments

PhiloNL picture PhiloNL  路  3Comments