can you ignore the job when model are missing with public $deleteWhenMissingModels = true;? don't know other option.
Oct 13, 2022
10
Level 1
Laravel Queue Job - No query results for model (After Commit = true)
Hey community,
recently, sometimes jobs (SerializedModels) failed with the Exception:
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [..]
Controller:
$model = new Model();
$model->save();
// Log::warning("Before dispatching", $model->toArray()); // Model displayed correctly, (id set)
Job::dispatch($model); // Connection: Redis - Queue: redis
// Log::warning("After dispatching", $model->toArray()); // Model displayed correctly, (id set)
Job:
class Job implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private Model $model;
public function __construct(Model $model)
{
$this->model = $model;
}
public function handle()
{
// Exception thrown before code running here
}
Setup:
- Queue is running by horizon with after_commit =true;
- Galera Cluster (Three Nodes: For Testing: Controller + Queue uses the same node)
- Queue Driver / Connection: REDIS
- Multiple App Servers
- Dedicated Queue Server
- Dedicated Redis Server
- In this case, there is no transaction or dispatched job inside the transaction
- The model was successfully saved before dispatching the job
if job retries are enabled, (2) the job ran successfully after a second attempt (Race Condition confirmed)
For sure, an option could be to remove the Serialized Model Trait, but this could not be the solution.
Any ideas why the queue job worker can't fetch the model, but a second attempt works as expected ? (Model Save runs before, After Commit enabled)
PHP Artisan About:
php artisan about
Environment ................................
Application Name ................... laravel
Laravel Version ..................... 9.35.1
PHP Version ......................... 8.1.11
Composer Version .................... 2.1.12
Environment ..................... production
Debug Mode ............................. OFF
Maintenance Mode ....................... OFF
Cache ....
Config .............................. CACHED
Events .............................. CACHED
Routes .............................. CACHED
Views ........................... NOT CACHED
Drivers ..
Broadcasting ........................ pusher
Cache ................................ redis
Database ............................. mysql
Logs .................. stack / daily, slack
Mail .................................. smtp
Queue ................................ redis
Session .............................. redis
Please or to participate in this conversation.