Skip to content

Commit

Permalink
Made cron async
Browse files Browse the repository at this point in the history
  • Loading branch information
aktoboy committed Nov 1, 2023
1 parent a25a832 commit f5aac88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 6 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 @@ -42,6 +42,7 @@ public class Main {
private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class);

public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
public static final ScheduledExecutorService scheduler2 = Executors.newScheduledThreadPool(1);

public static final boolean SKIP_SSRF_CHECK = "true".equalsIgnoreCase(System.getenv("SKIP_SSRF_CHECK"));
public static final boolean IS_SAAS = "true".equalsIgnoreCase(System.getenv("IS_SAAS"));
Expand Down Expand Up @@ -112,8 +113,11 @@ public static void main(String[] args) throws InterruptedException {
} while (!connectedToMongo);

setupRateLimitWatcher();
OptimizeStorageCron osc = new OptimizeStorageCron();
osc.init();

scheduler2.scheduleAtFixedRate(()-> {
OptimizeStorageCron osc = new OptimizeStorageCron();
osc.init();
}, 0, 3, TimeUnit.HOURS);

loggerMaker.infoAndAddToDb("Starting.......", LogDb.TESTING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void init(){
int limit = 10000;
Context.accountId.set(account.getId());
List<TestingRunResult> testingRunResults = TestingRunResultDao.instance.findAll(new BasicDBObject(), skip, limit, new BasicDBObject("_id", -1));
boolean runCompact = false;
while (!testingRunResults.isEmpty()) {
logger.infoAndAddToDb("Processing testing run results from: " + skip + " to: " + (skip + limit) + " for account: " + account.getId(), LoggerMaker.LogDb.TESTING);
int batch_time = Context.now();
Expand Down Expand Up @@ -61,6 +62,7 @@ public void init(){
if (notFixedCount > 0) {
// logger.infoAndAddToDb("Fixed: " + fixedCount + " not fixed: " + notFixedCount + " for testing run result: " + testingRunResult.getId(), LoggerMaker.LogDb.TESTING);
TestingRunResultDao.instance.replaceOne(Filters.eq("_id", testingRunResult.getId()), testingRunResult);
runCompact = true;
}
}
if (!urlToOriginalMessageMap.isEmpty()) {
Expand All @@ -86,12 +88,16 @@ public void init(){
logger.infoAndAddToDb("Finished processing testing run results from: " + skip + " to: " + (skip + limit) + " for account: " + account.getId() + " in: " + (Context.now() - batch_time), LoggerMaker.LogDb.TESTING);
testingRunResults = TestingRunResultDao.instance.findAll(new BasicDBObject(), skip, limit, new BasicDBObject("_id", 1));
}
int accountId = account.getId();
logger.infoAndAddToDb("Starting compact for account: " + accountId, LoggerMaker.LogDb.TESTING);
int now = Context.now();
clients[0].getDatabase(String.valueOf(accountId)).runCommand(new BasicDBObject("compact", "testing_run_result"));
int compactTime = Context.now() - now;
logger.infoAndAddToDb("Finished optimizing storage, compact time for account: " + accountId + " is: " + compactTime, LoggerMaker.LogDb.TESTING);
if(runCompact) {
int accountId = account.getId();
logger.infoAndAddToDb("Starting compact for account: " + accountId, LoggerMaker.LogDb.TESTING);
int now = Context.now();
clients[0].getDatabase(String.valueOf(accountId)).runCommand(new BasicDBObject("compact", "testing_run_result"));
int compactTime = Context.now() - now;
logger.infoAndAddToDb("Finished optimizing storage, compact time for account: " + accountId + " is: " + compactTime, LoggerMaker.LogDb.TESTING);
} else {
logger.infoAndAddToDb("No need to compact for account: " + account.getId(), LoggerMaker.LogDb.TESTING);
}
} catch(Exception e) {
e.printStackTrace();
logger.errorAndAddToDb("Error while optimizing storage for account: " + account.getId(), LoggerMaker.LogDb.TESTING);
Expand Down

0 comments on commit f5aac88

Please sign in to comment.