Deployment and logging issues (Maven)
Hello,
My program is running fine locally, but when I deploy it with Railway, I start seeing issues. I think the issue is primarily with grabbing the environment or database variable.
The reason I think it's some database/environment issue is because the logs on Railway says:
So it would appear that this part from main function around the JDA part is having issues:
I have double checked that the TOKEN variable is the same locally and on the Postgres database, so the issue is not the token itself.
I can't seem to figure out what is wrong. And also, how can I use the deploy log to my advantage for debugging? Locally, sometimes i'll just do System.out.println to check variables, etc., but this does not show up in the deploy logs.
72 Replies
Project ID:
e202dece-bdd8-491e-810f-9d28bf4c36e6
e202dece-bdd8-491e-810f-9d28bf4c36e6
Just realized I can use
for both dev and production, and once again, everything works fine locally, so something is going on with System.getenv on Railway
show us your service variables please
Are you Sure this is a correct url?
On some line in your code you check if the string starts with postgres://
Last I checked the one on your picture doesn't seem like it starts with that.
my undersatanding is that
System.getenv("DATABASE_URL") returns ${{Postgres.DATABASE_URL}}, which is pointing to this no?
and given this, it does indeed start with postgresql://
unless my understnding is wrong
It seems like you have double postgres:// in the beginning but it might be wrong from my part
Oh you have a worker and then a seperate service where you define the database.
I think you might need to use shared variables?
interesting, could you explain this? it's my first time on railway
Scratch that. Seems like it would still work the way you have it.
https://docs.railway.app/develop/variables#reference-variables
hi, did you make sure you can connect to your databse and that it's returning something? If it was the environment variable, Java would thrown a connection exception.
You could also set your token in an environment variable to make sure that it's a database connection issue
All I can say for sure is that when I run it locally, without changing the code at all, it all works fine. So it seems that it is possible to connect to the database and retrieve/set things as needed. But once I deploy it to Railway, that's when the ssues pop up
i was just thinking abt trying this
also, instead of parsing the connection string, you can separately set the credentials, make the code a lot cleaner.
you are close, that is called a reference variable, it references the postgres plugins own
DATABASE_URL
variable, but while you see ${{Postgres.DATABASE_URL}}
in the railway website, railway will replace that with the DATABASE_URL
connection string from the database plugin when running your app
do a console log of this please System.getenv("DATABASE_URL")
however, you said you logs are not making it into the deployment logs, so make sure you are logging to stdoutYep, System.out.println() not showing up.
I added some to the code and locally my console is flooded with the database_url (i put it inside the connection function)
i pushed itto Railway, and log is not showing anything
make sure you are logging to stdout
also confirming not working even without databse
private static Connection getConnection() throws SQLException {
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL_ENV"));
return DriverManager.getConnection(dbUrl);
}
send a screenshot of your logs
i'm going to set up Log4J2 for this, give me a moment
ok deploying this version now
nope
trying logger.error as well now, let's see if that works...
why don't you scroll up?
oh this is the bottom? let me check how to scroll up lol
with your scroll wheel?
indeed 💀
the top:
thought you meant the section i was already scrolling was not the actual top of the file somehow, so thought the buttons at the bottom right were the answer, but they're just the scroll wheel as well
hmm checking to see if dependencies are all good, gonna try something
arent there a lot of errors there that you should worry about before you worry about printing a database variable?
thing is i don't get any of those errors locally
yeah but this isn't locally
you need to make sure railway is running as close to as what you run locally
what jdk and gradle versions do you use locally?
what gradle version
switched from idea to vscode recently so checking where i can find it 1 sec
also i'm using maven
don't know what that is
it's basically gradle but not, idk too in depth, just that you can pick either maven or gradle for build
looks like railway will use maven then
okay tell railway to use jdk 19
https://nixpacks.com/docs/providers/java
ok
possible closest thing i found to maven version
dont know the default version of maven railway uses
in intellij, so looks like it's 3.8.1
messed around a bunch with dependencies, stackoverflow solutions, etc. all not working, still getting errors, but only when deployed... local is doing fine
and ofc the token will be invalid still because haven't figured out what is going on with the system.getenv()...
dockerfile time!
eh, i'm unfamiliar with it, any alternatives to railway?
fly
hmm alright i'll try that
thanks for the help
gotta tell me if you are able to deploy it on fly tho
yeah i'll try and do it real quick rn see how far i get
ok went on an adventue for sure... need to sleep too lmao
BUT, having at some point messed around learning dockerfiles, i came back to Railway and now by using the dockerfileinstead, the only error i'm getting is this
as the name suggests, it can't find your jarfile
you can try to get a shell into the container locally to see where that jarfile is in
docker run --entrypoint /bin/sh <image id>
indeed.. currently trying to explicitly define it following this https://docs.railway.app/deploy/dockerfiles
um could you explain this?
i know where the jar file is on my local machine, if i run that it will add something that will tell me where the jarfile is when it's built on Railway?
it may differ the location on a dockerfile. you've docker installed in your machine right? if yes, you can build it
docker build -t my-jda-bot .
and then run the docker image but get a shell instead of actually running it docker run --rm --entrypoint /bin/sh my-jda-bot
why does Railway hate me 🤣 it's right there!
it seems you're trying to run
/target/<...>.jar
isn't that where it's palced by default?
not in this Dockerfile for some reason
i'm lost what do i need to do?
isn't your jar inside /app? then change your Dockerfile to run that
i think it is?
CMD ["java", "-jar", "/app/TokenBot_1_maven-1.0-SNAPSHOT-jar-with-dependencies.jar"]
yeah
why is railway complaining 😭 😭 😭
ok maybe huge development?
why is it not reflecting on railway
i swear i can see the finish line now https://docs.railway.app/deploy/config-as-code
you have the wrong idea, railway won't update the config as code with the CMD from your dockerfile, but I see another issue, your start command that youve set in the service settings is overriding the CMD command in the dockerfile, remove the start command from the service settings
yeah i saw this setting after going the railway.json route
i might delete the railway.json and go fix it up in the settings on railway instead
but happy to report that after all this, it's finally working
i take it your deployment to fly didnt go well?
honestly can't even remember the full adventure i went on... was exploring fly.io and back4app, eventually forced to learn dockerfile somewhere, and for some reason ended up coming back to Railway
but then Railway used my dockerfile and i noticed it was no longer giving those other errors and that's basically where my msgs start
sounds good