0

My Laravel app needs to do some process (on the backend) which can last many tenths of seconds. This is done in a for loop so I have the ability to know the progress during the long running process. In my minimal test (within my Laravel project) it lasts 10 seconds.

public function massAction(Request $request) {

    for ($i = 0; $i < 10; $i++) {

        sleep(1);
        Log::info("Progress in long running task is " .$i );

    }

    $output = array(
        'success' => 'Whatever'
    );

    return response()->json($output);
}

The process is triggered by a button in a form. When the user clicks on the button, I want the progress to be shown on the page, but the progress is only shown when the long running process completed. This phenomenon is well described here, and I came there from this solution that I tried to apply (create a route that returns the progress).

For now I will let the ajax client code aside. As the "getProgress" route is "get" it can be called manually during the long running task (which last here 10 seconds) :

 public function getProgress() {
  $progress = rand(0, 100); // for testing purpose only
  Log::info("Progress is " . $progress);
  return response()->json(array($progress));
 }

So as you can see there is no use of PHP SESSION, and both methods are independent. Yet when I submit the form, and then go in another tab and request the url of getProgress, its result still comes after the long running task has completed. Here is an excerpt of the Laravel log file :

[2020-04-16 06:53:49] local.INFO: Progress in long running task is 0  
[2020-04-16 06:53:50] local.INFO: Progress in long running task is 1  
[2020-04-16 06:53:51] local.INFO: Progress in long running task is 2  
[2020-04-16 06:53:52] local.INFO: Progress in long running task is 3  
[2020-04-16 06:53:53] local.INFO: Progress in long running task is 4  
[2020-04-16 06:53:54] local.INFO: Progress in long running task is 5  
[2020-04-16 06:53:55] local.INFO: Progress in long running task is 6  
[2020-04-16 06:53:56] local.INFO: Progress in long running task is 7  
[2020-04-16 06:53:57] local.INFO: Progress in long running task is 8  
[2020-04-16 06:53:58] local.INFO: Progress in long running task is 9  
[2020-04-16 06:53:58] local.INFO: Progress in getProgress is 5  

I've read that PHP SESSION will make the calls being queued (what I am experiencing indeed). But I am not using it at all and neither does Laravel's Session. So what is (or what could be) postponing the call to getProgress in my Laravel project ? Or put another way what is preventing getProgress to return a result immediately ?

Any help appreciated :-),

Edit : Laravel dev server is SINGLE-THREADED

I was actually testing this on a dev machine via Laravel artisan serve which is single-threaded. No wonder then that I experienced that behaviour. Testing the same process on a real web server (Apache) removed all the problems!

HelloWorld
  • 2,275
  • 18
  • 29
  • Can you show your AJAX – lufc Apr 15 '20 at 18:22
  • @lufc see my edit although I doubt it comes from javascript since the direct call to `getProgress` via its route has also to wait until the completion of the long running process. – HelloWorld Apr 15 '20 at 19:16

0 Answers0