Use env variables for determining shard count/index

Its a really messy way, but k8s is dumb
This commit is contained in:
NovaFox161
2021-06-14 23:21:25 -05:00
parent 0809f9eba1
commit e31f116f41
7 changed files with 54 additions and 20 deletions

View File

@@ -4,9 +4,9 @@ import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Guild;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.rest.util.Image;
import org.dreamexposure.discal.Application;
import org.dreamexposure.discal.client.message.Messages;
import org.dreamexposure.discal.core.database.DatabaseManager;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.object.GuildSettings;
import org.dreamexposure.discal.core.object.command.CommandInfo;
import org.dreamexposure.discal.core.utils.ChannelUtils;
@@ -144,7 +144,7 @@ public class DisCalCommand implements Command {
spec.addField(Messages.getMessage("Embed.DisCal.Info.Developer", settings), "DreamExposure", true);
spec.addField(Messages.getMessage("Embed.Discal.Info.Version", settings), GlobalConst.version, true);
spec.addField(Messages.getMessage("Embed.DisCal.Info.Library", settings), GlobalConst.d4jVersion, false);
spec.addField("Shard Index", BotSettings.SHARD_INDEX.get() + "/" + BotSettings.SHARD_COUNT.get(), true);
spec.addField("Shard Index", Application.getShardIndex() + "/" + Application.getShardCount(), true);
spec.addField(Messages.getMessage("Embed.DisCal.Info.TotalGuilds", settings), guilds, true);
spec.addField(Messages.getMessage("Embed.DisCal.Info.TotalCalendars", settings), calendars, true);
spec.addField(Messages.getMessage("Embed.DisCal.Info.TotalAnnouncements", settings), announcements, true);

View File

@@ -2,9 +2,9 @@ package org.dreamexposure.discal.client.module.misc;
import discord4j.core.object.presence.Activity;
import discord4j.core.object.presence.Presence;
import org.dreamexposure.discal.Application;
import org.dreamexposure.discal.client.DisCalClient;
import org.dreamexposure.discal.core.database.DatabaseManager;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.utils.GlobalConst;
import java.util.ArrayList;
@@ -45,7 +45,7 @@ public class StatusChanger extends TimerTask {
status = status.replace("%guCount%", DisCalClient.getClient().getGuilds().count().block() + "");
status = status.replace("%calCount%", DatabaseManager.INSTANCE.getCalendarCount().block() + "");
status = status.replace("%annCount%", DatabaseManager.INSTANCE.getAnnouncementCount().block() + "");
status = status.replace("%shards%", BotSettings.SHARD_COUNT.get());
status = status.replace("%shards%", Application.getShardCount() + "");
DisCalClient.getClient().updatePresence(Presence.online(Activity.playing(status))).subscribe();
//Set new index.

View File

@@ -3,6 +3,7 @@ package org.dreamexposure.discal.client.service;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.dreamexposure.discal.Application;
import org.dreamexposure.discal.client.DisCalClient;
import org.dreamexposure.discal.core.logger.LogFeed;
import org.dreamexposure.discal.core.logger.object.LogObject;
@@ -35,7 +36,7 @@ public class KeepAliveHandler {
public void run() {
try {
final JSONObject data = new JSONObject();
data.put("index", Integer.parseInt(BotSettings.SHARD_INDEX.get()));
data.put("index", Application.getShardIndex());
if (DisCalClient.getClient() != null)
data.put("guilds", DisCalClient.getClient().getGuilds().count().block());

View File

@@ -157,8 +157,8 @@ class DisCalClient {
private fun getStrategy(): ShardingStrategy {
return ShardingStrategy.builder()
.count(BotSettings.SHARD_COUNT.get().toInt())
.indices(BotSettings.SHARD_INDEX.get().toInt())
.count(Application.getShardCount())
.indices(Application.getShardIndex().toInt())
.build()
}

View File

@@ -1,8 +1,11 @@
package org.dreamexposure.discal.core.logger.loggers;
import club.minnced.discord.webhook.WebhookClient;
import club.minnced.discord.webhook.send.WebhookEmbed;
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
import org.dreamexposure.discal.Application;
import org.dreamexposure.discal.core.logger.interfaces.Logger;
import org.dreamexposure.discal.core.logger.object.LogObject;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.utils.GlobalConst;
import java.io.IOException;
@@ -10,10 +13,6 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import club.minnced.discord.webhook.WebhookClient;
import club.minnced.discord.webhook.send.WebhookEmbed;
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
@SuppressWarnings("MagicNumber")
public class DiscordLogger implements Logger {
private final WebhookClient debugClient;
@@ -47,7 +46,7 @@ public class DiscordLogger implements Logger {
final WebhookEmbedBuilder builder = new WebhookEmbedBuilder()
.setTitle(new WebhookEmbed.EmbedTitle("Status", null))
.addField(new WebhookEmbed
.EmbedField(true, "Shard Index", BotSettings.SHARD_INDEX.get()))
.EmbedField(true, "Shard Index", Application.getShardIndex()))
.addField(new WebhookEmbed
.EmbedField(false, "Time", log.getTimestamp()))
.setDescription(log.getMessage())
@@ -66,7 +65,7 @@ public class DiscordLogger implements Logger {
final WebhookEmbedBuilder builder = new WebhookEmbedBuilder()
.setTitle(new WebhookEmbed.EmbedTitle("Debug", null))
.addField(new WebhookEmbed
.EmbedField(true, "Shard Index", BotSettings.SHARD_INDEX.get()))
.EmbedField(true, "Shard Index", Application.getShardIndex()))
.addField(new WebhookEmbed
.EmbedField(false, "Time", log.getTimestamp()))
.setDescription(log.getMessage())
@@ -99,7 +98,7 @@ public class DiscordLogger implements Logger {
final WebhookEmbedBuilder builder = new WebhookEmbedBuilder()
.setTitle(new WebhookEmbed.EmbedTitle("Exception", null))
.addField(new WebhookEmbed
.EmbedField(true, "Shard Index", BotSettings.SHARD_INDEX.get()))
.EmbedField(true, "Shard Index", Application.getShardIndex()))
.addField(new WebhookEmbed
.EmbedField(false, "Class", log.getClazz().getName()))
.addField(new WebhookEmbed

View File

@@ -1,8 +1,7 @@
package org.dreamexposure.discal.core.utils;
import org.dreamexposure.discal.core.object.BotSettings;
import discord4j.common.util.Snowflake;
import org.dreamexposure.discal.Application;
/**
* Created by Nova Fox on 11/6/17.
@@ -12,11 +11,11 @@ import discord4j.common.util.Snowflake;
public class GuildUtils {
@SuppressWarnings("MagicNumber")
public static int findShard(final Snowflake id) {
return ((int) id.asLong() >> 22) % Integer.parseInt(BotSettings.SHARD_COUNT.get());
return ((int) id.asLong() >> 22) % Application.getShardCount();
}
public static boolean isActive(final Snowflake id) {
//TODO: Determine an accurate way to detect if a guild is still connected to DisCal
return true;
}
}
}

View File

@@ -1,7 +1,42 @@
package org.dreamexposure.discal
import org.dreamexposure.discal.core.`object`.BotSettings
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
@SpringBootApplication(exclude = [SessionAutoConfiguration::class])
class Application
class Application {
companion object {
@JvmStatic
fun getShardIndex(): String {
/*
This fucking sucks. So k8s doesn't expose the pod ordinal for a pod in a stateful set
https://github.com/kubernetes/kubernetes/pull/68719
This has been an open issue and PR for over 3 years now, and has gone stale as of March 3rd 2021.
So, in order to get the pod ordinal since its not directly exposed, we have to get the hostname, and parse
the ordinal out of that.
To make sure we don't use this when running anywhere but inside of k8s, we are mapping the hostname to an env
variable SHARD_POD_NAME and if that is present, parsing it for the pod ordinal to tell the bot its shard index.
This will be removed when/if they add this feature directly and SHARD_INDEX will be an env variable...
*/
//Check if we are running in k8s or not...
val shardPodName = System.getenv("SHARD_POD_NAME")
return if (shardPodName != null) {
//In k8s, parse this shit
val parts = shardPodName.split("-")
parts[parts.size -1]
} else {
//Fall back to config value
BotSettings.SHARD_INDEX.get()
}
}
@JvmStatic
fun getShardCount(): Int {
val shardCount = System.getenv("SHARD_COUNT")
return shardCount?.toInt() ?: //Fall back to config
BotSettings.SHARD_COUNT.get().toInt()
}
}
}