Skip to content

Commit

Permalink
Adding try-catch to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ark2307 committed Jan 21, 2025
1 parent 34f082b commit 3a99143
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions apps/testing/src/main/java/com/akto/testing/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,30 @@ private static void setTestingRunConfig(TestingRun testingRun, TestingRunResultS
private static BasicDBObject checkIfAlreadyTestIsRunningOnMachine(){
// this will return true if consumer is running and this the latest summary of the testing run
// and also the summary should be in running state
try {
BasicDBObject currentTestInfo = readJsonContentFromFile(Constants.TESTING_STATE_FOLDER_PATH, Constants.TESTING_STATE_FILE_NAME, BasicDBObject.class);
if(currentTestInfo == null){
return null;
}
if(!currentTestInfo.getBoolean("CONSUMER_RUNNING", false)){
return null;
}
String testingRunId = currentTestInfo.getString("testingRunId");
String testingRunSummaryId = currentTestInfo.getString("summaryId");

BasicDBObject currentTestInfo = readJsonContentFromFile(Constants.TESTING_STATE_FOLDER_PATH, Constants.TESTING_STATE_FILE_NAME, BasicDBObject.class);
if(currentTestInfo == null){
return null;
}
if(!currentTestInfo.getBoolean("CONSUMER_RUNNING", false)){
return null;
}
String testingRunId = currentTestInfo.getString("testingRunId");
String testingRunSummaryId = currentTestInfo.getString("summaryId");

TestingRunResultSummary testingRunResultSummary = TestingRunResultSummariesDao.instance.findOne(Filters.eq(Constants.ID, new ObjectId(testingRunSummaryId)), Projections.include(TestingRunResultSummary.STATE));
if(testingRunResultSummary == null || testingRunResultSummary.getState() != null || testingRunResultSummary.getState() != State.RUNNING){
return null;
}
TestingRunResultSummary testingRunResultSummary = TestingRunResultSummariesDao.instance.findOne(Filters.eq(Constants.ID, new ObjectId(testingRunSummaryId)), Projections.include(TestingRunResultSummary.STATE));
if(testingRunResultSummary == null || testingRunResultSummary.getState() != null || testingRunResultSummary.getState() != State.RUNNING){
return null;
}

TestingRunResultSummary latestSummary = TestingRunResultSummariesDao.instance.findLatestOne(Filters.eq(TestingRunResultSummary.TESTING_RUN_ID, new ObjectId(testingRunId)));
if(latestSummary.getHexId().equals(testingRunSummaryId)){
return currentTestInfo;
}else{
TestingRunResultSummary latestSummary = TestingRunResultSummariesDao.instance.findLatestOne(Filters.eq(TestingRunResultSummary.TESTING_RUN_ID, new ObjectId(testingRunId)));
if(latestSummary.getHexId().equals(testingRunSummaryId)){
return currentTestInfo;
}else{
return null;
}
} catch (Exception e) {
logger.error("Error in reading the testing state file: " + e.getMessage());
return null;
}
}
Expand Down Expand Up @@ -339,9 +343,6 @@ public static void main(String[] args) throws InterruptedException {

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

TestingProducer testingProducer = new TestingProducer();
TestingConsumer testingConsumer = new TestingConsumer();

// read from files here and then see if we want to init the Producer and run the consumer
// if producer is running, then we can skip the check and let the default testing pick up the job

Expand All @@ -350,13 +351,16 @@ public static void main(String[] args) throws InterruptedException {
int accountId = currentTestInfo.getInt("accountId");
Context.accountId.set(accountId);
loggerMaker.infoAndAddToDb("Tests were already running on this machine, thus resuming the test for account: "+ accountId, LogDb.TESTING);

FeatureAccess featureAccess = UsageMetricUtils.getFeatureAccess(accountId, MetricTypes.TEST_RUNS);

String testingRunId = currentTestInfo.getString("testingRunId");
String testingRunSummaryId = currentTestInfo.getString("summaryId");
TestingRun testingRun = TestingRunDao.instance.findOne(Filters.eq(Constants.ID, new ObjectId(testingRunId)));
ObjectId summaryId = new ObjectId(testingRunSummaryId);

TestingProducer testingProducer = new TestingProducer();
TestingConsumer testingConsumer = new TestingConsumer();

testingProducer.initProducer(testingRun, summaryId, featureAccess.fetchSyncLimit(), true);
testingConsumer.init();
}
Expand Down Expand Up @@ -601,6 +605,8 @@ public void run() {
}
}
RequiredConfigs.initiate();
TestingProducer testingProducer = new TestingProducer();
TestingConsumer testingConsumer = new TestingConsumer();
if(!maxRetriesReached){
// init producer and the consumer here
// producer for testing is currently calls init functions from test-executor
Expand Down

0 comments on commit 3a99143

Please sign in to comment.