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

Viewed 1896

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?

enter image description here

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

enter image description here

3 Answers

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

is the right answer ... It will delete all chrome 'zombie' process older than 1 minute

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
Related