ba1ar
ba1ar

Reputation: 534

Laravel 5.1 - Execute a PHP script in the Background

I have a web application written using Laravel 5.1 that runs a script when a user requests a certain page or clicks a button. This should activate the script in the background. I have tried this using Jobs & queues.

Here is my code chunk: myjob.php

class myjob extends Job implements SelfHandling, ShouldQueue {
use InteractsWithQueue, SerializesModels;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    //
    set_time_limit(0);
    $this->writeJobLogs('Error', 'Start Execution');
    //Job Processing Code
    $this->writeJobLogs('Error', 'End Execution');
}
}

Controller.php

class ManageController extends Controller {
    public function testJob(){
        $this->dispatch(new myjob());
    }
}

Job Processing Code is expected to take at least 10 minutes to get executed. Now when I run the code it throws error after which is given below:

[Symfony\Component\Process\Exception\ProcessTimedOutException] The process ""C:\wamp\bin\php\php5.5.12\php.exe" "artisan" queue:work --queue="default" --delay=0 --memory=128 --sleep=3 --tries=0 --env="local"" exceeded the timeout of 60 seconds.

& Job Processing Code is expected to be executed multiple times simultaneously, respective to the user requests. So I have a doubt regarding that if queues will be working fine or I have any other better choice. If so, please suggest.

Upvotes: 2

Views: 2215

Answers (2)

DLMousey
DLMousey

Reputation: 147

The first solution that springs to mind for me (may not be ideal for you) would be to set up the logging commands on seperate functions inside the controller, eg.

public function writeLog()
{
    // Do your logging here
}

and make AJAX calls to a route pointing at those functions, eg;

// Laravel Route
Route::get('ajaxDoStuff', 'FooController@writeLog');

// AJAX on Blade Template
$.ajax({
//all the usual stuff for an AJAX call
url: "/ajaxDoStuff"
});

That's how i'd do it anyway not sure if it's ideal for you?

Upvotes: 0

user320487
user320487

Reputation:

The Laravel Queue does not run processes in the background, rather gives you the ability to defer execution until a later time. Your script is timing out because it is exceeding the max execution time specified in your php.ini.

Upvotes: 1

Related Questions