10 months ago
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:
container event container died
0 Replies
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
10 months ago
@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
10 months ago
I'm not familiar with poetry sorry so I'm not sure what the command would be.
10 months 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.
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
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
10 months ago
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).
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()
10 months ago
If you print out [DevCreds.DEV](DevCreds.DEV)_SITE_PASSWORD
does it print out what you expect?
10 months ago
Also the private url isn't just the password. It's the full connecction string.
10 months ago
Does it work locally if you use the public connection string?
i have not connected to railway db locally , i use local db for bot for testing only for production i use railway db
10 months ago
I'd try connecting to your railway db locally. There is a public connection string you can use.
10 months ago
So run your app locally and use the public connection string to connect to that Postgres instance.
2024-08-13 02:18:24 INFO discord.client logging in using static token
2024-08-13 02:19:04.516 | INFO | afw_bot.ext.utils.database:connect:12 - 2024-08-13 02:19:04 DATABASE Successfully connected
so locally railway db works but when i try to connect with app it doesnt
@𝐗-𝐥𝐞𝐦
2024-08-12 21:08:18.540 | WARNING | afw_bot.ext.utils.database:connect:14 - 2024-08-12 21:08:18 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 changed to public url which is not 5432 still it showing this only
port should be 40317
10 months ago
The connection string should have the port already? Where are you seeing the port is wrong? In the DB env variables?
10 months ago
Check the DB service env variables. That's probably the port it's using.
you are right but when i copy the public url why it has different port?
and i am able to connect with the 40317 port in my program locally on my pc
10 months ago
I'm assuming one's the internal port and one's the external port.
If your AFW-bot has an env called Core.DATABSE_URL
and it's set to ${{Postgres.DATABASE_URL}}
then it should work. I'm not sure what else the issue would be :/
this is exactly what i tried but it didnt connected on railway but locally with public url it did connect
@𝐗-𝐥𝐞𝐦 i am very sorry i wasted your time and i seriously apologize for this , being a absolute stupid person i mispelled a letter in my actual code DATBSEURL and tried to connect with DATABASEURL
10 months ago
😛
10 months ago
spelling mistakes kill us all
10 months ago
Did you not have that spelling mistake locally?