Skip to content

Commit

Permalink
+ timezone property
Browse files Browse the repository at this point in the history
+ disable-dev-shm-usage to default chrome properties
+ thread synchronization
  • Loading branch information
Frisch12 committed May 28, 2019
1 parent dabc1e2 commit f9bfc2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ buildscript {
plugins {
id 'java'
id "org.springframework.boot" version "2.1.4.RELEASE"
id "io.spring.dependency-management" version "1.0.7.RELEASE"
id "io.freefair.lombok" version "3.2.0"
}

Expand All @@ -21,6 +20,7 @@ sourceCompatibility = 1.11

ext {
awsVersion = "2.5.33"
springVersion = "2.1.4.RELEASE"
}

bootRun {
Expand All @@ -36,10 +36,10 @@ repositories {
}

dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "com.h2database:h2"
compile "org.springframework.boot:spring-boot-starter-web:${springVersion}"
compile "org.springframework.boot:spring-boot-starter-data-jpa:${springVersion}"
compile "org.springframework.boot:spring-boot-starter-actuator:${springVersion}"
compile "com.h2database:h2:1.4.199"

compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void run() {
((RemoteWebDriver)scheduledSeleniumSession.getSession().getDriver()).executeScript("window.scrollTo(0, " + scheduledSeleniumSession.getYScroll() + ");");
helper.createScreenshot(scheduledSeleniumSession.getSession().getDriver(), new File(new File(outputDirectory), screenshot.getId().toString() + ".png"), screenshot.isTimestamp());
} catch (Exception e) {
log.error("Error while creating screenshot", e);
scheduledSeleniumSession.delete();
log.error("Error while creating screenshot. Stopping Session!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.TimeZone;

@Component
public class SeleniumHelper {
Expand All @@ -27,6 +29,9 @@ public class SeleniumHelper {
@Value("${screenshot.date-font-size}")
private float fontSize;

@Value("${screenshot.time_zone}")
private String timeZone;

public void createScreenshot(WebDriver driver, File outputFile, boolean withTimestamp) throws IOException {
byte[] screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
if (withTimestamp) {
Expand All @@ -39,7 +44,7 @@ public void createScreenshot(WebDriver driver, File outputFile, boolean withTime
} catch (Exception ex) {
throw new RuntimeException(ex);
}
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateFormat));
String time = LocalDateTime.now().atZone(TimeZone.getTimeZone(timeZone).toZoneId()).format(DateTimeFormatter.ofPattern(dateFormat));
Rectangle2D stringBounds = graphics.getFontMetrics(font).getStringBounds(time, graphics);
int stringWidth = (int) stringBounds.getWidth();
int imageWidth = image.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,39 @@ public void doScreenshot() {
}

public ScheduledSeleniumSession getSession(UUID id) {
if(sessionMap.containsKey(id)) {
return sessionMap.get(id);
synchronized (sessionMap) {
if (sessionMap.containsKey(id)) {
return sessionMap.get(id);
}
return null;
}
return null;
}

private void execute(List<Screenshot> all) {
for (UUID key : sessionMap.keySet()) {
Screenshot screenshot = all.stream().filter(a -> a.getId().equals(key)).findFirst().orElse(null);
ScheduledSeleniumSession scheduledSeleniumSession = sessionMap.get(key);
if (screenshot == null) continue;
if (!scheduledSeleniumSession.isReady()) continue;

try {
if (((RemoteWebDriver)scheduledSeleniumSession.getSession().getDriver()).getSessionId() == null) {
throw new Exception("Session died! Try to restart automatically");
}
if ((System.currentTimeMillis() - scheduledSeleniumSession.getLastExecution()) < screenshot.getIntervalSeconds() * 1000)
continue;
log.info("Taking screenshot for {}", key.toString());
new ScreenshotThread(scheduledSeleniumSession, screenshot, outputDirectory, seleniumHelper).start();
scheduledSeleniumSession.setLastExecution(System.currentTimeMillis());
} catch (Exception ex) {
log.error("Error while execute " + key, ex);

if (((RemoteWebDriver)scheduledSeleniumSession.getSession().getDriver()).getSessionId() == null) {
scheduledSeleniumSession.getSession().getDriver().quit();
synchronized (sessionMap) {
for (UUID key : sessionMap.keySet()) {
Screenshot screenshot = all.stream().filter(a -> a.getId().equals(key)).findFirst().orElse(null);
ScheduledSeleniumSession scheduledSeleniumSession = sessionMap.get(key);
if (screenshot == null) continue;
if (!scheduledSeleniumSession.isReady()) continue;

try {
if (((RemoteWebDriver) scheduledSeleniumSession.getSession().getDriver()).getSessionId() == null) {
throw new Exception("Session died! Try to restart automatically");
}
if ((System.currentTimeMillis() - scheduledSeleniumSession.getLastExecution()) < screenshot.getIntervalSeconds() * 1000)
continue;
log.info("Taking screenshot for {}", key.toString());
new ScreenshotThread(scheduledSeleniumSession, screenshot, outputDirectory, seleniumHelper).start();
scheduledSeleniumSession.setLastExecution(System.currentTimeMillis());
} catch (Exception ex) {
log.error("Error while execute " + key, ex);

if (((RemoteWebDriver) scheduledSeleniumSession.getSession().getDriver()).getSessionId() == null) {
scheduledSeleniumSession.getSession().getDriver().quit();
}
sessionMap.remove(key);
}
sessionMap.remove(key);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
selenium.implicitWait=100
selenium.browser.height=1080
selenium.browser.width=1920
selenium.chrome_args=--no-sandbox --headless
selenium.chrome_args=--no-sandbox --headless --disable-dev-shm-usage

screenshot.outputDirectory=screenshots
screenshot.date-format=yyyy-MM-dd HH:mm:ss
screenshot.date-font-size=24
screenshot.time_zone=Europe/Berlin

screenshot.aws.enabled=false
screenshot.aws.region=eu-central-1
Expand Down

0 comments on commit f9bfc2f

Please sign in to comment.