Laravel Dusk 和内存使用 - 如何关闭浏览器和自定义进程

Ale*_*Per 5 php webdriver out-of-memory laravel laravel-dusk

我在控制器中使用 Laravel Dusk,以便用户使用我的网站获取任何网站的屏幕截图。

我的代码:

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Dusk\ElementResolver;
use Exception;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;
use Facebook\WebDriver\WebDriverBy;

use Mail;

class ScreenController extends Controller {
    public function take_screenshoot() {

        $process = (new ChromeProcess)->toProcess();
        if ($process->isStarted()) {
          $process->stop();
        }
        $process->start();

        $options      = (new ChromeOptions)->addArguments([
                '--disable-gpu',
                '--headless',
                '--window-size=1920,1080',
                '--no-sandbox'
            ]);
        $capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);

              $driver = retry(5, function () use ($capabilities) {
          return RemoteWebDriver::create('http://localhost:9515', $capabilities, 50000, 60000);
        }, 50);

        $browser = new Browser($driver, new ElementResolver($driver, ''));
        $browser->resize(1920, 1080);
        $browser->visit('http://www.example.com')->mouseover('iframe');

        $screenshoot_name = md5(time());
        $browser->driver->takeScreenshot(base_path('tests/Browser/screenshots/'.$screenshoot_name.'.png'));
        echo "<img src='/tests/Browser/screenshots/".$screenshoot_name.".png></img>";

        $process->stop();


      }
Run Code Online (Sandbox Code Playgroud)

这里一切正常,正如预期的那样,只是每个用户请求(用户访问)的内存使用量越来越高......如何解决这个问题?为什么$process->stop();不关闭浏览器?

在此输入图像描述

更新 我试过:

$browser->quit();
Run Code Online (Sandbox Code Playgroud)

$browser->driver->quit();
Run Code Online (Sandbox Code Playgroud)

但我得到了错误:

Facebook \ WebDriver \ Exception \ WebDriverCurlException http DELETE 抛出 Curl 错误到 /session/8a4f02f2d13648ccfbdead97b338e4f4 无法连接到本地主机端口 9515:连接被拒绝

UPDATE 2.0 这是我运行时得到的

# ps -aux | less
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ale*_*Per 0

 killall -o 1m chrome
Run Code Online (Sandbox Code Playgroud)

是正确的答案...它将删除所有超过 1 分钟的 chrome“僵尸”进程