R
Railway2mo ago
Anubhav

Database private url not working

Mounting volume on: /var/lib/containers/railwayapp/bind-mounts/2ff8f070-ab0c-4ee7-9676-2d3b36a36e0f/vol_8chdamizrj575fwk

Starting Container

[2024-08-12 19:18:19] [INFO ] discord.client: logging in using static token

2024-08-12 19:18:40.255 | WARNING | afw_bot.ext.utils.database:connect:15 - 2024-08-12 19:18:40 Failed to connect to the database: Multiple exceptions: [Errno 111] Connect call failed ('::1', 5432, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 5432)

Traceback (most recent call last):

File "/app/afw_bot/main.py", line 155, in run

super().run(*args, **kwargs)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 869, in run

asyncio.run(runner())

File "/root/.nix-profile/lib/python3.11/asyncio/runners.py", line 190, in run

return runner.run(main)

^^^^^^^^^^^^^^^^

File "/root/.nix-profile/lib/python3.11/asyncio/runners.py", line 118, in run

return self._loop.run_until_complete(task)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/root/.nix-profile/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete

return future.result()

^^^^^^^^^^^^^^^

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 858, in runner

await self.start(token, reconnect=reconnect)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 786, in start

await self.login(token)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 629, in login

await self.setup_hook()

File "/app/afw_bot/main.py", line 59, in setup_hook

await self.db.create_tables()

File "/app/afw_bot/ext/utils/database.py", line 22, in create_tables

raise RuntimeError("Database pool is not initialized")

RuntimeError: Database pool is not initialized

2024-08-12 19:18:40.284 | CRITICAL | afw_bot.main:run:158 - 2024-08-12 19:18:40 Login Failed

Reason: Database pool is not initialized

Unclosed client session

client_session: <aiohttp.client.ClientSession object at 0x7fab710c29d0>

container event container died
Mounting volume on: /var/lib/containers/railwayapp/bind-mounts/2ff8f070-ab0c-4ee7-9676-2d3b36a36e0f/vol_8chdamizrj575fwk

Starting Container

[2024-08-12 19:18:19] [INFO ] discord.client: logging in using static token

2024-08-12 19:18:40.255 | WARNING | afw_bot.ext.utils.database:connect:15 - 2024-08-12 19:18:40 Failed to connect to the database: Multiple exceptions: [Errno 111] Connect call failed ('::1', 5432, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 5432)

Traceback (most recent call last):

File "/app/afw_bot/main.py", line 155, in run

super().run(*args, **kwargs)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 869, in run

asyncio.run(runner())

File "/root/.nix-profile/lib/python3.11/asyncio/runners.py", line 190, in run

return runner.run(main)

^^^^^^^^^^^^^^^^

File "/root/.nix-profile/lib/python3.11/asyncio/runners.py", line 118, in run

return self._loop.run_until_complete(task)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/root/.nix-profile/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete

return future.result()

^^^^^^^^^^^^^^^

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 858, in runner

await self.start(token, reconnect=reconnect)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 786, in start

await self.login(token)

File "/root/.cache/pypoetry/virtualenvs/afw-bot-9TtSrW0h-py3.11/lib/python3.11/site-packages/discord/client.py", line 629, in login

await self.setup_hook()

File "/app/afw_bot/main.py", line 59, in setup_hook

await self.db.create_tables()

File "/app/afw_bot/ext/utils/database.py", line 22, in create_tables

raise RuntimeError("Database pool is not initialized")

RuntimeError: Database pool is not initialized

2024-08-12 19:18:40.284 | CRITICAL | afw_bot.main:run:158 - 2024-08-12 19:18:40 Login Failed

Reason: Database pool is not initialized

Unclosed client session

client_session: <aiohttp.client.ClientSession object at 0x7fab710c29d0>

container event container died
29 Replies
Percy
Percy2mo ago
Project ID: 2ff8f070-ab0c-4ee7-9676-2d3b36a36e0f
Anubhav
Anubhav2mo ago
wow it auto detected dont know why but its not getting connected my service is down and i want to make it active again : ( urgently
import asyncpg

from afw_bot.ext.utils.loggers import log_message


class Database:
def __init__(self, dsn):
self.dsn = dsn

async def connect(self):
try:
self.pool = await asyncpg.create_pool(dsn=self.dsn)
log_message(log_level="INFO", message="DATABASE Successfully connected")
except Exception as e:
log_message(
log_level="WARNING", message=f"Failed to connect to the database: {e}"
)
self.pool = None # Ensure pool is set to None on failure

async def create_tables(self):
if not self.pool:
raise RuntimeError("Database pool is not initialized")
async with self.pool.acquire() as conn:
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS audit (
thread_id BIGINT PRIMARY KEY,
clan_tag TEXT UNIQUE NOT NULL,
war_status TEXT NOT NULL,
applicant_id BIGINT NOT NULL,
war_end_timestamp BIGINT NOT NULL
);
"""
)

async def close(self):
if self.pool:
await self.pool.close()
import asyncpg

from afw_bot.ext.utils.loggers import log_message


class Database:
def __init__(self, dsn):
self.dsn = dsn

async def connect(self):
try:
self.pool = await asyncpg.create_pool(dsn=self.dsn)
log_message(log_level="INFO", message="DATABASE Successfully connected")
except Exception as e:
log_message(
log_level="WARNING", message=f"Failed to connect to the database: {e}"
)
self.pool = None # Ensure pool is set to None on failure

async def create_tables(self):
if not self.pool:
raise RuntimeError("Database pool is not initialized")
async with self.pool.acquire() as conn:
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS audit (
thread_id BIGINT PRIMARY KEY,
clan_tag TEXT UNIQUE NOT NULL,
war_status TEXT NOT NULL,
applicant_id BIGINT NOT NULL,
war_end_timestamp BIGINT NOT NULL
);
"""
)

async def close(self):
if self.pool:
await self.pool.close()
this is how i am connecting to db i really need to get this service up quickly : (
𝐗-𝐥𝐞𝐦
@Anubhav Make sure you're giving enough time for the private network to initialize.
Currently, private networks take up to 3 seconds to initialize on deploy. ... If you experience errors like those above, consider implementing a sleep or other wait mechanism in your app, before attempting to connect.
https://docs.railway.app/guides/private-networking#initialization-time
Anubhav
Anubhav2mo ago
i am using poetry run start does it will support sleep? sleep 3 && poetry run start right?
𝐗-𝐥𝐞𝐦
I'm not familiar with poetry sorry so I'm not sure what the command would be.
Anubhav
Anubhav2mo ago
: (
𝐗-𝐥𝐞𝐦
In my program I just check to see if the DB connection failed and then try again in 5 seconds. That fixed it for me.
Anubhav
Anubhav2mo ago
ok i just deployed with sleep 3 hoping to work i have added asyncio.sleep at my connect method so it waits hopefully it works
2024-08-12 20:37:38.877 | WARNING | afw_bot.ext.utils.database:connect:16 - 2024-08-12 20:37:38 Failed to connect to the database: Multiple exceptions: [Errno 111] Connect call failed ('::1', 5432, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 5432)
2024-08-12 20:37:38.877 | WARNING | afw_bot.ext.utils.database:connect:16 - 2024-08-12 20:37:38 Failed to connect to the database: Multiple exceptions: [Errno 111] Connect call failed ('::1', 5432, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 5432)
i am still getting same warning not fixed at all
class Database:
def __init__(self, dsn):
self.dsn = dsn

async def connect(self):
await asyncio.sleep(5)
try:
self.pool = await asyncpg.create_pool(dsn=self.dsn)
log_message(log_level="INFO", message="DATABASE Successfully connected")
except Exception as e:
log_message(
log_level="WARNING", message=f"Failed to connect to the database: {e}"
)
self.pool = None # Ensure pool is set to None on failure
class Database:
def __init__(self, dsn):
self.dsn = dsn

async def connect(self):
await asyncio.sleep(5)
try:
self.pool = await asyncpg.create_pool(dsn=self.dsn)
log_message(log_level="INFO", message="DATABASE Successfully connected")
except Exception as e:
log_message(
log_level="WARNING", message=f"Failed to connect to the database: {e}"
)
self.pool = None # Ensure pool is set to None on failure
my code : (
𝐗-𝐥𝐞𝐦
Does this work locally? I'm not seeing in your code where you're getting the env variable (I'm assuming you're using an env variable to pass your DB private URL to your application).
Anubhav
Anubhav2mo ago
class MyBot(commands.Bot):

def __init__(self, *, intents: discord.Intents):
super().__init__(
command_prefix="supersecretjutsu",
help_command=None,
intents=intents,
tree_cls=CommandErrorHandler,
)

async def setup_hook(self):
coc_client = coc.EventsClient(key_names=DevCreds.DEV_TOKEN_NAME)
await coc_client.login(
email=DevCreds.DEV_SITE_EMAIL, password=DevCreds.DEV_SITE_PASSWORD
)
self.coc_client: coc.Client = coc_client

self.db = Database(dsn=Core.DATABSE_URL)
await self.db.connect()
class MyBot(commands.Bot):

def __init__(self, *, intents: discord.Intents):
super().__init__(
command_prefix="supersecretjutsu",
help_command=None,
intents=intents,
tree_cls=CommandErrorHandler,
)

async def setup_hook(self):
coc_client = coc.EventsClient(key_names=DevCreds.DEV_TOKEN_NAME)
await coc_client.login(
email=DevCreds.DEV_SITE_EMAIL, password=DevCreds.DEV_SITE_PASSWORD
)
self.coc_client: coc.Client = coc_client

self.db = Database(dsn=Core.DATABSE_URL)
await self.db.connect()
it does work locally and works perfectly : ( i am not able to establish connection on railway for some reason
𝐗-𝐥𝐞𝐦
If you print out DevCreds.DEV_SITE_PASSWORD does it print out what you expect?
Anubhav
Anubhav2mo ago
yep its another api creds
𝐗-𝐥𝐞𝐦
Also the private url isn't just the password. It's the full connecction string.
Anubhav
Anubhav2mo ago
i referenced it
𝐗-𝐥𝐞𝐦
Does it work locally if you use the public connection string?
Want results from more Discord servers?
Add your server