From b5dfb5945883e48655f89eaabaa3b8026a1743c9 Mon Sep 17 00:00:00 2001 From: Sheldan <5037282+Sheldan@users.noreply.github.com> Date: Sat, 10 Dec 2022 16:43:29 +0100 Subject: [PATCH] [SIS-14] adding a configurable delay for debra donation notifications adding more websocket event logging preparing for release --- .github/workflows/release.yml | 2 +- .../debra/config/DebraFeatureConfig.java | 8 ++++ .../debra/listener/WebsocketListener.java | 44 +++++++++++++++++-- .../module/debra/service/DonationService.java | 8 ++-- .../debra/src/main/resources/debra.properties | 3 ++ .../docker-compose/src/main/resources/.env | 2 +- pom.xml | 4 +- 7 files changed, 61 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bf05edd2..952e315b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,5 +34,5 @@ jobs: env: REGISTRY_PREFIX: docker.pkg.github.com/sheldan/sissi/ VERSION: ${{ env.version }} - ABSTRACTO_VERSION: 1.4.13 + ABSTRACTO_VERSION: 1.4.14 ABSTRACTO_REGISTRY_PREFIX: docker.pkg.github.com/sheldan/abstracto/ \ No newline at end of file diff --git a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/config/DebraFeatureConfig.java b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/config/DebraFeatureConfig.java index af5ef210..1830c467 100644 --- a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/config/DebraFeatureConfig.java +++ b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/config/DebraFeatureConfig.java @@ -10,6 +10,9 @@ import java.util.List; @Component public class DebraFeatureConfig implements FeatureConfig { + + public static final String DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY = "debraDonationNotificationDelayMillis"; + public static final String DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME = "DEBRA_DONATION_NOTIFICATION_SERVER_ID"; @Override public FeatureDefinition getFeature() { return DebraFeatureDefinition.DEBRA; @@ -19,4 +22,9 @@ public class DebraFeatureConfig implements FeatureConfig { public List getRequiredPostTargets() { return Arrays.asList(DebraPostTarget.DEBRA_DONATION_NOTIFICATION, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2); } + + @Override + public List getRequiredSystemConfigKeys() { + return Arrays.asList(DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY); + } } diff --git a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/listener/WebsocketListener.java b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/listener/WebsocketListener.java index d281fe13..11f24136 100644 --- a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/listener/WebsocketListener.java +++ b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/listener/WebsocketListener.java @@ -1,6 +1,7 @@ package dev.sheldan.sissi.module.debra.listener; import dev.sheldan.abstracto.core.listener.AsyncStartupListener; +import dev.sheldan.abstracto.core.service.ConfigService; import dev.sheldan.sissi.module.debra.config.DebraProperties; import dev.sheldan.sissi.module.debra.model.Donation; import dev.sheldan.sissi.module.debra.service.DonationService; @@ -9,8 +10,12 @@ import okhttp3.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javax.annotation.Nullable; import java.util.concurrent.TimeUnit; +import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY; +import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME; + @Component @Slf4j public class WebsocketListener extends WebSocketListener implements AsyncStartupListener { @@ -21,6 +26,12 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup @Autowired private DebraProperties debraProperties; + @Autowired + private ConfigService configService; + + private WebSocket webSocketObj; + private OkHttpClient clientObj; + @Override public void onOpen(WebSocket webSocket, Response response) { log.info("Connected to donation websocket."); @@ -31,6 +42,11 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup public void onMessage(WebSocket webSocket, String text) { log.info("Handling received message on websocket."); try { + Long targetServerId = Long.parseLong(System.getenv(DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME)); + Long delayMillis = configService.getLongValueOrConfigDefault(DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY, targetServerId); + log.info("Waiting {} milli seconds to send notification.", delayMillis); + Thread.sleep(delayMillis); + log.info("Loading new donation amount and sending notification."); Donation donation = donationService.parseDonationFromMessage(text); donationService.sendDonationNotification(donation).thenAccept(unused -> { log.info("Successfully notified about donation."); @@ -43,16 +59,38 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup } } + @Override + public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) { + log.warn("Websocket connection failed...", t); + webSocket.close(1000, null); + webSocket.close(1000, "Connection closed"); + } + + @Override + public void onClosing(WebSocket webSocket, int code, String reason) { + webSocket.close(1000, null); + log.info("Closing websocket connection. It was closed with code {} and reason {}.", code, reason); + } + @Override public void execute() { - OkHttpClient client = new OkHttpClient.Builder() + if(clientObj != null) { + clientObj.connectionPool().evictAll(); + clientObj.dispatcher().executorService().shutdownNow(); + } + clientObj = new OkHttpClient.Builder() .readTimeout(0, TimeUnit.MILLISECONDS) .retryOnConnectionFailure(true) .build(); + startConnection(clientObj); + clientObj.dispatcher().executorService().shutdown(); + } + + private void startConnection(OkHttpClient client) { + log.info("Starting websocket connection."); Request request = new Request.Builder() .url(debraProperties.getWebsocketURL()) .build(); - client.newWebSocket(request, this); - client.dispatcher().executorService().shutdown(); + this.webSocketObj = client.newWebSocket(request, this); } } diff --git a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/service/DonationService.java b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/service/DonationService.java index f251f759..a5c1c59f 100644 --- a/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/service/DonationService.java +++ b/application/sissi-modules/debra/src/main/java/dev/sheldan/sissi/module/debra/service/DonationService.java @@ -26,6 +26,8 @@ import java.util.concurrent.CompletableFuture; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME; + @Component @Slf4j public class DonationService { @@ -39,7 +41,7 @@ public class DonationService { @Autowired private TemplateService templateService; - private static final String DONATION_NOTIFICATION_TEMPLATE_KEY = "debra_donation_notification"; + private static final String DEBRA_DONATION_NOTIFICATION_TEMPLATE_KEY = "debra_donation_notification"; private static final Pattern MESSAGE_PATTERN = Pattern.compile("(.*) hat (\\d{1,9},\\d{2}) Euro gespendet!
Vielen Dank!
Nachricht:
(.*)"); private static final Pattern DONATION_PAGE_AMOUNT_PARTNER = Pattern.compile("\"metric4\",\\s*\"(.*)\""); @@ -86,8 +88,8 @@ public class DonationService { .donation(donation) .totalDonationAmount(fetchCurrentDonationAmount()) .build(); - MessageToSend messageToSend = templateService.renderEmbedTemplate(DONATION_NOTIFICATION_TEMPLATE_KEY, model); - Long targetServerId = Long.parseLong(System.getenv("DEBRA_DONATION_NOTIFICATION_SERVER_ID")); + MessageToSend messageToSend = templateService.renderEmbedTemplate(DEBRA_DONATION_NOTIFICATION_TEMPLATE_KEY, model); + Long targetServerId = Long.parseLong(System.getenv(DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME)); List> firstMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION, targetServerId); List> secondMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2, targetServerId); firstMessage.addAll(secondMessage); diff --git a/application/sissi-modules/debra/src/main/resources/debra.properties b/application/sissi-modules/debra/src/main/resources/debra.properties index feeccf76..10adf11c 100644 --- a/application/sissi-modules/debra/src/main/resources/debra.properties +++ b/application/sissi-modules/debra/src/main/resources/debra.properties @@ -6,3 +6,6 @@ abstracto.postTargets.debraDonationNotification2.name=debraDonationNotification2 sissi.debra.websocketURL=ws://spenden.baba.fm:8765/ sissi.debra.donationsPageURL=https://em.altruja.de/discord-fuer-debra-2022 + +abstracto.systemConfigs.debraDonationNotificationDelayMillis.name=debraDonationNotificationDelayMillis +abstracto.systemConfigs.debraDonationNotificationDelayMillis.longValue=60000 \ No newline at end of file diff --git a/deployment/docker-compose/src/main/resources/.env b/deployment/docker-compose/src/main/resources/.env index ef8e619d..8abcbc49 100644 --- a/deployment/docker-compose/src/main/resources/.env +++ b/deployment/docker-compose/src/main/resources/.env @@ -31,4 +31,4 @@ DEBRA_DONATION_NOTIFICATION_SERVER_ID=0 PGADMIN_DEFAULT_PASSWORD=admin TOKEN= YOUTUBE_API_KEY= -SISSI_VERSION=1.3.12 \ No newline at end of file +SISSI_VERSION=1.3.13 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 80dcc9af..a3283437 100644 --- a/pom.xml +++ b/pom.xml @@ -20,8 +20,8 @@ 1.8 - 1.4.13 - 1.4.8 + 1.4.14 + 1.4.9