Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add /verifydb command #43

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/discord-cluster-manager/cogs/misc_cog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import discord
from discord import app_commands
from discord.ext import commands
import psycopg2
from utils import setup_logging
from consts import DATABASE_URL

logger = setup_logging()

Expand Down Expand Up @@ -36,3 +38,27 @@ async def resync(self, interaction: discord.Interaction):
await interaction.response.send_message(
"You need administrator permissions to use this command"
)

@app_commands.command(name="verifydb")
async def verify_db(self, interaction: discord.Interaction):
"""Command to verify database connectivity"""
if not DATABASE_URL:
message = "DATABASE_URL not set."
logger.error(message)
await interaction.response.send_message(message)
return

try:
with psycopg2.connect(DATABASE_URL, sslmode='require') as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT RANDOM()")
result = cursor.fetchone()
if result:
random_value = result[0]
await interaction.response.send_message(f"Your lucky number is {random_value}.")
else:
await interaction.response.send_message("No result returned.")
except Exception as e:
message = "Error interacting with the database"
logger.error(f"{message}: {str(e)}", exc_info=True)
await interaction.response.send_message(f"{message}.")
1 change: 1 addition & 0 deletions src/discord-cluster-manager/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ class ModalGPU(Enum):
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably won't need the POSTGRES_* constants, but I wanted to leave them for now in order to verify we can connect to the database on Heroku. Once we verify that, we should be able to remove these constants.

DATABASE_URL = os.getenv("DATABASE_URL")
Loading