P
P
Pavel2021-12-09 17:00:22
Laravel
Pavel, 2021-12-09 17:00:22

Why are some queue jobs in Laravel repeated twice?

I am using job chaining and sometimes I find that the jobs are running again. Here is an example of my code:

<?php

namespace App\Http\Controllers;

use App\Jobs\SendInvoice;
use Illuminate\Http\Request;
use JustIversen\JobChainer\JobChainer;

class NotificationsController extends Controller
{
    public function bulk_sending_by_type(Request $request)
    {
        $numbers = $request->numbers;
        $chain = new JobChainer;
        foreach ($numbers as $key => $number){
            $chain->add(SendInvoice::class, $number);
        }

        $chain->dispatch();
        return response()->json([]);
    }
}

Job file
<?php

namespace App\Jobs;

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

class SendInvoice implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $number;
    public $queue;
    public $timeout = 500;

    public function __construct($number )
    {
        $this->queue = 'invoices';
        $this->number = $number;
    }

    public function handle()
    {
        InvoicesHelper::send_invoice($this->number);
        sleep(30);
    }
}

config
'connections' => [

    'sync' => [
        'driver' => 'sync',
    ],

    'invoices' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'invoices',
        'retry_after' => 520,
    ],
],

I am using supervisor to manage the queue.
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/sites/test.fo/public/artisan queue:work --queue=invoices --tries=1
autostart=true
autorestart=true
user=main
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/sites/test.fo/public/worker.log
stopwaitsecs=9000

I set the number of attempts with tries, but it doesn't work. I also added --tries to the command.
And as stated in the documentation, in order not to run tasks twice, the timeout must be less than the retry_after parameter, which I did. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2021-12-11
@jazzus

The number of attempts is not guaranteed. the user can press the button 10 times and there will be 1 attempt everywhere. try adding uniqueness with invoice number https://laravel.com/docs/8.x/queues#unique-jobs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question