mirror of
https://github.com/Sheldan/Sissi.git
synced 2026-01-05 08:54:27 +00:00
[SIS-14] adding a configurable delay for debra donation notifications
adding more websocket event logging preparing for release
This commit is contained in:
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@@ -34,5 +34,5 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
REGISTRY_PREFIX: docker.pkg.github.com/sheldan/sissi/
|
REGISTRY_PREFIX: docker.pkg.github.com/sheldan/sissi/
|
||||||
VERSION: ${{ env.version }}
|
VERSION: ${{ env.version }}
|
||||||
ABSTRACTO_VERSION: 1.4.13
|
ABSTRACTO_VERSION: 1.4.14
|
||||||
ABSTRACTO_REGISTRY_PREFIX: docker.pkg.github.com/sheldan/abstracto/
|
ABSTRACTO_REGISTRY_PREFIX: docker.pkg.github.com/sheldan/abstracto/
|
||||||
@@ -10,6 +10,9 @@ import java.util.List;
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DebraFeatureConfig implements FeatureConfig {
|
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
|
@Override
|
||||||
public FeatureDefinition getFeature() {
|
public FeatureDefinition getFeature() {
|
||||||
return DebraFeatureDefinition.DEBRA;
|
return DebraFeatureDefinition.DEBRA;
|
||||||
@@ -19,4 +22,9 @@ public class DebraFeatureConfig implements FeatureConfig {
|
|||||||
public List<PostTargetEnum> getRequiredPostTargets() {
|
public List<PostTargetEnum> getRequiredPostTargets() {
|
||||||
return Arrays.asList(DebraPostTarget.DEBRA_DONATION_NOTIFICATION, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2);
|
return Arrays.asList(DebraPostTarget.DEBRA_DONATION_NOTIFICATION, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getRequiredSystemConfigKeys() {
|
||||||
|
return Arrays.asList(DEBRA_DONATION_NOTIFICATION_DELAY_CONFIG_KEY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.sissi.module.debra.listener;
|
package dev.sheldan.sissi.module.debra.listener;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.core.listener.AsyncStartupListener;
|
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.config.DebraProperties;
|
||||||
import dev.sheldan.sissi.module.debra.model.Donation;
|
import dev.sheldan.sissi.module.debra.model.Donation;
|
||||||
import dev.sheldan.sissi.module.debra.service.DonationService;
|
import dev.sheldan.sissi.module.debra.service.DonationService;
|
||||||
@@ -9,8 +10,12 @@ import okhttp3.*;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.concurrent.TimeUnit;
|
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
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WebsocketListener extends WebSocketListener implements AsyncStartupListener {
|
public class WebsocketListener extends WebSocketListener implements AsyncStartupListener {
|
||||||
@@ -21,6 +26,12 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DebraProperties debraProperties;
|
private DebraProperties debraProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
|
||||||
|
private WebSocket webSocketObj;
|
||||||
|
private OkHttpClient clientObj;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket webSocket, Response response) {
|
public void onOpen(WebSocket webSocket, Response response) {
|
||||||
log.info("Connected to donation websocket.");
|
log.info("Connected to donation websocket.");
|
||||||
@@ -31,6 +42,11 @@ public class WebsocketListener extends WebSocketListener implements AsyncStartup
|
|||||||
public void onMessage(WebSocket webSocket, String text) {
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
log.info("Handling received message on websocket.");
|
log.info("Handling received message on websocket.");
|
||||||
try {
|
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);
|
Donation donation = donationService.parseDonationFromMessage(text);
|
||||||
donationService.sendDonationNotification(donation).thenAccept(unused -> {
|
donationService.sendDonationNotification(donation).thenAccept(unused -> {
|
||||||
log.info("Successfully notified about donation.");
|
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
|
@Override
|
||||||
public void execute() {
|
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)
|
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||||
.retryOnConnectionFailure(true)
|
.retryOnConnectionFailure(true)
|
||||||
.build();
|
.build();
|
||||||
|
startConnection(clientObj);
|
||||||
|
clientObj.dispatcher().executorService().shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startConnection(OkHttpClient client) {
|
||||||
|
log.info("Starting websocket connection.");
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(debraProperties.getWebsocketURL())
|
.url(debraProperties.getWebsocketURL())
|
||||||
.build();
|
.build();
|
||||||
client.newWebSocket(request, this);
|
this.webSocketObj = client.newWebSocket(request, this);
|
||||||
client.dispatcher().executorService().shutdown();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class DonationService {
|
public class DonationService {
|
||||||
@@ -39,7 +41,7 @@ public class DonationService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TemplateService templateService;
|
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!<br \\/>Vielen Dank!<br \\/>Nachricht:<br \\/>(.*)");
|
private static final Pattern MESSAGE_PATTERN = Pattern.compile("(.*) hat (\\d{1,9},\\d{2}) Euro gespendet!<br \\/>Vielen Dank!<br \\/>Nachricht:<br \\/>(.*)");
|
||||||
private static final Pattern DONATION_PAGE_AMOUNT_PARTNER = Pattern.compile("\"metric4\",\\s*\"(.*)\"");
|
private static final Pattern DONATION_PAGE_AMOUNT_PARTNER = Pattern.compile("\"metric4\",\\s*\"(.*)\"");
|
||||||
@@ -86,8 +88,8 @@ public class DonationService {
|
|||||||
.donation(donation)
|
.donation(donation)
|
||||||
.totalDonationAmount(fetchCurrentDonationAmount())
|
.totalDonationAmount(fetchCurrentDonationAmount())
|
||||||
.build();
|
.build();
|
||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(DONATION_NOTIFICATION_TEMPLATE_KEY, model);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(DEBRA_DONATION_NOTIFICATION_TEMPLATE_KEY, model);
|
||||||
Long targetServerId = Long.parseLong(System.getenv("DEBRA_DONATION_NOTIFICATION_SERVER_ID"));
|
Long targetServerId = Long.parseLong(System.getenv(DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME));
|
||||||
List<CompletableFuture<Message>> firstMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION, targetServerId);
|
List<CompletableFuture<Message>> firstMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION, targetServerId);
|
||||||
List<CompletableFuture<Message>> secondMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2, targetServerId);
|
List<CompletableFuture<Message>> secondMessage = postTargetService.sendEmbedInPostTarget(messageToSend, DebraPostTarget.DEBRA_DONATION_NOTIFICATION2, targetServerId);
|
||||||
firstMessage.addAll(secondMessage);
|
firstMessage.addAll(secondMessage);
|
||||||
|
|||||||
@@ -6,3 +6,6 @@ abstracto.postTargets.debraDonationNotification2.name=debraDonationNotification2
|
|||||||
|
|
||||||
sissi.debra.websocketURL=ws://spenden.baba.fm:8765/
|
sissi.debra.websocketURL=ws://spenden.baba.fm:8765/
|
||||||
sissi.debra.donationsPageURL=https://em.altruja.de/discord-fuer-debra-2022
|
sissi.debra.donationsPageURL=https://em.altruja.de/discord-fuer-debra-2022
|
||||||
|
|
||||||
|
abstracto.systemConfigs.debraDonationNotificationDelayMillis.name=debraDonationNotificationDelayMillis
|
||||||
|
abstracto.systemConfigs.debraDonationNotificationDelayMillis.longValue=60000
|
||||||
@@ -31,4 +31,4 @@ DEBRA_DONATION_NOTIFICATION_SERVER_ID=0
|
|||||||
PGADMIN_DEFAULT_PASSWORD=admin
|
PGADMIN_DEFAULT_PASSWORD=admin
|
||||||
TOKEN=<INSERT TOKEN>
|
TOKEN=<INSERT TOKEN>
|
||||||
YOUTUBE_API_KEY=<INSERT KEY>
|
YOUTUBE_API_KEY=<INSERT KEY>
|
||||||
SISSI_VERSION=1.3.12
|
SISSI_VERSION=1.3.13
|
||||||
4
pom.xml
4
pom.xml
@@ -20,8 +20,8 @@
|
|||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<!-- edit in release.yml as well -->
|
<!-- edit in release.yml as well -->
|
||||||
<!-- when releasing a new bot version, update the .env as well-->
|
<!-- when releasing a new bot version, update the .env as well-->
|
||||||
<abstracto.version>1.4.13</abstracto.version>
|
<abstracto.version>1.4.14</abstracto.version>
|
||||||
<abstracto.templates.version>1.4.8</abstracto.templates.version>
|
<abstracto.templates.version>1.4.9</abstracto.templates.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
|
|||||||
Reference in New Issue
Block a user