Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

simonw's avatar

Observer not triggering from a running job.

Hi all, I have a model. class Post, and when I use Post::create([]) I have an observer successfully triggering it's created method.

I have the Observer set up in the AppServiceProvider boot() method. \App\Models\Post::observe(\App\Observers\PostObserver::class);

This has been working for over a year now so no worries in that. However I decided to move the creation of said Post to a job. So now php artisan queue:work --queue=myqueue is running, and I can fire off the job and watch it spit out debugs in the command line.

*I can see my Post is being created in the database, but for some reason the Observer is not triggering. Literally nothing has changed in the set up of the Observer, merely the location of where I create the job.

Has anybody encountered this before?

0 likes
4 replies
bobbybouwmann's avatar

This is very odd, because the process for creating a model is still the same here. Can you show us how you store the post to the database in your listener?

simonw's avatar

Of course. So this is my Observer:

public function created(Post $post)
{
    dd('should run this');
}

and this is the Job:

public function handle(PostService $service) {
   ...
    var_dump('saving post to database...');
   $service->post(title, description, x, y, z);
}

and Service:

public static function post(title, description, x, y, z) {
    ...
    $post = Post::create([
                    'title' => $title,
                    'description' => $description,
                    'x' => $x,
                    'y' => $y,
        ]);
}

output from bash:

Processing: App\Jobs\Broker\Post
/var/www/app/Jobs/Broker/Post.php:119:
string(45) "saving post to database..."

And checking the database, I do indeed see the post I just created. new ID, created_at timestamp so I'm sure it's not an old one i'm looking at. but the dd() never runs. :(

Snapey's avatar

do you see

Processed: App\Jobs\Broker\Post

Any errors in the log file?

simonw's avatar

No errors that I could see, and it does say Processed. However have since found this issue. I WAS killing the worker each time to ensure that the file rebuilt (any changes to a running worker will not be present until you restart the job queue). My docker container from the outside was telling me I had no running worker. By running docker top my_container I could see my worker disappearing when I ran artisan queue:restart.

However, when I logged directly in to the container using docker exec -it my_container bash it seemed it had left a rogue worker running in the background. I was unable to kill it, so I had to rebuild the container to stop it.

Once I had done that, the changes started showing, and the file started var dumping when expected.

Lessons to be learnt? If in doubt, take off and nuke the site from orbit.

Please or to participate in this conversation.