@@ -62,7 +62,7 @@ private static ObjectId createTRRSummaryIfAbsent(TestingRun testingRun, int star
62
62
Updates .set (TestingRunResultSummary .STATE , TestingRun .State .RUNNING ));
63
63
} catch (Exception e ){
64
64
TestingRunResultSummary summary = new TestingRunResultSummary (start , 0 , new HashMap <>(),
65
- 0 , testingRun .getId (), testingRun .getId ().toHexString (), 0 );
65
+ 0 , testingRun .getId (), testingRun .getId ().toHexString (), 0 , 0 );
66
66
67
67
summaryId = TestingRunResultSummariesDao .instance .insertOne (summary ).getInsertedId ().asObjectId ().getValue ();
68
68
}
@@ -92,6 +92,73 @@ public void accept(Account t) {
92
92
93
93
private static final int LAST_TEST_RUN_EXECUTION_DELTA = 5 * 60 ;
94
94
95
+ private static TestingRun findPendingTestingRun () {
96
+ int delta = Context .now () - 20 *60 ;
97
+
98
+ Bson filter1 = Filters .and (Filters .eq (TestingRun .STATE , TestingRun .State .SCHEDULED ),
99
+ Filters .lte (TestingRun .SCHEDULE_TIMESTAMP , Context .now ())
100
+ );
101
+ Bson filter2 = Filters .and (
102
+ Filters .eq (TestingRun .STATE , TestingRun .State .RUNNING ),
103
+ Filters .lte (TestingRun .SCHEDULE_TIMESTAMP , delta )
104
+ );
105
+
106
+ Bson update = Updates .combine (
107
+ Updates .set (TestingRun .PICKED_UP_TIMESTAMP , Context .now ()),
108
+ Updates .set (TestingRun .STATE , TestingRun .State .RUNNING )
109
+ );
110
+
111
+ return TestingRunDao .instance .getMCollection ().findOneAndUpdate (
112
+ Filters .or (filter1 ,filter2 ), update );
113
+ }
114
+
115
+ private static TestingRunResultSummary findPendingTestingRunResultSummary () {
116
+ int delta = Context .now () - 20 *60 ;
117
+
118
+ Bson filter1 = Filters .and (
119
+ Filters .eq (TestingRun .STATE , TestingRun .State .SCHEDULED ),
120
+ Filters .lte (TestingRunResultSummary .START_TIMESTAMP , Context .now ()),
121
+ Filters .gt (TestingRunResultSummary .START_TIMESTAMP , delta )
122
+ );
123
+
124
+ Bson filter2 = Filters .and (
125
+ Filters .eq (TestingRun .STATE , TestingRun .State .RUNNING ),
126
+ Filters .gt (TestingRunResultSummary .START_TIMESTAMP , delta )
127
+ );
128
+
129
+ Bson update = Updates .set (TestingRun .STATE , TestingRun .State .RUNNING );
130
+
131
+ TestingRunResultSummary trrs = TestingRunResultSummariesDao .instance .getMCollection ().findOneAndUpdate (Filters .or (filter1 ,filter2 ), update );
132
+
133
+ return trrs ;
134
+ }
135
+
136
+ private static void setTestingRunConfig (TestingRun testingRun , TestingRunResultSummary trrs ) {
137
+ long timestamp = testingRun .getId ().getTimestamp ();
138
+ long seconds = Context .now () - timestamp ;
139
+ loggerMaker .infoAndAddToDb ("Found one + " + testingRun .getId ().toHexString () + " created: " + seconds + " seconds ago" , LogDb .TESTING );
140
+
141
+ TestingRunConfig configFromTrrs = null ;
142
+ TestingRunConfig baseConfig = null ;
143
+
144
+ if (trrs != null && trrs .getTestIdConfig () > 1 ) {
145
+ configFromTrrs = TestingRunConfigDao .instance .findOne (Constants .ID , trrs .getTestIdConfig ());
146
+ loggerMaker .infoAndAddToDb ("Found testing run config with id :" + configFromTrrs .getId (), LogDb .TESTING );
147
+ }
148
+
149
+ if (testingRun .getTestIdConfig () > 1 ) {
150
+ baseConfig = TestingRunConfigDao .instance .findOne (Constants .ID , testingRun .getTestIdConfig ());
151
+ loggerMaker .infoAndAddToDb ("Found testing run config with id :" + baseConfig .getId (), LogDb .TESTING );
152
+ }
153
+
154
+ if (configFromTrrs == null ) {
155
+ testingRun .setTestingRunConfig (baseConfig );
156
+ } else {
157
+ configFromTrrs .rebaseOn (baseConfig );
158
+ testingRun .setTestingRunConfig (configFromTrrs );
159
+ }
160
+ }
161
+
95
162
public static void main (String [] args ) throws InterruptedException {
96
163
String mongoURI = System .getenv ("AKTO_MONGO_CONN" );;
97
164
DaoInit .init (new ConnectionString (mongoURI ));
@@ -117,66 +184,50 @@ public static void main(String[] args) throws InterruptedException {
117
184
118
185
while (true ) {
119
186
AccountTask .instance .executeTask (account -> {
120
- int delta = Context .now () - 20 *60 ;
121
-
122
- Bson filter1 = Filters .and (Filters .eq (TestingRun .STATE , TestingRun .State .SCHEDULED ),
123
- Filters .lte (TestingRun .SCHEDULE_TIMESTAMP , Context .now ())
124
- );
125
- Bson filter2 = Filters .and (
126
- Filters .eq (TestingRun .STATE , TestingRun .State .RUNNING ),
127
- Filters .lte (TestingRun .SCHEDULE_TIMESTAMP , delta )
128
- );
129
-
130
- Bson update = Updates .combine (
131
- Updates .set (TestingRun .PICKED_UP_TIMESTAMP , Context .now ()),
132
- Updates .set (TestingRun .STATE , TestingRun .State .RUNNING )
133
- );
134
187
135
188
int start = Context .now ();
136
189
137
- TestingRun testingRun = TestingRunDao .instance .getMCollection ().findOneAndUpdate (
138
- Filters .or (filter1 ,filter2 ), update );
190
+ TestingRunResultSummary trrs = findPendingTestingRunResultSummary ();
191
+ TestingRun testingRun ;
192
+ ObjectId summaryId = null ;
193
+ if (trrs == null ) {
194
+ testingRun = findPendingTestingRun ();
195
+ } else {
196
+ summaryId = trrs .getId ();
197
+ testingRun = TestingRunDao .instance .findOne ("_id" , trrs .getTestingRunId ());
198
+ }
139
199
140
200
if (testingRun == null ) {
141
201
return ;
142
202
}
143
203
144
-
145
- ObjectId summaryId = null ;
146
204
try {
147
- long timestamp = testingRun .getId ().getTimestamp ();
148
- long seconds = Context .now () - timestamp ;
149
- loggerMaker .infoAndAddToDb ("Found one + " + testingRun .getId ().toHexString () + " created: " + seconds + " seconds ago" , LogDb .TESTING );
150
- if (testingRun .getTestIdConfig () > 1 ) {
151
- TestingRunConfig testingRunConfig = TestingRunConfigDao .instance .findOne (Constants .ID , testingRun .getTestIdConfig ());
152
- if (testingRunConfig != null ) {
153
- loggerMaker .infoAndAddToDb ("Found testing run config with id :" + testingRunConfig .getId (), LogDb .TESTING );
154
- testingRun .setTestingRunConfig (testingRunConfig );
155
- }else {
156
- loggerMaker .errorAndAddToDb ("Couldn't find testing run config id for " + testingRun .getTestIdConfig (), LogDb .TESTING );
157
- }
158
- }
159
- if (testingRun .getState ().equals (TestingRun .State .RUNNING )){
160
- Map <ObjectId , TestingRunResultSummary > objectIdTestingRunResultSummaryMap = TestingRunResultSummariesDao .instance .fetchLatestTestingRunResultSummaries (Collections .singletonList (testingRun .getId ()));
161
- TestingRunResultSummary testingRunResultSummary = objectIdTestingRunResultSummaryMap .get (testingRun .getId ());
162
- List <TestingRunResult > testingRunResults = TestingRunResultDao .instance .fetchLatestTestingRunResult (Filters .eq (TestingRunResult .TEST_RUN_RESULT_SUMMARY_ID , testingRunResultSummary .getId ()), 1 );
163
- if (testingRunResults != null && !testingRunResults .isEmpty ()){
164
- TestingRunResult testingRunResult = testingRunResults .get (0 );
165
- if (Context .now () - testingRunResult .getEndTimestamp () < LAST_TEST_RUN_EXECUTION_DELTA ){
166
- loggerMaker .infoAndAddToDb ("Skipping test run as it was executed recently, TRR_ID:"
167
- + testingRunResult .getHexId () + ", TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
168
- return ;
205
+ setTestingRunConfig (testingRun , trrs );
206
+
207
+ if (summaryId == null ) {
208
+ if (testingRun .getState ().equals (TestingRun .State .RUNNING )) {
209
+ Map <ObjectId , TestingRunResultSummary > objectIdTestingRunResultSummaryMap = TestingRunResultSummariesDao .instance .fetchLatestTestingRunResultSummaries (Collections .singletonList (testingRun .getId ()));
210
+ TestingRunResultSummary testingRunResultSummary = objectIdTestingRunResultSummaryMap .get (testingRun .getId ());
211
+ List <TestingRunResult > testingRunResults = TestingRunResultDao .instance .fetchLatestTestingRunResult (Filters .eq (TestingRunResult .TEST_RUN_RESULT_SUMMARY_ID , testingRunResultSummary .getId ()), 1 );
212
+ if (testingRunResults != null && !testingRunResults .isEmpty ()) {
213
+ TestingRunResult testingRunResult = testingRunResults .get (0 );
214
+ if (Context .now () - testingRunResult .getEndTimestamp () < LAST_TEST_RUN_EXECUTION_DELTA ) {
215
+ loggerMaker .infoAndAddToDb ("Skipping test run as it was executed recently, TRR_ID:"
216
+ + testingRunResult .getHexId () + ", TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
217
+ return ;
218
+ } else {
219
+ loggerMaker .infoAndAddToDb ("Test run was executed long ago, TRR_ID:"
220
+ + testingRunResult .getHexId () + ", TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
221
+ TestingRunResultSummariesDao .instance .updateOne (Filters .eq (TestingRunResultSummary .ID , testingRunResultSummary .getId ()), Updates .set (TestingRunResultSummary .STATE , TestingRun .State .FAILED ));
222
+ }
169
223
} else {
170
- loggerMaker .infoAndAddToDb ("Test run was executed long ago, TRR_ID:"
171
- + testingRunResult .getHexId () + ", TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
224
+ loggerMaker .infoAndAddToDb ("No executions made for this test, will need to restart it, TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
172
225
TestingRunResultSummariesDao .instance .updateOne (Filters .eq (TestingRunResultSummary .ID , testingRunResultSummary .getId ()), Updates .set (TestingRunResultSummary .STATE , TestingRun .State .FAILED ));
173
226
}
174
- } else {
175
- loggerMaker .infoAndAddToDb ("No executions made for this test, will need to restart it, TRRS_ID:" + testingRunResultSummary .getHexId () + " TR_ID:" + testingRun .getHexId (), LogDb .TESTING );
176
- TestingRunResultSummariesDao .instance .updateOne (Filters .eq (TestingRunResultSummary .ID , testingRunResultSummary .getId ()), Updates .set (TestingRunResultSummary .STATE , TestingRun .State .FAILED ));
177
227
}
228
+
229
+ summaryId = createTRRSummaryIfAbsent (testingRun , start );
178
230
}
179
- summaryId = createTRRSummaryIfAbsent (testingRun , start );
180
231
TestExecutor testExecutor = new TestExecutor ();
181
232
testExecutor .init (testingRun , summaryId );
182
233
raiseMixpanelEvent (summaryId , testingRun );
0 commit comments