3
votes

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
The only way I found is to turn OFF server and then turn ON again but that's not long term solutionAleks Per
@thisiskelvin APP_NAME=Laravel APP_ENV=local APP_KEY=base64:S6/heKijvfw8KB2K10vY77777hUhVYB1ZdzUhPEbqo= APP_DEBUG=true APP_URL=localhostAleks Per
Thank you, posting an answer which may helpthisiskelvin
have you tried gc after each request? gc_collect_cycles()Misagh Laghaei
@MisaGH Unfortunately it doesn't help...Aleks Per

3 Answers

1
votes

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.

0
votes
 killall -o 1m chrome

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

0
votes

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