Closed
Description
Laravel Version
10.4.1
PHP Version
8.2
Database Driver & Version
No response
Description
I am using Job Queue to handle checkout request but it's not working as expected.
When I inject a dependency in the handle
method, job failed again and again. Sometimes jobs are silently failed.
Dispatching Job
dispatch(new CheckOutJob($event, $ticket));
CheckOutJob.php
<?php
namespace App\Jobs;
use App\Actions\CheckOutOrderAction;
use App\Enums\TicketStatus;
use App\Models\Event;
use App\Models\Order;
use App\Models\Ticket;
use App\Services\WalletService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CheckOutJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(
public readonly Event $event,
public readonly Ticket $ticket,
)
{
//
}
/**
* Execute the job.
* @throws \Exception
*/
public function handle(CheckOutOrderAction $checkOutOrderAction): void
{
$checkOutOrderAction->handle($this->event, $this->ticket);
}
}
CheckOutAction.php
<?php
namespace App\Actions;
use App\Models\Event;
use App\Models\Ticket;
use App\Models\User;
use App\Services\WalletService;
use Junges\Kafka\Facades\Kafka;
use Junges\Kafka\Message\Message;
class CheckOutOrderAction
{
public function __construct(
public readonly WalletService $walletService
)
{
}
/**
* @throws \Exception
*/
public function handle(Event $event, Ticket $ticket): void
{
$authUserWalletId = $this->walletService->getMyWallet()['wallet_id'];
$eventOwnerWalletId = $this->walletService->getMyWallet($event->user)['wallet_id'];
$ticketPrice = $ticket->ticketType->price;
$message = new Message(body: createKafkaPayload(
topic: 'wallets',
pattern: 'wallets.transfer',
data: [
'amount' => $ticketPrice,
'from_wallet_id' => $authUserWalletId,
'to_wallet_id' => $eventOwnerWalletId,
],
));
Kafka::publishOn('wallets')
->withMessage($message)
->send();
}
}
WalletService.php
<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Facades\Http;
class WalletService
{
public function getMyWallet(?User $user = null)
{
try {
$response = Http::wallet()
->async()
->post('/wallets', [
'user_id' => $user->id ?? auth()->id(),
])
->then(fn($response) => $response->json());
$wallets = $response->wait();
return $wallets['user']['wallets'][0];
} catch (\Exception $exception) {
info('[ERROR]' . $exception->getMessage());
abort(500, 'Internal Server Error');
}
}
}
Job Failed Error in Horizon
App\Jobs\CheckOutJob
RetryID
393eadd4-7fd0-4558-b456-d366448e10c3
Queue
default
Attempts
2
Retries
0
Tags
App\Models\Event:31, App\Models\Ticket:3
Pushed
2024-01-31 06:25:12
Failed
2024-01-31 06:25:14
Exception
Illuminate\Queue\MaxAttemptsExceededException: App\Jobs\CheckOutJob has been attempted too many times. in /var/www/gateway/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:785
--
Stack trace:
#0 /var/www/gateway/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(519): Illuminate\Queue\Worker->maxAttemptsExceededException(Object(Illuminate\Queue\Jobs\RedisJob))
#1 /var/www/gateway/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(428): Illuminate\Queue\Worker->markJobAsFailedIfAlreadyExceedsMaxAttempts('redis', Object(Illuminate\Queue\Jobs\RedisJob), 1)
#2 /var/www/gateway/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(389): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
Show All
Exception Context
null
Data
{
"event":{
"class":"App\Models\Event",
"id":31,
"relations":[
],
"connection":"mysql",
"collectionClass":null
},
"ticket":{
"class":"App\Models\Ticket",
"id":3,
"relations":[
],
"connection":"mysql",
"collectionClass":null
}
}
Steps To Reproduce
php artisan serve
php artisan horizon
Metadata
Metadata
Assignees
Labels
No labels