I have an issue that periodically appears due to the launch of the job class in the command on the Laravel Framework 10.48.10 project.
Exception text:
"SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (Connection: mysql, SQL: delete from jobs where id = 10164)"
"Exception
{
"class": "Illuminate\Database\QueryException",
"message": "SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (Connection: mysql, SQL: delete from `jobs` where `id` = 10164)",
"code": 40001,
"file": "/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:829",
"trace": [
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:783",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:619",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:564",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3756",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php:357",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:30",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php:359",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Jobs/DatabaseJob.php:66",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php:82",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php:102",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:439",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:389",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:176",
"
I also have a slightly different error text, but it is also error 1213 Deadlock:
SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (Connection: mysql, SQL: select * from jobs where queue = default and ((reserved_at is null and available_at <= 1722524426) or (reserved_at <= 1722524336)) order by id asc limit 1 for update)
Exception
```{
"class": "Illuminate\Database\QueryException",
"message": "SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (Connection: mysql, SQL: select * from jobs where queue = default and ((reserved_at is null and available_at <= 1722524426) or (reserved_at <= 1722524336)) order by id asc limit 1 for update)",
"code": 40001,
"file": "/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:829",
"trace": [
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:783",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:431",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2872",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2860",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3414",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2861",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php:333",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php:246",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php:224",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:30",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php:227",
"/var/www/vhosts/index.parsoft.io/httpdocs/vendor/laravel/`…
I have a first command GoogleIndexSearchedCarsCommand with job.
Launching in the Kernel.php:
$schedule->command('app:google-index-searched-cars-command')->hourly()->between('10:00', '23:59')->withoutOverlapping();
Inside the command GoogleIndexSearchedCarsCommand I have this code where I run the job:
<?php
namespace AppConsoleCommands;
use AppJobsGoogleIndexSearchedCarsJob;
use AppServicesResourceLibraryHiddenCarResourceLibraryHiddenCarService;
use IlluminateConsoleCommand;
class GoogleIndexSearchedCarsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:google-index-searched-cars-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* @var ResourceLibraryHiddenCarService
*/
private ResourceLibraryHiddenCarService $resourceLibraryHiddenCarService;
public function __construct()
{
parent::__construct();
$this->resourceLibraryHiddenCarService = app(ResourceLibraryHiddenCarService::class);
}
/**
* Execute the console command.
*/
public function handle(): void
{
$this->googleIndexSearchedCars();
}
private function googleIndexSearchedCars(): void
{
$resources = $this->resourceLibraryHiddenCarService->getResourcesForGoogleIndexingCars(typeIndexing: 'searched');
if ($resources->isNotEmpty()) {
foreach ($resources as $resource) {
GoogleIndexSearchedCarsJob::dispatch($resource);
}
}
}
}
I also have a second similar command GoogleIndexHiddenCarsCommand with job.
Launching in the Kernel.php:
$schedule->command('app:google-index-hidden-cars-command')->hourlyAt(30)->between('10:00', '23:59')->withoutOverlapping();
Inside this command this code:
<?php
namespace AppConsoleCommands;
use AppJobsGoogleIndexHiddenCarsJob;
use AppServicesResourceLibraryHiddenCarResourceLibraryHiddenCarService;
use IlluminateConsoleCommand;
class GoogleIndexHiddenCarsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:google-index-hidden-cars-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* @var ResourceLibraryHiddenCarService
*/
private ResourceLibraryHiddenCarService $resourceLibraryHiddenCarService;
public function __construct()
{
parent::__construct();
$this->resourceLibraryHiddenCarService = app(ResourceLibraryHiddenCarService::class);
}
/**
* Execute the console command.
*/
public function handle(): void
{
$this->googleIndexHiddenCars();
}
private function googleIndexHiddenCars(): void
{
$resources = $this->resourceLibraryHiddenCarService->getResourcesForGoogleIndexingCars(typeIndexing: 'hidden');
if ($resources->isNotEmpty()) {
foreach ($resources as $resource) {
GoogleIndexHiddenCarsJob::dispatch($resource);
}
}
}
}
What is the reason for the Deadlock error? How to fix it? Please help.