Laravel 4.2 - Working with asynchronous tasks to generate reports .xlsx

0

Hello, I am working with an application in Laravel 4.2 and it requires that when the user requests to generate a system report, a task is launched to generate the report, when this report is ready in a folder, I need to send an email to the user with the URL to download the report.

Is there a way to work with asynchronous tasks in Laravel? Any examples?

Thanks for your attention.

    
asked by anonymous 23.11.2016 / 21:02

1 answer

2

I found a solution to meet my needs, a queue manager application called Beanstalkd, consuming this service is really easy in Laravel 4.2 because it has support for using it. It is also interesting to know that there are many libraries in different languages (C / C ++, Django, Go, Java, PHP, Moon, Python, among others) to use Beanstalkd.

**

Beanstalkd Installation

**

The installation can be done on several versions of Linux, I mention Ubuntu, using the easy-to-use apt-get package manager, the website has installation information for various Operating Systems, to install only use the command line:

sudo apt-get update

To update the package manager list, and then:

sudo apt-get install beanstalkd

To install Beanstalkd, to start the service use the command:

sudo service beanstalkd start

**

Installing the Beanstalkd Console

**

In order to see the contents of the queues in Beanstalkd I recommend the Beanstalkd Console, simple to install makes it easy to check the queues of Beanstalkd and even close processes that are locked in the queues, to install is easy, just download the official website file , unzip and configure in your environment as a website. Local access is by localhost: 8080 (port varies according to the configuration of your environment).

**

Laravel 4.2 Configuration

**

To consume the service in Laravel 4.2:

First add dependency to the project using Composer:

composer require pda/pheanstalk ~2.0

Next, with the database already configured in Laravel, add a table in the base to receive queue execution errors with the commands:

php artisan queue:failed-table

php artisan migrate

You should then configure the file in app / config / queue.php , the following lines have to be changed:

'default' => 'beanstalkd', // Driver a ser utilizado, linha 18

'host'   => 'minhaFila', // Nome do host da sua fila, linha 39

'queue'  => 'projeto', // Nome da fila do seu projeto, linha 40

With this, Laravel 4.2 is configured and ready for use. In order for Laravel to be able to "listen" to the queue and answer your requests, in the command line, inside the folder of your Laravel application use the command:

php artisan queue:listen

**

Example

**

As a test example, in a "clean" version of Laravel already properly configured to use Beanstalkd, do the following:

1. Change /app/routes.php to:

<?php

Route::get('/', 'HomeController@showWelcome');

2. Change /app/controllers/HomeController.php to:

class HomeController extends BaseController {

    public function showWelcome()
    {
        $numero = 120;
        // envia tarefa para fila
        Queue::push('Calculos@fatorial', array('numero' => $numero));

        return View::make('hello');
    }

}

3. Create a file named Calculations.php in / app / controllers , with the following code:

<?php

    class Calculos {
        public function fatorial($job, $data)
        {
            Log::info('Iniciando JOB Fatorial');
            $valor = $data['numero'];
            Log::info('Valor obtido para Fatorial, iniciando calculos...');

            $r = 1;
            $fatorial = 0;

            // CALCULANDO
            for($i = $valor; $i > 0; $i--) {
                $r = $r * $i;
            }
            $fatorial = $r;
            Log::info('O fatorial obtido foi: ' . $fatorial);

            // Após execução da Job remove-la da lista
            $job->delete();

            Log::info('JOB Fatorial removida da Lista');
        }
    }

Now just run your environment and in the browser enter the access URL, each time the page is refreshed, a new task will be launched in the Beanstalkd list, you can view the tasks and even remove them from the list using the Beanstalkd Console. Remember to use the command:

php artisan queue:listen

Within the root directory of your Laravel project so that it can receive requests from the Beanstalkd List.

Conclusion

In my case Beanstalkd was a great solution for my web application to be able to perform tasks that require a long run time without impacting the customers' navigation so I decided to spend some time writing some of the knowledge I got because was not easy, I hope the above content is of great help.

Hug.

    
28.11.2016 / 15:24