R
Railway

✋|help

Deployment and logging issues (Maven)

Bbonjr8/25/2023
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.

private static Connection getConnection() throws SQLException {
        // String dbUrl = toJdbcUrl(readFromFile("DATABASE_URL")); // local testing
        String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL")); // live

        return DriverManager.getConnection(dbUrl);
    }

    private static String toJdbcUrl(String databaseUrl) {
        if (databaseUrl == null || !databaseUrl.startsWith("postgresql://")) {
            return null;
        }

        int protocolEnd = databaseUrl.indexOf("://");
        int credentialsEnd = databaseUrl.lastIndexOf("@");

        String credentials = databaseUrl.substring(protocolEnd + 3, credentialsEnd);
        String[] splitCredentials = credentials.split(":");

        String user = splitCredentials[0];
        String password = splitCredentials[1];

        String afterCredentials = databaseUrl.substring(credentialsEnd + 1);
        String host = afterCredentials.substring(0, afterCredentials.indexOf(":"));
        String portAndDatabase = afterCredentials.substring(afterCredentials.indexOf(":") + 1);

        String jdbcUrl = "jdbc:postgresql://" + host + ":" + portAndDatabase + "?user=" + user + "&password="
                + password;

        return jdbcUrl;
    }


The reason I think it's some database/environment issue is because the logs on Railway says:

Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)


So it would appear that this part from main function around the JDA part is having issues:

// bot
        JDABuilder builder = JDABuilder.createDefault(Utility.readFromDatabase("TOKEN"));


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.

public static String readFromDatabase(String key) {
        String sql = "SELECT value FROM config WHERE key = ?";

        try (Connection conn = getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, key);

            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                return rs.getString("value");
            }
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
        }

        return null;
    }


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.
Bbonjr8/25/2023
e202dece-bdd8-491e-810f-9d28bf4c36e6
Just realized I can use

private static Connection getConnection() throws SQLException {
        String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL"));

        return DriverManager.getConnection(dbUrl);
    }


for both dev and production, and once again, everything works fine locally, so something is going on with System.getenv on Railway
Bbrody1928/25/2023
show us your service variables please
Bbonjr8/25/2023
Mmullet.8/25/2023
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.
Bbonjr8/25/2023
my undersatanding is that

System.getenv("DATABASE_URL") returns ${{Postgres.DATABASE_URL}}, which is pointing to this no?
Bbonjr8/25/2023
Bbonjr8/25/2023
and given this, it does indeed start with postgresql://

unless my understnding is wrong
Mmullet.8/25/2023
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?
Bbonjr8/25/2023
interesting, could you explain this? it's my first time on railway
Mmullet.8/25/2023
Scratch that. Seems like it would still work the way you have it.

https://docs.railway.app/develop/variables#reference-variables
Tthallescomh8/25/2023
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
Bbonjr8/25/2023
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
Tthallescomh8/25/2023
also, instead of parsing the connection string, you can separately set the credentials, make the code a lot cleaner.
Bbrody1928/25/2023
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 stdout
Bbonjr8/25/2023
Yep, 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
Bbrody1928/25/2023
make sure you are logging to stdout
Bbonjr8/25/2023
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);
}
Tthallescomh8/25/2023
send a screenshot of your logs
Bbonjr8/25/2023
i'm going to set up Log4J2 for this, give me a moment
Bbonjr8/25/2023
Bbonjr8/25/2023
ok deploying this version now
Bbonjr8/25/2023
nope
Bbonjr8/25/2023
trying logger.error as well now, let's see if that works...
Bbrody1928/25/2023
why don't you scroll up?
Bbonjr8/25/2023
oh this is the bottom? let me check how to scroll up lol
Bbrody1928/25/2023
with your scroll wheel?
Bbonjr8/25/2023
indeed 💀
Bbonjr8/25/2023
the top:
Bbonjr8/25/2023
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
Bbrody1928/25/2023
arent there a lot of errors there that you should worry about before you worry about printing a database variable?
Bbonjr8/25/2023
thing is i don't get any of those errors locally
Bbrody1928/25/2023
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?
Bbonjr8/25/2023
Bbrody1928/25/2023
what gradle version
Bbonjr8/25/2023
switched from idea to vscode recently so checking where i can find it 1 sec
also i'm using maven
Bbrody1928/25/2023
don't know what that is
Bbonjr8/25/2023
it's basically gradle but not, idk too in depth, just that you can pick either maven or gradle for build
Bbrody1928/25/2023
looks like railway will use maven then
okay tell railway to use jdk 19
https://nixpacks.com/docs/providers/java
Bbonjr8/25/2023
ok
Bbonjr8/25/2023
possible closest thing i found to maven version
Bbrody1928/25/2023
dont know the default version of maven railway uses
Bbonjr8/25/2023
in intellij, so looks like it's 3.8.1
Bbonjr8/25/2023
messed around a bunch with dependencies, stackoverflow solutions, etc. all not working, still getting errors, but only when deployed... local is doing fine


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".

SLF4J: Defaulting to no-operation MDCAdapter implementation.

SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.

Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)


and ofc the token will be invalid still because haven't figured out what is going on with the system.getenv()...
Bbrody1928/25/2023
dockerfile time!
Bbonjr8/25/2023
eh, i'm unfamiliar with it, any alternatives to railway?
Bbrody1928/25/2023
fly
Bbonjr8/25/2023
hmm alright i'll try that
thanks for the help
Bbrody1928/25/2023
gotta tell me if you are able to deploy it on fly tho
Bbonjr8/25/2023
yeah i'll try and do it real quick rn see how far i get
Bbonjr8/25/2023
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
Tthallescomh8/25/2023
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>
Bbonjr8/25/2023
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?
Tthallescomh8/25/2023
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
Bbonjr8/25/2023
Bbonjr8/25/2023
why does Railway hate me 🤣 it's right there!
Tthallescomh8/25/2023
it seems you're trying to run /target/<...>.jar
Bbonjr8/25/2023
isn't that where it's palced by default?
Tthallescomh8/25/2023
not in this Dockerfile for some reason
Bbonjr8/25/2023
i'm lost :ChikaConfused: what do i need to do?
Tthallescomh8/25/2023
isn't your jar inside /app? then change your Dockerfile to run that
Bbonjr8/25/2023
i think it is?
CMD ["java", "-jar", "/app/TokenBot_1_maven-1.0-SNAPSHOT-jar-with-dependencies.jar"]
Tthallescomh8/25/2023
yeah
Bbonjr8/25/2023
why is railway complaining 😭 😭 😭
Bbonjr8/25/2023
ok maybe huge development?

why is it not reflecting on railway
Bbonjr8/25/2023
i swear i can see the finish line now https://docs.railway.app/deploy/config-as-code
Bbrody1928/25/2023
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
Bbonjr8/25/2023
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
Bbrody1928/25/2023
i take it your deployment to fly didnt go well?
Bbonjr8/25/2023
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
Bbrody1928/25/2023
sounds good

Looking for more? Join the community!

Recommended Posts
Issues with Postgres DBI don't really understand why I am not seeing any tables in my DB. Seeding works, but the database sversion 'GLIBC 2.29' not foundweird error that seems to be because of my code, but it works on my computer <:bing_shrug:5837915814Use rust nightlyidk how docker works but i cant use the nightly rust dockerfile image for some reasonFix Missing libuuid LibraryHey, I am currently runnig into problems deploying my FastAPI backend after adding a new dependency I can no longer connect to my MySQL database, even after restarting the databaseI'm not sure what my project id is. My link is this: https://railway.app/project/31b9ee92-06cc-44c1-RPC showing forbidden access 403Hi. I've got some crypto related script which pings an RPC server - it works fine locally but on raiHow would I connect my project with the SQL database ?I have a model for users and I would like to populate my production DB with them. How can I do that?Hipaa/socI wanted to confirm if the information on https://railway.app/help is current as the email listed boMemory consumption differenceSame app deployed in docker local and Hw(M1 16gb) consumes 52 mb with no load railway took 74mb can Nextjs 13 Environmental variableHello! I recently installed Nextjs to try out the "new" app directory, because I heard server actioRailway CLI postgresql not installed?I installed the CLI and postgresql on my computer to use pgloader to migrate a mysql db to postgres How do I hook up my frontend correctly./I am able to deploy my backend but my frontend is not working yet. Error: ``` #8 [4/5] RUN npm ruStyling bug on project service usage pageIs there a way I can have Railway restart manually every 2 hours?Pretty self explanatory, I’m running some js codePostgres, Aspnet Core API and Angular WebguiHi guys. I'm new here and getting to know railway! I find it fantastic for us. I am lost. I would lCannot change default provider for environment thats created during GitHub Pull RequestI posted about this a while back (https://discord.com/channels/713503345364697088/112689969354953113expressidkI'm not sure on how to transfer my wiki.js database data to railway.86f37e49-db7c-4b6f-9c3e-71f5447b116a I am either getting a broken wiki.js database that results in Build and deployment is accountable in service consumptionconsider in a context of docker image number of images downloaded and each operation performed in doQuestion about billHello i have a railway acount and i work with my accounting team but they need an tax number identif