How to create a job Queue in Laravel Framework

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2016/12/create-job-queue-laravel-framework/

In computer science, a queue  is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.

Queue is very important and interesting features of Lumen,You can run your tasks(background process/cron tasks) using queue without fail.You pushed task into queue and rest of work will do by worker.Sometime we need to run a task one by one or partially which will process some data and return final processed data as a response.You can use queue in Lumen for periodically running tasks,cron tasks and background scripts tasks.

Normally we are using CRON job to call task periodically but some time that task failed due execution time,server response time 500 or any other server issue,next time that task will run on scheduled time, again and again that process will continue but failed process will not retry if scheduled task was failed.

I am using following module for Lumen queue

  • Beanstalk : Beanstalk is a simple, fast work queue.It was designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.

I will use following file:

  • Composer : I will use this file to add beanstalkd for install as a dependency module
  • Config/Queue.php : This file will use for configuration of Queue and hist driver.
  • Controller/sampleController.php : This controller file will use for sent task to jobQueue
  • Jobs/testQueue.php : This jobs file will responsible to push task into queue using method.

Now run update composer from command line,
D:\lumen_code> composer update

Step 2: Added Queue configuration in config/queue.php file, If you do not have cache folder then you need create Config folder in root of limen application(ex. – lumen_code\Config) and copy queue.phpfile from vendor\laravel\lumen-framework\config and paste into lumen_code/config folder, so new file location would be lumen_code/config/queue.php. You need update queue.php as like below configuration.

1
2
3
4
5
6
7
8
<span class="php"><span class="php-string">'default'</span> <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-string">'beanstalkd'</span>,

<span class="php-string">'beanstalkd'</span> <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-brackets">[</span>
            <span class="php-string">'driver'</span> <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-string">'beanstalkd'</span>,
            <span class="php-string">'host'</span>   <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-string">'127.0.0.1'</span>,
            <span class="php-string">'queue'</span>  <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-string">'default'</span>,
            <span class="php-string">'ttr'</span>    <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-number">3</span><span class="php-number">6</span><span class="php-number">0</span><span class="php-number">0</span>,
        <span class="php-brackets">]</span>,</span>

We have changed default queue driver to beanstalkd queue driver and updated beanstalkd driver configuration.

Step 3: Now I have created a new jobs file testQueue.php in jobs/ folder.We need to add below code into testQueue.php file,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<span class="php"><span class="php-script-tag">&lt;?php</span>

<span class="php-keyword">namespace</span> App\Jobs;

<span class="php-keyword">use</span> App\Jobs\Job;
<span class="php-keyword">use</span> Log;
<span class="php-keyword">use</span> Illuminate\Http\Request;

<span class="php-keyword">class</span> testQueue <span class="php-keyword">extends</span> Job
<span class="php-brackets">{</span>
    <span class="php-keyword">public</span> <span class="php-var">$queue</span>;

    <span class="php-comment">/**
     * Create a new job instance.
     *
     * @return void
     */</span>
    <span class="php-keyword">public</span> <span class="php-function">function</span> __construct<span class="php-brackets">(</span><span class="php-var">$data</span><span class="php-brackets">)</span>
    <span class="php-brackets">{</span>
        <span class="php-var">$this</span><span class="php-operator">-</span><span class="php-operator">&gt;</span>queue <span class="php-operator">=</span> <span class="php-var">$data</span><span class="php-brackets">[</span><span class="php-string">'queue'</span><span class="php-brackets">]</span>;
    <span class="php-brackets">}</span>
    <span class="php-comment">/**
     * Execute the job.
     *
     * @param  
     * @return void
     */</span>

    <span class="php-keyword">public</span> <span class="php-function">function</span> handle<span class="php-brackets">(</span><span class="php-brackets">)</span> <span class="php-brackets">{</span>
      Log<span class="php-operator">:</span><span class="php-operator">:</span>info<span class="php-brackets">(</span><span class="php-string">'Hello! Queue job '</span><span class="php-operator">.</span><span class="php-var">$this</span><span class="php-operator">-</span><span class="php-operator">&gt;</span>queue<span class="php-operator">.</span><span class="php-string">' is run at start time - '</span><span class="php-operator">.</span><span class="php-function">microtime</span><span class="php-brackets">(</span><span class="php-keyword">true</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;
 <span class="php-brackets">}</span>
<span class="php-brackets">}</span></span>

I have created parameterized queue method that will help to run task with parameter.I have just created sample queue method which will print messages into LOG file,You can see into storage/Log/lumen.log file.

Step 4: We have successfully created jobs and created a new method which will push task into queue,I have created test_queue() method into sampleController.php file like below code which will push tasks into queue.

1
2
3
4
5
6
7
8
9
<span class="php"><span class="php-keyword">use</span> Illuminate\Support\Facades\Queue;
<span class="php-keyword">use</span> App\Jobs\testQueue;

<span class="php-keyword">public</span> <span class="php-function">function</span> test_queue<span class="php-brackets">(</span><span class="php-brackets">)</span> <span class="php-brackets">{</span>
    <span class="php-keyword">for</span><span class="php-brackets">(</span><span class="php-var">$i</span><span class="php-operator">=</span><span class="php-number">0</span>; <span class="php-var">$i</span><span class="php-operator">&lt;</span><span class="php-operator">=</span><span class="php-number">1</span><span class="php-number">0</span>; <span class="php-var">$i</span><span class="php-operator">+</span><span class="php-operator">+</span><span class="php-brackets">)</span><span class="php-brackets">{</span>
        Queue<span class="php-operator">:</span><span class="php-operator">:</span>push<span class="php-brackets">(</span><span class="php-keyword">new</span> testQueue<span class="php-brackets">(</span><span class="php-keyword">array</span><span class="php-brackets">(</span><span class="php-string">'queue'</span> <span class="php-operator">=</span><span class="php-operator">&gt;</span> <span class="php-var">$i</span><span class="php-brackets">)</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;
        <span class="php-keyword">echo</span> <span class="php-string">"successfully push"</span>;
    <span class="php-brackets">}</span>
<span class="php-brackets">}</span></span>

I have included testQueue which is earlier created for queue jobs.

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2016/12/create-job-queue-laravel-framework/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger