title: Laravel: How to programmatically run job for 5 times if failed?
body: <p>This is the code for job and I need to run the job after 5 seconds for only 5 times if it fails. Before diving into code, the <code>$this-&gt;attempts()</code> always return 1 so I didn't include it in code.</p>
  <pre><code>&lt;?php
  
  namespace Modules\Order\Jobs;
  
  use Illuminate\Bus\Queueable;
  use Illuminate\Queue\SerializesModels;
  use Illuminate\Queue\InteractsWithQueue;
  use Illuminate\Contracts\Queue\ShouldQueue;
  use Illuminate\Foundation\Bus\Dispatchable;
  use Modules\Order\Entities\Order;
  use Modules\User\Entities\User;
  
  class CreditPurchase implements ShouldQueue
  {
      use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  
      /**
       * Create a new job instance.
       *
       * @return void
       */
      private $order;
      private $user;
      private $paid_at;
  
      public function __construct(Order $order, User $user, $paid_at)
      {
          $this-&gt;order = $order;
          $this-&gt;user = $user;
          $this-&gt;paid_at = $paid_at;
      }
  
  
      /**
       * Execute the job.
       *
       * @return void
       */
      public function handle()
      {
          try {
              $t = 4 / 0;
  
          } catch (\Throwable $exception) {
              $this-&gt;fail();
              $this-&gt;delete();
              $this-&gt;release(5);
          }
      }
  }
  </code></pre>
  <p>But I get this error:</p>
  <blockquote>
  <p>[2021-01-20 23:16:09] local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '014c3080-3ee3-4198-946c-dfe1d8d858a7' for key 'failed_jobs_uuid_unique' (SQL: insert into <code>failed_jobs</code> (<code>uuid</code>, <code>connection</code>, <code>queue</code>, <code>payload</code>, <code>exception</code>, <code>failed_at</code>) values (014c3080-3ee3-4198-946c-dfe1d8d858a7, redis, default, {&quot;uuid&quot;:&quot;014c3080-3ee3-4198-946c-dfe1d8d858a7&quot;,&quot;timeout&quot;:null,&quot;id&quot;:&quot;HDww70Nm2TmRcmeO5cqXN8qrxbvzBfDq&quot;,&quot;backoff&quot;:null,&quot;displayName&quot;:&quot;Modules\Order\Jobs\CreditPurchase&quot;,&quot;maxTries&quot;:null,&quot;maxExceptions&quot;:null,&quot;retryUntil&quot;:null,&quot;data&quot;:{&quot;command&quot;:&quot;O:33:&quot;Modules\Order\Jobs\CreditPurchase&quot;:13:{s:40:&quot;\u0000Modules\Order\Jobs\CreditPurchase\u0000order&quot;;O:45:&quot;Illuminate\Contracts\Database\ModelIdentifier&quot;:4:{s:5:&quot;class&quot;;s:28:&quot;Modules\Order\Entities\Order&quot;;s:2:&quot;id&quot;;i:1;s:9:&quot;relations&quot;;a:1:{i:0;s:5:&quot;items&quot;;}s:10:&quot;connection&quot;;s:5:&quot;mysql&quot;;}s:39:&quot;\u0000Modules\Order\Jobs\CreditPurchase\u0000user&quot;;O:45:&quot;Illuminate\Contracts\Database\ModelIdentifier&quot;:4:{s:5:&quot;class&quot;;s:26:&quot;Modules\User\Entities\User&quot;;s:2:&quot;id&quot;;i:1;s:9:&quot;relations&quot;;a:0:{}s:10:&quot;connection&quot;;s:5:&quot;mysql&quot;;}s:42:&quot;\u0000Modules\Order\Jobs\CreditPurchase\u0000paid_at&quot;;O:13:&quot;Carbon\Carbon&quot;:3:{s:4:&quot;date&quot;;s:26:&quot;2021-01-20 23:16:00.344324&quot;;s:13:&quot;timezone_type&quot;;i:3;s:8:&quot;timezone&quot;;s:3:&quot;UTC&quot;;}s:3:&quot;job&quot;;N;s:10:&quot;connection&quot;;N;s:5:&quot;queue&quot;;N;s:15:&quot;chainConnection&quot;;N;s:10:&quot;chainQueue&quot;;N;s:19:&quot;chainCatchCallbacks&quot;;N;s:5:&quot;delay&quot;;i:2;s:11:&quot;afterCommit&quot;;N;s:10:&quot;middleware&quot;;a:0:{}s:7:&quot;chained&quot;;a:0:{}}&quot;,&quot;commandName&quot;:&quot;Modules\Order\Jobs\CreditPurchase&quot;},&quot;job&quot;:&quot;Illuminate\Queue\CallQueuedHandler@call&quot;,&quot;attempts&quot;:1}, Illuminate\Queue\MaxAttemptsExceededException: Modules\Order\Jobs\CreditPurchase has been attempted too many times or run too long. The job may have previously timed out. in C:\xampp\htdocs\avakala\vendor\laravel\framework\src\Illuminate\Queue\Worker.php:713</p>
  </blockquote>