R
Railway4mo 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
Percy4mo ago
Project ID: 2ff8f070-ab0c-4ee7-9676-2d3b36a36e0f
Anubhav
AnubhavOP4mo 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
AnubhavOP4mo 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
AnubhavOP4mo 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
AnubhavOP4mo 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
AnubhavOP4mo 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
AnubhavOP4mo ago
yep its another api creds
𝐗-𝐥𝐞𝐦
Also the private url isn't just the password. It's the full connecction string.
Anubhav
AnubhavOP4mo ago
i referenced it
𝐗-𝐥𝐞𝐦
Does it work locally if you use the public connection string?
Anubhav
AnubhavOP4mo ago
${{Postgres.DATABASE_URL}}
${{Postgres.DATABASE_URL}}
i have not connected to railway db locally , i use local db for bot for testing only for production i use railway db
Anubhav
AnubhavOP4mo ago
No description
𝐗-𝐥𝐞𝐦
I'd try connecting to your railway db locally. There is a public connection string you can use. So run your app locally and use the public connection string to connect to that Postgres instance.
Anubhav
AnubhavOP4mo ago
ok i will try
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
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
it connected succsfully but at railway its different thats failing there 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
𝐗-𝐥𝐞𝐦
The connection string should have the port already? Where are you seeing the port is wrong? In the DB env variables?
Anubhav
AnubhavOP4mo ago
in the error connection string have 40317 port
𝐗-𝐥𝐞𝐦
Check the DB service env variables. That's probably the port it's using.
Anubhav
AnubhavOP4mo ago
postgresql://postgres:[email protected]:40317/railway
postgresql://postgres:[email protected]:40317/railway
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
𝐗-𝐥𝐞𝐦
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 :/
Anubhav
AnubhavOP4mo ago
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 DATBSE_URL and tried to connect with DATABASE_URL its connecting now
𝐗-𝐥𝐞𝐦
😛 spelling mistakes kill us all
Anubhav
AnubhavOP4mo ago
literally wasted 4h for nothing
𝐗-𝐥𝐞𝐦
Did you not have that spelling mistake locally?
Anubhav
AnubhavOP4mo ago
actually suggestion from my IDE it suggested me wrong as i have created wrong var name at starting so i didnt noticed
Want results from more Discord servers?
Add your server