diff --git a/apps/testing/src/main/java/com/akto/testing/Main.java b/apps/testing/src/main/java/com/akto/testing/Main.java index 01b1b4a0d6..6b3f940d98 100644 --- a/apps/testing/src/main/java/com/akto/testing/Main.java +++ b/apps/testing/src/main/java/com/akto/testing/Main.java @@ -59,8 +59,10 @@ import org.slf4j.LoggerFactory; import java.util.*; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -202,8 +204,28 @@ private static void setTestingRunConfig(TestingRun testingRun, TestingRunResultS } if (testingRun.getTestIdConfig() > 1) { - baseConfig = TestingRunConfigDao.instance.findOne(Constants.ID, testingRun.getTestIdConfig()); - loggerMaker.infoAndAddToDb("Found testing run base config with id :" + baseConfig.getId(), LogDb.TESTING); + int counter = 0; + do { + baseConfig = TestingRunConfigDao.instance.findOne(Constants.ID, testingRun.getTestIdConfig()); + if (baseConfig == null) { + loggerMaker.errorAndAddToDb("in loop Couldn't find testing run base config:" + testingRun.getTestIdConfig(), LogDb.TESTING); + } else { + loggerMaker.infoAndAddToDb("in loop Found testing run base config with id :" + baseConfig.getId(), LogDb.TESTING); + } + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + + } + counter++; + } while (baseConfig == null && counter <= 5); + + if (baseConfig == null) { + loggerMaker.errorAndAddToDb("Couldn't find testing run base config:" + testingRun.getTestIdConfig(), LogDb.TESTING); + } else { + loggerMaker.infoAndAddToDb("Found testing run base config with id :" + baseConfig.getId(), LogDb.TESTING); + } } if (configFromTrrs == null) { @@ -219,6 +241,40 @@ private static void setTestingRunConfig(TestingRun testingRun, TestingRunResultS } } + + // Runnable task for monitoring memory + static class MemoryMonitorTask implements Runnable { + @Override + public void run() { + Runtime runtime = Runtime.getRuntime(); + + // Loop to print memory usage every 1 second + while (true) { + // Calculate memory statistics + long totalMemory = runtime.totalMemory(); + long freeMemory = runtime.freeMemory(); + long usedMemory = totalMemory - freeMemory; + + // Print memory statistics + System.out.print("Used Memory: " + (usedMemory / 1024 / 1024) + " MB "); + System.out.print("Free Memory: " + (freeMemory / 1024 / 1024) + " MB "); + System.out.print("Total Memory: " + (totalMemory / 1024 / 1024) + " MB "); + System.out.print("Available Memory: " + ((runtime.maxMemory() - usedMemory) / 1024 / 1024) + " MB "); + System.out.println("-------------------------"); + + // Pause for 1 second + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + System.err.println("Memory monitor thread interrupted: " + e.getMessage()); + break; // Exit the loop if thread is interrupted + } + } + } + } + + private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + public static void main(String[] args) throws InterruptedException { String mongoURI = System.getenv("AKTO_MONGO_CONN"); ReadPreference readPreference = ReadPreference.secondary(); @@ -236,6 +292,8 @@ public static void main(String[] args) throws InterruptedException { } while (!connectedToMongo); setupRateLimitWatcher(); + + executorService.scheduleAtFixedRate(new Main.MemoryMonitorTask(), 0, 1, TimeUnit.SECONDS); if (!SKIP_SSRF_CHECK) { Setup setup = SetupDao.instance.findOne(new BasicDBObject());