Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job has been attempted too many times with Horizon in development mode #49920

Closed
theaungmyatmoe opened this issue Jan 31, 2024 · 1 comment
Closed

Comments

@theaungmyatmoe
Copy link

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

Job Failed Error

App\Jobs\CheckOutJob

 Retry
ID
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
}
}
App\Jobs\CheckOutJob ID 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](http://localhost:8001/horizon/failed/*) 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
@driesvints
Copy link
Member

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants