Skip to content

Commit

Permalink
Merge pull request #1861 from akto-api-security/base_config_fetch_retry
Browse files Browse the repository at this point in the history
add retries
  • Loading branch information
notshivansh authored Dec 24, 2024
2 parents 40f9df1 + 3c1eb16 commit 8a8cd88
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions apps/testing/src/main/java/com/akto/testing/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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());
Expand Down

0 comments on commit 8a8cd88

Please sign in to comment.