<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mitul Lakhani</title>
    <description>The latest articles on DEV Community by Mitul Lakhani (@mitulmlakhani).</description>
    <link>https://dev.to/mitulmlakhani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F708621%2F55c1e657-e87b-48b4-8044-d09333d36f0b.jpg</url>
      <title>DEV Community: Mitul Lakhani</title>
      <link>https://dev.to/mitulmlakhani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mitulmlakhani"/>
    <language>en</language>
    <item>
      <title>Laravel Dispatch Queue Job with Delay and Get Job ID</title>
      <dc:creator>Mitul Lakhani</dc:creator>
      <pubDate>Sat, 18 Sep 2021 17:01:10 +0000</pubDate>
      <link>https://dev.to/mitulmlakhani/laravel-delay-dispatch-queue-job-and-get-job-id-36ke</link>
      <guid>https://dev.to/mitulmlakhani/laravel-delay-dispatch-queue-job-and-get-job-id-36ke</guid>
      <description>&lt;p&gt;With Laravel when we dispatch Queue Job asynchronously, the job is added to the queue then we have no control over it anymore. So I find a way to take control of the job, to either remove or process jobs from the queue.&lt;/p&gt;

&lt;p&gt;Let's Start!&lt;/p&gt;

&lt;p&gt;Create a new helper for dispatch queue job and get Job ID&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!function_exists("custom_dispatch")) {
    function custom_dispatch($job): int {
        return app(\Illuminate\Contracts\Bus\Dispatcher::class)-&amp;gt;dispatch($job);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Now let's use "custom_dispatch" helper.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new controller to dispatch the job and handle the job.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:controller JobHandlerController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's use our helper and implement the controller.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace  App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class JobHandlerController extends  Controller
{
    public function dispatchJob(): int
    {
        // Create your job instance with delay, so we can back here within delay and take control in our hands.
        $job = new App\Jobs\YourAwesomeJob($params)-&amp;gt;delay(now()-&amp;gt;addSeconds(60));

        // Dispath your job with our custom_dispatch helper. This will return job id from jobs table
        $jobId = custom_dispatch($job);

        return $jobId;
    }

    public function processJob(int $jobId, string $action): bool
    {
        // Here deleting job from the jobs table
        if($action === "delete") {
            DB::table('jobs')-&amp;gt;whereIn('id', $args['jobIds'])-&amp;gt;delete();
        }

        // Here update available_at field with the current timestamp, So now the queue worker will process the job immediately. 
        DB::table('jobs')-&amp;gt;where('id', $jobIds)-&amp;gt;update(['available_at' =&amp;gt; time()]);

        return true;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: This thing is useful when the queue driver is not "sync".&lt;/p&gt;

&lt;p&gt;That's it!&lt;/p&gt;

&lt;p&gt;Hope you find it useful.&lt;/p&gt;

</description>
      <category>laravel</category>
    </item>
  </channel>
</rss>
