Search code examples
phplaravelherokulumen

Make an asynchronous Artisan::call on Heroku


I have an HTTP endpoints written in lumen and deployed on heroku which is calling Artisan::call("queue:work"...) to start processing some queue jobs.

 public function startProcess(Request $request)
    {
     
        Artisan::call('queue:work', ['--stop-when-empty', '--max-jobs' => 1]);
        return 'ok';
        
    }

As heroku allows a max timeout of 30 seconds, the process is failing as Artisan::call is synchronous, is there any way to make it asynchronous ?


Solution

  • You can continue processing after returning the response, if that is what you want:

        public function startProcess(Request $request)
        {
            // dont abort script execution if client disconnect
            ignore_user_abort(true);
    
            // dont limit the time
            set_time_limit(0);
    
            // turn on output buffering
            ob_start();
    
            // prepare the response
    
            // send the response
            return 'ok';
            header('Connection: close');
            header('Content-Length: '.ob_get_length());
            // turn off output buffering
            ob_end_flush();
            // flush system output buffer
            flush();
    
            // continue processing
            Artisan::call('queue:work', ['--stop-when-empty', '--max-jobs' => 1]);
        }