我正在使用Dusk 5.1。
我的目标是使用Dusk在生产中刮擦外部站点。没什么多余的,只是一些RSS提要,也许还有论坛主题。我的应用程序在production
中的自己的docker-compose堆栈中运行(不是Laradock或其他东西)。
我没有注册
DuskServiceProvider
,因此在production
中没有风险。
问题出在这里
Facebook\WebDriver\Exception\WebDriverCurlException : Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"w3c":false,"binary":"","args":["--disable-gpu","--headless","--no-sandbox","--verbose"]}}}
Failed to connect to localhost port 9515: Connection refused
这里是我正在使用的Browser.php
,而不是Dusk随附的用于测试的对象:
/**
* Reporting browser for console commands
*/
class Browser
{
/**
* @var \Laravel\Dusk\Browser
*/
private $browser;
public function browse(Closure $callback)
{
if (!$this->browser) {
$this->browser = $this->newBrowser($this->createWebDriver());
}
try {
$callback($this->browser);
} catch (Exception $e) {
throw $e;
} catch (Throwable $e) {
throw $e;
}
}
function __destruct()
{
if ($this->browser) {
$this->closeBrowser();
}
}
protected function closeBrowser()
{
if (!$this->browser) {
throw new Exception("The browser hasn't been initialized yet");
}
$this->browser->quit();
$this->browser = null;
}
protected function newBrowser($driver)
{
return new DuskBrowser($driver);
}
/**
* Create the remote web driver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function createWebDriver()
{
return retry(5, function () {
return $this->driver();
}, 50);
}
protected function driver()
{
$options = new ChromeOptions();
$options->addArguments([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--verbose',
]);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
return RemoteWebDriver::create(
'http://localhost:9515', $capabilities
);
}
}
然后在我使用的应用程序中:
$defaultBrowser = app(Browser::class);
$defaultBrowser->browse(function (\Laravel\Dusk\Browser $browser) use ($feed) {
$pageSoure = $browser->visit($this->feedUrl())
->driver->getPageSource();
preg_match('/(<rss.+?<\/rss>)/is', $pageSoure, $matches);
$feed->fromContents($matches[0]);
});
我不确定要采取哪些步骤才能正确启动容器中的chrome驱动程序。
注意:在线上有指南可将Laravel Dusk与依赖Selenium的Docker结合使用。这样做会导致我的应用程序出现并发问题,因为它们需要将Selenium作为独立服务运行,当多个作业同时使用它时会导致会话问题。
我的docker-compose 我不确定为什么这会有意义,但是有些人要求这样做。
version: '3'
services:
web:
build:
context: .
dockerfile: infrastructure/web/Dockerfile
image: registry.gitlab.com/namespace/project:web
env_file: .env
volumes:
- ./:/var/www/html
- ./infrastructure/web:/etc/nginx/conf.d
links:
- redis
- db
ports:
- "80:80"
frontend-dev:
image: registry.gitlab.com/namespace/project:web
depends_on:
- web
volumes:
- ./:/var/www/html
command: sh -c "yarn install && yarn run dev"
queue:
build:
context: .
dockerfile: infrastructure/cli/Dockerfile
image: registry.gitlab.com/namespace/project:cli
env_file: .env
volumes:
- ./:/var/www/html
command: php /var/www/html/artisan horizon
links:
- redis
- db
redis:
image: redis:5.0-alpine
ngrok:
image: wernight/ngrok
env_file: .env
depends_on:
- web
ports:
- "4040:4040"
db:
image: mysql:8.0.3
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: xxxxxx
MYSQL_DATABASE: xxxxxx
MYSQL_USER: xxxxxx
MYSQL_PASSWORD: xxxxxx