Issue with Flask Deployment

Hello, here is my project id: e8d46baa-5de4-495e-bc2e-a8a2b37ed2b0. I am trying to deploy my flask/redis application to railway so that one server is running where 1 port is running the flask api part and another part which is running the redis part. I believe I got the redis part on railway to work, as I can see all my data, but for some reason the flask deployment isnt working as expected. In my main.py, I have this code:
if __name__ == '__main__':
load_data_to_redis()
app.run(host='0.0.0.0', port=int(os.getenv("RAILWAY_PORT", 5000)), debug=True)
if __name__ == '__main__':
load_data_to_redis()
app.run(host='0.0.0.0', port=int(os.getenv("RAILWAY_PORT", 5000)), debug=True)
However, when I run the deployment, I see this in the deploy logs:
* Serving Flask app 'main'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.225:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 122-734-501
* Serving Flask app 'main'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.225:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 122-734-501
For some reason, it's running locally instead of the railway url and it gives this notification/popup above the logs saying "Looks like your app is listening on 127.0.0.1. You may need to listen on 0.0.0.0 instead." even though I am listening on 0.0.0.0. I can provide more of what's in my main.py if needed. I'm not sure what I am doing wrong so would really appreciate it if someone could take a look. Much appreciated!
25 Replies
Percy
Percy2y ago
Project ID: e8d46baa-5de4-495e-bc2e-a8a2b37ed2b0
Adam
Adam2y ago
It’s only possible to expose one port per service on Railway You should put your redis in a different service And judging by your logs, it does seem like something is running on 127.0.0.1. Once you split the services out it’ll be easier to debug
Brody
Brody2y ago
you should also be using gunicorn
This is a development server. Do not use it
krypton201
krypton201OP2y ago
So there's no way to run redis as a subprocess under the main flask process on a separate port? I have a function that starts the redis server:
def start_redis_server():
print("test")
redis_process = subprocess.Popen(['redis-server'])
def start_redis_server():
print("test")
redis_process = subprocess.Popen(['redis-server'])
and I call that function in
if __name__ == '__main__
if __name__ == '__main__
` but it doesn't seem to be even running anything in if name = main. When I deploy to railway, I get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'redis-server'
FileNotFoundError: [Errno 2] No such file or directory: 'redis-server'
Brody
Brody2y ago
you will want to use the redis plugin railway provides
MantisInABox
MantisInABox2y ago
The port for your flask server should also be os.getenv(“PORT”) not RAILWAY_PORT
krypton201
krypton201OP2y ago
Thanks that fixed it. Last question: is there anyway to change the value of REDIS_PASSWORD? I hard coded the value in my main.py file instead of using .env variables and accidently pushed to github (its in a private repo so its fine for now).
Brody
Brody2y ago
you can regenerate your redis password in the redis plugin's settings
MantisInABox
MantisInABox2y ago
Really?
Brody
Brody2y ago
yes!
MantisInABox
MantisInABox2y ago
I don’t use redis, so I had no idea I don’t like redis
Brody
Brody2y ago
you can do it in any database railway provides
MantisInABox
MantisInABox2y ago
Never looked into the settings honestly. I just create, use, and tear down when I’m done. But good to know
krypton201
krypton201OP2y ago
ah got it, I was looking in the connect section instead of settings. thanks
Brody
Brody2y ago
please make sure you are using variable references not a .env file
krypton201
krypton201OP2y ago
like add the variables directly on the railway dashboard?
krypton201
krypton201OP2y ago
ah ok what's wrong with using a .env file?
Brody
Brody2y ago
storing credentials for a public facing database in plaintext is not a good idea in my opinion keep all secrets in the railway service, and then use the railway cli with railway run <the command you normally use to start your app> this way your app has access to the railway service variables without you ever having to copy your secrets to a .env file
krypton201
krypton201OP2y ago
got it wow that was much easier than i expected, thx @Brody
Brody
Brody2y ago
sanity check: show me a screenshot of your service variables?
krypton201
krypton201OP2y ago
Brody
Brody2y ago
looks good to me and now you dont have to put secrets in plaintext! pretty neat right?
krypton201
krypton201OP2y ago
I have another question. I am trying to use the RediSearch module (https://redis.io/docs/stack/search/) to perform a query against the redis storage. However, when I run my code, I get this error:
redis.exceptions.ResponseError: unknown command `FT.SEARCH`, with args beginning with: `courses_index`, `operating systems`, `VERBATIM`, `LIMIT`, `0`, `10`,
redis.exceptions.ResponseError: unknown command `FT.SEARCH`, with args beginning with: `courses_index`, `operating systems`, `VERBATIM`, `LIMIT`, `0`, `10`,
in the railway api console which seems to occur when the redisearch library is not properly installed. I have RediSearch in my requirements.txt file, so any help would be appreciated.
Redis
RediSearch
Queries, secondary indexing, and full-text search for Redis
Brody
Brody2y ago
the redis database doesn't have the redisearch module installed, this has nothing to do with whats in your requirements.txt. you cant install modules yourself so if you need rediseach you will have to use a different database host
Want results from more Discord servers?
Add your server