简体   繁体   中英

Laravel Dusk and memory usage - how to close the browser and custom process

I'm using Laravel Dusk in the controller for users to get screenshots of the any website using mine website.

My code:

<?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();


      }

Everything works well here as expected just memory usage is by each user request (user visit) higher and higher... How to solve that problem? Why $process->stop(); do not close the browser?

在此处输入图片说明

UPDATE I tried:

$browser->quit();

and

$browser->driver->quit();

but I got error:

Facebook \\ WebDriver \\ Exception \\ WebDriverCurlException Curl error thrown for http DELETE to /session/8a4f02f2d13648ccfbdead97b338e4f4 Failed to connect to localhost port 9515: Connection refused

UPDATE 2.0 This is what I got when I run

# ps -aux | less

在此处输入图片说明

I had similar issues using a package which wrapped chrome. What I ended up doing is running this on the commands line:

pkill -f -- "chromium-browser"

This killed all processes which contained chromium-browser in it. I'm sure there is a way to add this to the code to fire once completed.

 killall -o 1m chrome

是正确的答案......它将删除所有超过 1 分钟的 chrome 'zombie' 进程

I got the exact same issue and ended up solving it adding:

$browser->quit();
$process->stop();

Note that the order matters . If I swap the lines I get the same error that you mentioned:

Facebook \ WebDriver \ Exception \ WebDriverCurlException Curl error thrown for http DELETE to /session/8a4f02f2d13648ccfbdead97b338e4f4 Failed to connect to localhost port 9515: Connection refused

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM