Database private url not working
anubhav1603
HOBBYOP

2 years 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

62 Replies

anubhav1603
HOBBYOP

2 years ago

wow


anubhav1603
HOBBYOP

2 years ago

it auto detected


anubhav1603
HOBBYOP

2 years ago

dont know why but its not getting connected


anubhav1603
HOBBYOP

2 years ago

my service is down and i want to make it active again : ( urgently


anubhav1603
HOBBYOP

2 years ago

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


anubhav1603
HOBBYOP

2 years ago

i really need to get this service up quickly : (


2 years 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


anubhav1603
HOBBYOP

2 years ago

i am using poetry run start does it will support sleep?


anubhav1603
HOBBYOP

2 years ago

sleep 3 && poetry run start right?


2 years ago

I'm not familiar with poetry sorry so I'm not sure what the command would be.


anubhav1603
HOBBYOP

2 years ago

: (


2 years 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.


anubhav1603
HOBBYOP

2 years ago

ok i just deployed with sleep 3 hoping to work


anubhav1603
HOBBYOP

2 years ago

i have added asyncio.sleep at my connect method


anubhav1603
HOBBYOP

2 years ago

so it waits


anubhav1603
HOBBYOP

2 years ago

hopefully it works


anubhav1603
HOBBYOP

2 years ago

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


anubhav1603
HOBBYOP

2 years ago

not fixed at all


anubhav1603
HOBBYOP

2 years ago

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

anubhav1603
HOBBYOP

2 years ago

my code


anubhav1603
HOBBYOP

2 years ago

: (


2 years 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).


anubhav1603
HOBBYOP

2 years 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()

anubhav1603
HOBBYOP

2 years ago

it does work locally


anubhav1603
HOBBYOP

2 years ago

and works perfectly : (


anubhav1603
HOBBYOP

2 years ago

i am not able to establish connection on railway for some reason


2 years ago

If you print out DevCreds.DEV_SITE_PASSWORD does it print out what you expect?


anubhav1603
HOBBYOP

2 years ago

yep


anubhav1603
HOBBYOP

2 years ago

its another api creds


2 years ago

Also the private url isn't just the password. It's the full connecction string.


anubhav1603
HOBBYOP

2 years ago

i referenced it


2 years ago

Does it work locally if you use the public connection string?


anubhav1603
HOBBYOP

2 years ago

${{Postgres.DATABASE_URL}}


anubhav1603
HOBBYOP

2 years ago

i have not connected to railway db locally , i use local db for bot for testing only for production i use railway db


anubhav1603
HOBBYOP

2 years ago

1272657732406612000


2 years ago

I'd try connecting to your railway db locally. There is a public connection string you can use.


2 years ago

So run your app locally and use the public connection string to connect to that Postgres instance.


anubhav1603
HOBBYOP

2 years ago

ok i will try


anubhav1603
HOBBYOP

2 years ago

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

anubhav1603
HOBBYOP

2 years ago

it connected succsfully


anubhav1603
HOBBYOP

2 years ago

but at railway its different


anubhav1603
HOBBYOP

2 years ago

thats failing there


anubhav1603
HOBBYOP

2 years ago

so locally railway db works but when i try to connect with app it doesnt


anubhav1603
HOBBYOP

2 years ago

@𝐗-𝐥𝐞𝐦
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


2 years ago

The connection string should have the port already? Where are you seeing the port is wrong? In the DB env variables?


anubhav1603
HOBBYOP

2 years ago

in the error


anubhav1603
HOBBYOP

2 years ago

connection string have 40317 port


2 years ago

Check the DB service env variables. That's probably the port it's using.


anubhav1603
HOBBYOP

2 years ago

postgresql://postgres:pass@monorail.proxy.rlwy.net:40317/railway


anubhav1603
HOBBYOP

2 years ago

you are right but when i copy the public url why it has different port?


anubhav1603
HOBBYOP

2 years ago

and i am able to connect with the 40317 port in my program locally on my pc


2 years 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 :/


anubhav1603
HOBBYOP

2 years ago

this is exactly what i tried but it didnt connected on railway but locally with public url it did connect


anubhav1603
HOBBYOP

2 years ago

@𝐗-𝐥𝐞𝐦 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


anubhav1603
HOBBYOP

2 years ago

its connecting now


2 years ago

😛


2 years ago

spelling mistakes kill us all


anubhav1603
HOBBYOP

2 years ago

literally wasted 4h for nothing


2 years ago

Did you not have that spelling mistake locally?


anubhav1603
HOBBYOP

2 years ago

actually suggestion from my IDE


anubhav1603
HOBBYOP

2 years ago

it suggested me wrong as i have created wrong var name at starting


anubhav1603
HOBBYOP

2 years ago

so i didnt noticed


Loading...