<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class EsIndexDocument implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $model;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->model->esIndexDocument();
    }
}

<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Jobs;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;

class EsIndexDocument implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    private array $modelMeta;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($model)
    {
        $this->modelMeta = [
            'class' => get_class($model),
            'id' => $model->getKey(),
        ];
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(): void
    {
        $id = $this->modelMeta['id'];

        if (!is_scalar($id)) {
            log_error(new Exception("can't index models with unsupported primary key: ".json_encode($id)));

            return;
        }

        $class = $this->modelMeta['class'];
        $model = $class::find($id);

        if ($model !== null) {
            $model->esIndexDocument();

            return;
        }

        $model = new $class();
        $keyName = $model->getKeyName();
        $model->setAttribute($keyName, $id);
        $model->esDeleteDocument();
    }
}

Fix handling deleted model for indexing
Fix handling deleted model for indexing
