|
| 1 | +package io.codeclou.java.is.database.up; |
| 2 | + |
| 3 | +import org.apache.commons.cli.*; |
| 4 | + |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.DriverManager; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +public class ReadinessChecker { |
| 10 | + |
| 11 | + private CommandLineParser parser = new DefaultParser(); |
| 12 | + private Options options = new Options(); |
| 13 | + private Boolean hasCmdLineParameterErrors = false; |
| 14 | + |
| 15 | + protected void run(String[] args) throws Exception { |
| 16 | + // |
| 17 | + // Pull in drivers |
| 18 | + // |
| 19 | + Class.forName("org.postgresql.Driver"); |
| 20 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 21 | + // |
| 22 | + // Options |
| 23 | + // |
| 24 | + options.addOption("j", "jdbc", true, "the jdbc url e.g.: jdbc:postgresql://localhost/test"); |
| 25 | + options.addOption("u", "user", true, "the database username"); |
| 26 | + options.addOption("p", "password", true, "the database password"); |
| 27 | + options.addOption("s", "ssl", true, "use ssl true/false. default true."); |
| 28 | + options.addOption("w", "maxwait", true, "how long should we wait until we exit with error in minutes."); |
| 29 | + options.addOption("v", "verbose", false, "if specified a lot of info is printed."); |
| 30 | + CommandLine cmd = this.parser.parse(options, args); |
| 31 | + System.out.println("\033[35m+-------------------------+\033[0m"); |
| 32 | + System.out.println("\033[35m| Java Is Database Up |\033[0m"); |
| 33 | + System.out.println("\033[35m+-------------------------+\033[0m"); |
| 34 | + |
| 35 | + if (!cmd.hasOption("jdbc")) { |
| 36 | + System.out.println("\033[31mError >> Please specify connection with -j\033[0m"); |
| 37 | + hasCmdLineParameterErrors = true; |
| 38 | + } |
| 39 | + if (!cmd.hasOption("user")) { |
| 40 | + System.out.println("\033[31mError >> Please specify database user with -u\033[0m"); |
| 41 | + hasCmdLineParameterErrors = true; |
| 42 | + } |
| 43 | + if (!cmd.hasOption("password")) { |
| 44 | + System.out.println("\033[31mError >> Please specify database password with -p\033[0m"); |
| 45 | + hasCmdLineParameterErrors = true; |
| 46 | + } |
| 47 | + if (!cmd.hasOption("ssl")) { |
| 48 | + System.out.println("\033[31mError >> Please specify database connection should use ssl with -s\033[0m"); |
| 49 | + hasCmdLineParameterErrors = true; |
| 50 | + } |
| 51 | + if (!cmd.hasOption("maxwait")) { |
| 52 | + System.out.println("\033[31mError >> Please specify maxwait in minutes with -w\033[0m"); |
| 53 | + hasCmdLineParameterErrors = true; |
| 54 | + } |
| 55 | + if (!hasCmdLineParameterErrors) { |
| 56 | + // |
| 57 | + // CONNECTION PROPS |
| 58 | + // |
| 59 | + String url = cmd.getOptionValue("jdbc"); |
| 60 | + Properties props = new Properties(); |
| 61 | + props.setProperty("user", cmd.getOptionValue("user")); |
| 62 | + props.setProperty("password", cmd.getOptionValue("password")); |
| 63 | + props.setProperty("ssl", cmd.getOptionValue("ssl")); |
| 64 | + Integer maxWaitMinutes = Integer.parseInt(cmd.getOptionValue("maxwait")); |
| 65 | + System.out.println("\033[36mTrying >> to connect to db " + url + " and waiting for max " + maxWaitMinutes + "min. \033[0m"); |
| 66 | + for (int i=0; i < maxWaitMinutes * 2; i++) { |
| 67 | + // |
| 68 | + // TRY CONNECTION |
| 69 | + // |
| 70 | + try { |
| 71 | + Connection conn = DriverManager.getConnection(url, props); |
| 72 | + System.out.println("\033[32mSuccess >> database is up\033[0m"); |
| 73 | + System.exit(0); |
| 74 | + } catch (Exception e) { |
| 75 | + if (cmd.hasOption("verbose")) { |
| 76 | + System.out.println(" . " + e.getMessage()); |
| 77 | + } else { |
| 78 | + System.out.println(" ."); |
| 79 | + } |
| 80 | + Thread.sleep(30000); // sleep 30s |
| 81 | + } |
| 82 | + } |
| 83 | + // |
| 84 | + // FINALLY (after maxwait reached) |
| 85 | + // |
| 86 | + System.out.println("\033[31mError >> Could not connect to db. Max wait reached. Exit.\033[0m"); |
| 87 | + System.exit(1); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments