API Timeout from Mongo

andremaytorenaPRO

a year ago

I have deployed a new version to my railway server, I didn't change anything related to my mongodb connection yet now my server is crashing and it is displaying this error, i am using Gunicorn with python flask, it runs perfectly on local with the same command as the one in the server

0 Replies

andremaytorenaPRO

a year ago


andremaytorenaPRO

a year ago

project id: 4cc29972-e583-4641-81d0-26868fcc02e7


andremaytorenaPRO

a year ago

even deploying an old version that used to work gives the same error


a year ago

is this a mongodb hosted on railway, or elsewhere, like atlas


andremaytorenaPRO

a year ago

atlas


a year ago

have you whitelisted all ip ranges? 0.0.0.0/0


andremaytorenaPRO

a year ago

yeah


andremaytorenaPRO

a year ago

ill delete it and rewhitelist to see if it helps tho


a year ago

please send a screenshot of that setting too


andremaytorenaPRO

a year ago


a year ago

is that 0.0.0.0/0 network access rule set on the correct database?


andremaytorenaPRO

a year ago

yeah its the only one i have


a year ago

okay, can you show me the client code that connects to the database


andremaytorenaPRO

a year ago

from pymongo import MongoClient
import certifi
from models.config import MONGO_DB_URL

class MongoDB:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)

            cls._instance.client = MongoClient(MONGO_DB_URL)
            cls._instance.db = cls._instance.client["bookitspot"]
        return cls._instance.db

db = MongoDB()

andremaytorenaPRO

a year ago

MONGO_DB_URL = "mongodb+srv://username:password@cluster/?retryWrites=true&w=majority"


a year ago

and where do you have that service variable set?


andremaytorenaPRO

a year ago

the connection url?


andremaytorenaPRO

a year ago

or the actual db variable?


a year ago

both


andremaytorenaPRO

a year ago

my root folder is called v2

the connection url is inside: v2 -> models -> config.py

the db variable is set inside -> v2 -> models -> database -> db_connection.py

the code i sent for the connection is the whole db_connection.py file

this is how I grab that variable to use:

from models.database.db_connection import db


a year ago

so you are hardcoding the url?


andremaytorenaPRO

a year ago

yeah, how else would I do it?


a year ago

set it as a service variable
then read the variable from the environment in code


andremaytorenaPRO

a year ago

ok ill try


andremaytorenaPRO

a year ago

by any chance do you know how I would use it inside the code? ive never used smth like this and the example is for node js


a year ago

i saw that lol


andremaytorenaPRO

a year ago

just realized how lol


a year ago

always a quick google away! 🙂


andremaytorenaPRO

a year ago

indeed


andremaytorenaPRO

a year ago

ok just tried and getting the same error


andremaytorenaPRO

a year ago

from pymongo import MongoClient
import certifi
# from models.config import MONGO_DB_URL
import os

class MongoDB:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)

            db_url = os.environ['DB_URL']
            cls._instance.client = MongoClient(db_url)

            cls._instance.db = cls._instance.client["bookitspot"]
        return cls._instance.db

db = MongoDB()

a year ago

you have set it as a service variable?


andremaytorenaPRO

a year ago

yeah


andremaytorenaPRO

a year ago


a year ago

please do not use a shared variable for this


andremaytorenaPRO

a year ago

ok i think ive set it as a service var now


andremaytorenaPRO

a year ago

i reset the server and still error


a year ago

show me the service variables please


andremaytorenaPRO

a year ago

looks the same


andremaytorenaPRO

a year ago


a year ago

and you are sure that is set correctly?


andremaytorenaPRO

a year ago

i pressed on new variable and pasted my url


a year ago

why not just run mongo on railway?


andremaytorenaPRO

a year ago

idk this is just how I set it up before I even set up the server


andremaytorenaPRO

a year ago

but it was working all the time, until today, my server was even running fine in the morning with the same setup


a year ago

my current thought process is that atlas has blocked the shared ip that you are trying to connect from


andremaytorenaPRO

a year ago

usually i get a different error when it doesnt allow the IP


a year ago

im not talking about the access rules


andremaytorenaPRO

a year ago

ah


a year ago

would you be interested in running a mongo database on railway?


andremaytorenaPRO

a year ago

hmm probably not, as i already have this setup


a year ago

unfortunaly it doesn’t seem like its going to work out


andremaytorenaPRO

a year ago

i feel like there has to be a way to make it work again, since its been working for over a month


a year ago

you could get in contact with atlas


andremaytorenaPRO

a year ago

ok yh ive opened a ticket with them to see what they say, its so strange tbh as there is nothing online abt the error too


andremaytorenaPRO

a year ago

hey i did some more testing with different versions of packages


andremaytorenaPRO

a year ago

and i think it's due to my dnspython package version


andremaytorenaPRO

a year ago

Locally im using version 2.4.2, but even on local when I update it to latest version it gives me the same error


andremaytorenaPRO

a year ago

Now, I set the version for dnspython==2.4.2 on my requiremenets.txt file but when I check the build logs on the server it's installing the latest version


andremaytorenaPRO

a year ago

10 16.45 Successfully installed Flask-3.0.0 Flask-Cors-4.0.0 Jinja2-3.1.2 MarkupSafe-2.1.3 PyJWT-2.8.0 Werkzeug-3.0.1 anyio-4.3.0 bcrypt-4.0.1 bidict-0.23.1 blinker-1.7.0 certifi-2023.7.22 cffi-1.16.0 charset-normalizer-3.3.2 click-8.1.7 cloudinary-1.38.0 cryptography-41.0.5 dnspython-2.6.1 eventlet-0.34.2 exceptiongroup-1.2.0 flask_socketio-5.3.6 gevent-24.2.1 gevent-websocket-0.10.1 greenlet-3.0.3 gunicorn-21.2.0 h11-0.14.0 h2-4.1.0 hpack-4.0.0 httpcore-1.0.4 httpx-0.26.0 hyperframe-6.0.1 idna-3.4 importlib-metadata-7.0.1 itsdangerous-2.1.2 packaging-23.2 pycparser-2.21 pymongo-4.6.0 python-dateutil-2.8.2 python-engineio-4.9.0 python-socketio-5.11.1 requests-2.31.0 simple-websocket-1.0.0 six-1.16.0 sniffio-1.3.1 typing-extensions-4.10.0 urllib3-2.0.7 wsproto-1.2.0 zipp-3.17.0 zope.event-5.0 zope.interface-6.2


andremaytorenaPRO

a year ago

its installing dnspython-2.6.1 which I don't want


andremaytorenaPRO

a year ago

How can I change the version since I thought it pulled from the requierements file?


a year ago

by specifying the wanted version in your requirements.txt


andremaytorenaPRO

a year ago

this is what i have there:
dnspython==2.4.2


andremaytorenaPRO

a year ago

but its not installing that version


a year ago

you have a different version set elsewhere


a year ago

or you haven't pushed those changes


andremaytorenaPRO

a year ago

thats crazy


andremaytorenaPRO

a year ago

it works now


andremaytorenaPRO

a year ago

it was legit just the version of that


andremaytorenaPRO

a year ago

thank you a lot for your time


a year ago

interesting, well my apologies for sending you in the wrong direction, it was really looking like an IP ban from atlas


andremaytorenaPRO

a year ago

dont worry, mongo support couldnt even help aswell


a year ago

I don't think there's anyone who could have helped with such an obscure cause of this issue


andremaytorenaPRO

a year ago

yeah it was weird


a year ago

but happy you found a solution!


API Timeout from Mongo - Railway Help Station