Skip to content

Commit

Permalink
close resources on application stop
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 19, 2024
1 parent aba5269 commit 9489768
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
24 changes: 22 additions & 2 deletions src/main/java/vc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PreDestroy;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -38,6 +39,8 @@ public class Application {
@Value("${BOT_TOKEN}")
String token;
private static final Logger LOGGER = getLogger("Application");
private GatewayDiscordClient gatewayDiscordClient;
private ScheduledExecutorService scheduledExecutorService;

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
Expand All @@ -54,12 +57,13 @@ public ClientHttpRequestFactory clientHttpRequestFactory() {

@Bean
public GatewayDiscordClient gatewayDiscordClient() {
return DiscordClientBuilder.create(token).build()
this.gatewayDiscordClient = DiscordClientBuilder.create(token).build()
.gateway()
.setEnabledIntents(IntentSet.of(Intent.GUILDS))
.setInitialPresence(ignore -> ClientPresence.of(Status.ONLINE, ClientActivity.custom("/commands")))
.login()
.block();
return this.gatewayDiscordClient;
}

@Bean
Expand Down Expand Up @@ -91,12 +95,28 @@ public ObjectMapper objectMapper() {
mapper.registerModule(new JsonNullableModule());
return mapper;
}

@Bean
public ScheduledExecutorService scheduledExecutorService() {
return Executors.newScheduledThreadPool(4, new ThreadFactoryBuilder()
this.scheduledExecutorService = Executors.newScheduledThreadPool(4, new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("scheduled-%d")
.setUncaughtExceptionHandler((t, e) -> LOGGER.error("Uncaught exception in scheduled thread: {}", t.getName(), e))
.build());
return this.scheduledExecutorService;
}

@PreDestroy
public void onDestroy() {
try {
if (this.gatewayDiscordClient != null) {
this.gatewayDiscordClient.logout().block();
}
if (this.scheduledExecutorService != null) {
this.scheduledExecutorService.shutdownNow();
}
} catch (Exception e) {
LOGGER.error("Error during shutdown", e);
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/vc/config/GuildConfigDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -20,7 +21,7 @@
import java.util.Optional;

@Component
public class GuildConfigDatabase {
public class GuildConfigDatabase implements DisposableBean {
private static final Logger LOGGER = LoggerFactory.getLogger(GuildConfigDatabase.class);
// backups older than this date will be deleted
private static final Duration ROLLING_BACKUP_DURATION = Duration.ofDays(7);
Expand Down Expand Up @@ -48,10 +49,10 @@ public GuildConfigDatabase(
LOGGER.error("Error initializing guild config database connection", e);
throw new RuntimeException(e);
}
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
}

private void close() {
@Override
public void destroy() {
try {
connection.close();
} catch (final Exception e) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/vc/config/RemoteDatabaseBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -13,7 +14,7 @@
import java.util.stream.StreamSupport;

@Component
public class RemoteDatabaseBackup {
public class RemoteDatabaseBackup implements DisposableBean {
private static final Logger LOGGER = LoggerFactory.getLogger("RemoteDatabaseBackup");
private final String bucketName;
private final MinioClient minioClient;
Expand Down Expand Up @@ -105,4 +106,13 @@ public void downloadDatabaseBackup(final String backupPath) {
throw new RuntimeException(e);
}
}

@Override
public void destroy() {
try {
minioClient.close();
} catch (final Exception e) {
LOGGER.error("Error closing Minio client", e);
}
}
}
15 changes: 14 additions & 1 deletion src/main/java/vc/live/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import org.redisson.api.RBoundedBlockingQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RedisClient {
public class RedisClient implements DisposableBean {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisClient.class);
private RedissonClient redissonClient;

public RedisClient(@Value("${REDIS_URL}") final String redisURL, @Value("${REDIS_USERNAME}") final String redisUsername, @Value("${REDIS_PASSWORD}") final String redisPassword) {
Expand All @@ -31,4 +35,13 @@ public RedissonClient buildRedisClient(final String redisURL, final String redis
.setConnectionMinimumIdleSize(1);
return Redisson.create(config);
}

@Override
public void destroy() {
try {
redissonClient.shutdown();
} catch (Exception e) {
LOGGER.error("Failed to shutdown Redisson client", e);
}
}
}

0 comments on commit 9489768

Please sign in to comment.