Rajiv is an enthusiastic web developer and product developer. He is a Computer Science Engineer from India. He loves PHP and has a deep affection for Laravel. He loves exploring new technologies. In his free time he reads technical articles written for PHP and tries to contribute to the community.

Rajiv's articles

  1. IronMQ and Laravel: Implementation

    This entry is part 2 of 2 in the series IronMQ and Laravel

    Welcome back to the IronMQ and Laravel series – this is the second and final part in which we finalize our background-job enabled web app.

    There are several tutorials out there related to queues already – for example: http://vimeo.com/64703617 where Taylor teaches you how to write to a file using queues. In this tutorial, we will try something different.

    We will make a jobs table which has job_id, and the status of the job. When you put a job on the queue, the job status will be queued and when we receive the job, we set the status to running. Accordingly, after finishing it we will mark it finished.

    Later, we will resize images.

    Jobs Table

    Step 1

    Let’s create a jobs table:

    php artisan migrate:make create_jobs_table
    

    Go to app/database/migrations/xxxxx_create_jobs_table.php and add the following (This code is extracted from app/database/migrations/xxxxx_create_jobs_table.php, so edit your file accordingly):

    public function up()
    {
        Schema::create('jobs', function($table)
    {
        $table->increments('id');
        $table->string('job_id');
        $table->enum('status', array('queued', 'running','finished'))->default('queued');
        $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::drop('jobs');
    }
    

    Here we created a table with the columns job_id, status and timestamps. Run php artisan migrate to create the table.

    Step 2

    Create a file job.php in app/models with the following content:

    <?php
    
    class Job extends Eloquent {
    
        protected $table = 'jobs';
    
        protected $fillable = array('job_id', 'status');
    
    }
    

    Make a JobController using the following command:

    php artisan controller:make JobController
    

    and add the following in the app/routes.php file:

    Route::resource('job','JobController');
    

    Go to JobController populate the index method.

    public function index()
    {
        $jobs = Job::all();
    
        return View::make('job.index')->with('jobs',$jobs);
    }
    
  2. IronMQ and Laravel: Setup

    This entry is part 1 of 2 in the series IronMQ and Laravel

    This two-part article series aims to make a beginner understand using push queues with Laravel. To give a brief overview, the final solution which we will be looking at is a simple form to upload photos and resize them. Resizing images is a time consuming task, so instead of making a user wait until the image is resized we can do it in the background. At the same time, we’ll learn to use a tool called ngrok so that we can use queues in our local system.

    The source code is provided at: https://github.com/phpmasterdotcom/laravel-queues

    You can also try it in a proper live server.

    Queues and Iron

    A queue is nothing but a pipeline where we line up our jobs and they are executed in the order they are inserted. Let’s say currently our queue is empty and we put Job A into it, then we put Job B and Job C. We read the queue in the same order. First we take Job A from the queue and finish it, and then work on next one. If you focus on the “read” part, we always have to check our queue to see if any new job is posted. Wouldn’t it be better if the queue itself notifies us when a job is available ? Queues like those are called push queues, and that’s what we’ll be using with IronMQ.

    Iron MQ is a service which makes our life easier when it comes to using queues. When you create a push queue you also need to mention a subscriber to that queue. A subscriber is nothing but a url which will be called when a job is available in the queue with the data originally provided to the job.

    To read more about job queues and see comparisons between different solutions, see this article.

    Setup and Installation

    In this step we install Laravel and all required dependencies via composer. We also create an account at Iron and install a tool called ngrok.

    Laravel

    1. Install Composer.

    2. Install Laravel

      composer create-project laravel/laravel --prefer-dist

      After this command you should see the folder “laravel”. Open the laravel folder in your favorite text editor or IDE.

      Go into the folder via the command line and run:

      php artisan serve

      At this point if you open http://localhost:8000 you should see the home page of your Laravel installation.

    3. Set up the Database : We will use MySQL for our database. Create a database via the command line, phpmyadmin, or whichever tool you are comfortable with. After you created a database, go to app/config/app.php and find the section similar to the following: