Skip to content

Commit

Permalink
Job streaming queue implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-mulligan committed Nov 6, 2023
1 parent 85299a0 commit 8fea4ee
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 82 deletions.
4 changes: 2 additions & 2 deletions patchfinder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
22 changes: 17 additions & 5 deletions patchfinder/src/main/java/FixFinderMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/

import db.DatabaseHelper;
import env.FixFinderEnvVars;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -37,6 +38,11 @@
*/
public class FixFinderMain extends Thread {
private final static Logger logger = LogManager.getLogger(FixFinderMain.class);
private final DatabaseHelper databaseHelper;

public FixFinderMain(DatabaseHelper dbh) {
this.databaseHelper = dbh;
}

/**
* Entry point for the FixFinder, initializes necessary classes and start listening for jobs with RabbitMQ
Expand All @@ -45,18 +51,24 @@ public class FixFinderMain extends Thread {
public void run() {
logger.info("Starting FixFinder...");

// Init FixFinder
FixFinder.init();
// Get input mode
final String inputMode = FixFinderEnvVars.getInputMode();

// Determine run mode and start PatchFinder
switch (FixFinderEnvVars.getInputMode()) {
switch (inputMode) {
case "db":
// Init FixFinder
FixFinder.init(this.databaseHelper);
runDb();
break;
case "rabbit":
// Init FixFinder
FixFinder.init(this.databaseHelper);
runRabbit();
break;
case "dev":
// Init FixFinder
FixFinder.init(this.databaseHelper);
runDev();
break;
default:
Expand Down Expand Up @@ -95,7 +107,7 @@ private void runDev() {
}

public static void main(String[] args) {
FixFinderMain finder = new FixFinderMain();
finder.start();
// FixFinderMain finder = new FixFinderMain();
// finder.start();
}
}
8 changes: 7 additions & 1 deletion patchfinder/src/main/java/PatchFinderMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/

import db.DatabaseHelper;
import env.PatchFinderEnvVars;
import messenger.Messenger;
import model.CpeGroup;
Expand All @@ -41,6 +42,11 @@
*/
public class PatchFinderMain extends Thread {
private final static Logger logger = LogManager.getLogger(PatchFinderMain.class);
private final DatabaseHelper databaseHelper;

public PatchFinderMain(DatabaseHelper dbh) {
this.databaseHelper = dbh;
}

/**
* Entry point for the PatchFinder, initializes necessary classes and start listening for jobs with RabbitMQ
Expand All @@ -49,7 +55,7 @@ public class PatchFinderMain extends Thread {
public void run() {
logger.info("Starting PatchFinder...");
// Init PatchFinder
PatchFinder.init();
PatchFinder.init(this.databaseHelper);

// Determine run mode and start PatchFinder
switch (PatchFinderEnvVars.getInputMode()) {
Expand Down
14 changes: 12 additions & 2 deletions patchfinder/src/main/java/PatchFixMain.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import db.DatabaseHelper;
import env.SharedEnvVars;

public class PatchFixMain {
public static void main(String[] args) {
new PatchFinderMain().start();
new FixFinderMain().start();
SharedEnvVars.initializeEnvVars(false);
final DatabaseHelper dbh = new DatabaseHelper(
SharedEnvVars.getDatabaseType(),
SharedEnvVars.getHikariUrl(),
SharedEnvVars.getHikariUser(),
SharedEnvVars.getHikariPassword()
);
new PatchFinderMain(dbh).start();
new FixFinderMain(dbh).start();
}
}
112 changes: 112 additions & 0 deletions patchfinder/src/main/java/env/SharedEnvVars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package env;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import static env.EnvVarLoader.loadEnvVarsFromFile;

public class SharedEnvVars {
private static final Logger logger = LogManager.getLogger(PatchFinderEnvVars.class);
private static final String envVarPath = "env.list";

// Default values for database environment variables
private static String databaseType = "mysql";
private static String hikariUrl = "jdbc:mysql://localhost:3306/nvip?useSSL=false&allowPublicKeyRetrieval=true";
private static String hikariUser = "root";
private static String hikariPassword = "root";

public static String getDatabaseType() { return databaseType; }
public static String getHikariUrl() { return hikariUrl; }
public static String getHikariUser() { return hikariUser; }
public static String getHikariPassword() { return hikariPassword; }

/**
* Loads environment variables from both env.list file and System.getenv(). If both of these fail, resorts to
* default values defined above. Prioritizes System.getenv() first and then from file second.
*/
public static void initializeEnvVars(boolean testMode) {
logger.info("CURRENT PATH --> " + System.getProperty("user.dir"));
if(testMode) logger.info("Initializing Test Environment Variables...");
else logger.info("Initializing Environment Variables...");

Map<String, String> fileProps = null;
Map<String, String> systemProps = System.getenv();
String filePath = envVarPath;
if(testMode) filePath = "src/test/" + filePath;

try {
// Assumes in `nvip-crawler/patchfinder` working directory
fileProps = loadEnvVarsFromFile(filePath);
} catch (FileNotFoundException e){
// If that path doesn't work, assumes we are in `nvip-crawler` directory and tries new path with `patchfinder` appended to it
try{
String possiblePath = "patchfinder\\" + filePath;
fileProps = loadEnvVarsFromFile(possiblePath);
} catch (Exception ignored) {}
}

// If env vars couldn't be loaded from file, pass in empty map
if(fileProps == null) fileProps = new HashMap<>();
fetchEnvVars(systemProps, fileProps);
}

/**
* Attempts to fetch all required environment variables from props map safely, logging any
* missing or incorrect variables.
*
* If environment variable is not found from System.getenv(), it will attempt to fetch it from the loaded file. If it
* is still not found, it will resort to default value. Priority: System.getenv() <- env.list file <- default values
*
* @param systemProps map of environment variables from System.getenv()
* @param fileProps map of environment variables read from file
*/
private static void fetchEnvVars(Map<String, String> systemProps, Map<String, String> fileProps) {
fetchHikariEnvVars(systemProps, fileProps);
}

/**
* Initialize database env vars
*
* @param systemProps map of environment variables from System.getenv()
* @param fileProps map of environment variables read from file
*/
private static void fetchHikariEnvVars(Map<String, String> systemProps, Map<String, String> fileProps) {

if(systemProps.containsKey("DB_TYPE")) {
databaseType = systemProps.get("DB_TYPE");
logger.info("Setting DB_TYPE to {}", databaseType);
} else if (fileProps.containsKey("DB_TYPE")) {
databaseType = fileProps.get("DB_TYPE");
logger.info("Setting DB_TYPE to {}", databaseType);
} else logger.warn("Could not fetch DB_TYPE from env vars, defaulting to {}", databaseType);

if(systemProps.containsKey("HIKARI_URL")) {
hikariUrl = systemProps.get("HIKARI_URL");
logger.info("Setting HIKARI_URL to {}", hikariUrl);
} else if (fileProps.containsKey("HIKARI_URL")) {
hikariUrl = fileProps.get("HIKARI_URL");
logger.info("Setting HIKARI_URL to {}", hikariUrl);
} else logger.warn("Could not fetch HIKARI_URL from env vars, defaulting to {}", hikariUrl);

if(systemProps.containsKey("HIKARI_USER")) {
hikariUser = systemProps.get("HIKARI_USER");
logger.info("Setting HIKARI_USER to {}", hikariUser);
} else if (fileProps.containsKey("HIKARI_USER")) {
hikariUser = fileProps.get("HIKARI_USER");
logger.info("Setting HIKARI_USER to {}", hikariUser);
} else logger.warn("Could not fetch HIKARI_USER from env vars, defaulting to {}", hikariUser);

if(systemProps.containsKey("HIKARI_PASSWORD")) {
hikariPassword = systemProps.get("HIKARI_PASSWORD");
logger.info("Setting HIKARI_PASSWORD to {}", hikariPassword);
} else if (fileProps.containsKey("HIKARI_PASSWORD")) {
hikariPassword = fileProps.get("HIKARI_PASSWORD");
logger.info("Setting HIKARI_PASSWORD to {}", hikariPassword);
} else logger.warn("Could not fetch HIKARI_PASSWORD from env vars, defaulting to {}", hikariPassword);

}
}
14 changes: 7 additions & 7 deletions patchfinder/src/main/java/fixes/FixFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,12 @@ public class FixFinder {
/**
* Initialize the FixFinder and its subcomponents
*/
public static void init() {
public static void init(DatabaseHelper dbh) {
logger.info("Initializing FixFinder...");

// Init db helper
logger.info("Initializing DatabaseHelper...");
databaseHelper = new DatabaseHelper(
FixFinderEnvVars.getDatabaseType(),
FixFinderEnvVars.getHikariUrl(),
FixFinderEnvVars.getHikariUser(),
FixFinderEnvVars.getHikariPassword()
);
databaseHelper = dbh;

// Init FixUrlFinders
logger.info("Initializing FixUrlFinders...");
Expand Down Expand Up @@ -170,4 +165,9 @@ public static void run(List<String> cveIds) {
// existingInserts
// );
}

@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
Loading

0 comments on commit 8fea4ee

Please sign in to comment.