在 Heroku 上做一个异步 Artisan::call

Make an asynchronous Artisan::call on Heroku

我有一个用 lumen 编写并部署在 heroku 上的 HTTP 端点,它正在调用 Artisan::call("queue:work"...) 开始处理一些队列作业。

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

由于 heroku 允许最长 30 秒的超时,进程失败,因为 Artisan::call 是同步的,有什么办法让它异步吗?

您可以在返回响应后继续处理,如果这是您想要的:

    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]);
    }