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

Asked

Viewed 324 times

0

Hello, I am working with an application in Laravel 4.2 and need that, when the user makes the request 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? Some example?

Thank you for your attention.

1 answer

3


I found a solution to meet my needs, a queue manager application called Beanstalkd, consuming this service is really easy in Laravel 4.2, as it has support to use it. Even more interesting is knowing that there are several libraries in several languages (C/C++, Django, Go, Java, PHP, Lua, Python, among others) to use Beanstalkd.

**

Installation of Beanstalkd

**

The installation can be done in several versions of Linux, I cite Ubuntu, using apt-get package manager, easy to use, the website has installation information for various Operating Systems, to install only using 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

**

Beanstalkd Console Installation

**

To be able to see the contents of the queues in Beanstalkd I recommend the Beanstalkd Console, simple to install makes it very easy to check the queues of Beanstalkd and even to close processes that are stuck in the queues, to install is easy, just download the official website archive, unpack and set up in your environment as a website. Local access is: localhost:8080 (port varies according to the configuration of your environment).

**

Configuration of Laravel 4.2

**

To consume the service in Laravel 4.2:

First add project dependency using Composer:

composer require pda/pheanstalk ~2.0

Then, already with the database configured in Laravel, add a table in the database to receive errors running from the queue 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. So that Laravel can "listen" to the queue and answer your requests, on the command line, inside the folder of your Laravel application use the command:

php artisan queue:listen

**

Example

**

As an example test, in a "clean" version of Laravel already configured to use Beanstalkd, proceed as follows:

1. Alter /app/Routes.php for:

<?php

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

2. Alter /app/controllers/Homecontroller.php for:

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 with name Calculus.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 type the access URL, each time the page is updated, 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

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

Completion

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

Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.