Fix UI healthchecker: run webdriver remotely

This commit is contained in:
ViacheslavKlimov 2022-12-02 13:46:37 +02:00
parent 03a22cb722
commit 4521f15130
3 changed files with 20 additions and 18 deletions

View File

@ -18,8 +18,8 @@ package org.thingsboard.monitoring.service;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -30,8 +30,8 @@ import org.thingsboard.monitoring.data.Latencies;
import org.thingsboard.monitoring.data.MonitoredServiceKey; import org.thingsboard.monitoring.data.MonitoredServiceKey;
import org.thingsboard.monitoring.util.TbStopWatch; import org.thingsboard.monitoring.util.TbStopWatch;
import javax.annotation.PostConstruct; import java.net.MalformedURLException;
import java.io.File; import java.net.URL;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -56,8 +56,8 @@ public class WebUiHealthChecker {
private int monitoringRateSec; private int monitoringRateSec;
@Value("${monitoring.rest_request_timeout_ms}") @Value("${monitoring.rest_request_timeout_ms}")
private int timeoutMs; private int timeoutMs;
@Value("${monitoring.ui.webdriver_location}") @Value("${monitoring.ui.remote_webdriver_url}")
private File webdriverLocation; private String remoteWebdriverUrl;
private final MonitoringReporter monitoringReporter; private final MonitoringReporter monitoringReporter;
private final ScheduledExecutorService monitoringExecutor; private final ScheduledExecutorService monitoringExecutor;
@ -68,21 +68,13 @@ public class WebUiHealthChecker {
private static final String SUBMIT_BTN = "//button[@type='submit']"; private static final String SUBMIT_BTN = "//button[@type='submit']";
private static final String DEVICES_BTN = "//mat-toolbar//a[@href='/devices']"; private static final String DEVICES_BTN = "//mat-toolbar//a[@href='/devices']";
@PostConstruct
private void init() {
System.setProperty("webdriver.gecko.driver", webdriverLocation.getAbsolutePath());
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");
}
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
public void startMonitoring() { public void startMonitoring() {
monitoringExecutor.scheduleWithFixedDelay(() -> { monitoringExecutor.scheduleWithFixedDelay(() -> {
WebDriver driver = null; RemoteWebDriver driver = null;
try { try {
FirefoxOptions options = new FirefoxOptions(); driver = createDriver();
options.setHeadless(true); WebDriverWait wait = createDriverWait(driver);
driver = new FirefoxDriver(options);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(timeoutMs));
driver.manage().window().maximize(); driver.manage().window().maximize();
driver.get(url + "/login"); driver.get(url + "/login");
@ -108,4 +100,14 @@ public class WebUiHealthChecker {
}, 0, monitoringRateSec, TimeUnit.SECONDS); }, 0, monitoringRateSec, TimeUnit.SECONDS);
} }
private RemoteWebDriver createDriver() throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
options.setPageLoadTimeout(Duration.ofMillis(timeoutMs));
return new RemoteWebDriver(new URL(remoteWebdriverUrl + "/wd/hub"), options);
}
private WebDriverWait createDriverWait(WebDriver driver) {
return new WebDriverWait(driver, Duration.ofMillis(timeoutMs));
}
} }

View File

@ -66,8 +66,8 @@ monitoring:
ui: ui:
enabled: '${UI_MONITORING_ENABLED:false}' enabled: '${UI_MONITORING_ENABLED:false}'
url: '${UI_URL:http://localhost:4200}' url: '${UI_URL:http://localhost:4200}'
remote_webdriver_url: '${UI_MONITORING_REMOTE_WEBDRIVER_URL:http://localhost:4444}'
monitoring_rate_sec: '${UI_MONITORING_RATE_SEC:300}' monitoring_rate_sec: '${UI_MONITORING_RATE_SEC:300}'
webdriver_location: '${WEBDRIVER_LOCATION:classpath:geckodriver}'
notification_channels: notification_channels:
slack: slack: