@@ -59,69 +59,85 @@ public void run() {
59
59
List <InjectStatus > injectStatuses = this .injectStatusRepository .pendingForInjectType (CalderaContract .TYPE );
60
60
// For each one ask for traces and status
61
61
injectStatuses .forEach ((injectStatus -> {
62
- log .log (Level .INFO , "Found inject status: " + injectStatus );
62
+ log .log (Level .INFO , "Found inject status: " + injectStatus . getId () );
63
63
// Add traces and close inject if needed.
64
64
Instant finalExecutionTime = injectStatus .getTrackingSentDate ();
65
65
List <String > linkIds = injectStatus .statusIdentifiers ();
66
- log .log (Level .INFO , "Found links IDs: " + linkIds );
67
- List <ResultStatus > completedActions = new ArrayList <>();
68
- for (String linkId : linkIds ) {
69
- ResultStatus resultStatus = new ResultStatus ();
70
- try {
71
- log .log (Level .INFO , "Trying to get result for " + linkId );
72
- resultStatus = this .calderaService .results (linkId );
73
- } catch (Exception e ) {
74
- injectStatus .getTraces ().add (traceError ("Cannot get result for linkID " + linkId + ", injection has failed" ));
75
- resultStatus .setFail (true );
76
- completedActions .add (resultStatus );
77
- injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
78
- }
79
- if (resultStatus .getPaw () == null ) {
80
- if (injectStatus .getTrackingSentDate ().isBefore (Instant .now ().minus (EXPIRATION_TIME / 60 , ChronoUnit .MINUTES ))) {
66
+ if (linkIds .isEmpty ()) {
67
+ computeInjectStatus (injectStatus , finalExecutionTime , 0 , 0 );
68
+ computeInject (injectStatus );
69
+ } else {
70
+ log .log (Level .INFO , "Found links IDs: " + linkIds );
71
+ List <ResultStatus > completedActions = new ArrayList <>();
72
+ for (String linkId : linkIds ) {
73
+ ResultStatus resultStatus = new ResultStatus ();
74
+ try {
75
+ log .log (Level .INFO , "Trying to get result for " + linkId );
76
+ resultStatus = this .calderaService .results (linkId );
77
+ } catch (Exception e ) {
81
78
injectStatus .getTraces ().add (traceError ("Cannot get result for linkID " + linkId + ", injection has failed" ));
79
+ log .log (Level .INFO , "Cannot get result for linkID " + linkId + ", injection has failed" );
82
80
resultStatus .setFail (true );
83
81
completedActions .add (resultStatus );
84
82
injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
85
83
}
86
- } else {
87
- if (resultStatus .isComplete ()) {
88
- completedActions .add (resultStatus );
89
- injectStatus .setTrackingTotalSuccess (injectStatus .getTrackingTotalSuccess () + 1 );
90
- // Compute biggest execution time
91
- if (resultStatus .getFinish ().isAfter (finalExecutionTime )) {
92
- finalExecutionTime = resultStatus .getFinish ();
84
+ if (resultStatus .getPaw () == null ) {
85
+ if (injectStatus .getTrackingSentDate ().isBefore (Instant .now ().minus (EXPIRATION_TIME / 60 , ChronoUnit .MINUTES ))) {
86
+ injectStatus .getTraces ().add (traceError ("Cannot get result for linkID " + linkId + ", injection has failed" ));
87
+ log .log (Level .INFO , "Cannot get result for linkID " + linkId + ", injection has failed" );
88
+ resultStatus .setFail (true );
89
+ completedActions .add (resultStatus );
90
+ injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
91
+ }
92
+ } else {
93
+ if (resultStatus .isComplete ()) {
94
+ completedActions .add (resultStatus );
95
+ if (resultStatus .isFail ()) {
96
+ injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
97
+ injectStatus .getTraces ().add (traceError ("Failed result for linkID " + linkId + " (" + resultStatus .getContent () + ")" ));
98
+ } else {
99
+ injectStatus .setTrackingTotalSuccess (injectStatus .getTrackingTotalSuccess () + 1 );
100
+ injectStatus .getTraces ().add (traceInfo ("Success result for linkID " + linkId + " (" + resultStatus .getContent () + ")" ));
101
+ }
102
+ // Compute biggest execution time
103
+ if (resultStatus .getFinish ().isAfter (finalExecutionTime )) {
104
+ finalExecutionTime = resultStatus .getFinish ();
105
+ }
106
+ } else if (injectStatus .getTrackingSentDate ().isBefore (Instant .now ().minus (5L , ChronoUnit .MINUTES ))) {
107
+ injectStatus .getTraces ().add (traceError ("Timeout on linkID " + linkId + ", injection has failed" ));
108
+ log .log (Level .INFO , "Timeout on linkID " + linkId + ", injection has failed" );
109
+ resultStatus .setFail (true );
110
+ completedActions .add (resultStatus );
111
+ injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
93
112
}
94
- } else if (injectStatus .getTrackingSentDate ().isBefore (Instant .now ().minus (5L , ChronoUnit .MINUTES ))) {
95
- injectStatus .getTraces ().add (traceError ("Timeout on linkID " + linkId + ", injection has failed" ));
96
- resultStatus .setFail (true );
97
- completedActions .add (resultStatus );
98
- injectStatus .setTrackingTotalError (injectStatus .getTrackingTotalError () + 1 );
99
113
}
100
114
}
101
- }
102
- // Compute status only if all actions are completed
103
- if (! linkIds . isEmpty () && completedActions .size () == linkIds . size ()) {
104
- int failedActions = ( int ) completedActions .stream (). filter ( ResultStatus :: isFail ). count ( );
105
- computeInjectStatus ( injectStatus , finalExecutionTime , completedActions . size (), failedActions );
106
- // Update related inject
107
- computeInject ( injectStatus );
115
+ // Compute status only if all actions are completed
116
+ if ( completedActions . size () == linkIds . size ()) {
117
+ int failedActions = ( int ) completedActions .stream (). filter ( ResultStatus :: isFail ). count ();
118
+ computeInjectStatus ( injectStatus , finalExecutionTime , completedActions .size (), failedActions );
119
+ // Update related inject
120
+ computeInject ( injectStatus );
121
+ }
108
122
}
109
123
}));
110
124
}
111
125
112
126
// -- INJECT STATUS --
113
127
114
- @ Transactional
115
128
public void computeInjectStatus (
116
129
@ NotNull final InjectStatus injectStatus ,
117
130
@ NotNull final Instant finalExecutionTime ,
118
131
final int completedActions ,
119
132
final int failedActions ) {
120
- boolean hasError = injectStatus .getTraces ().stream ().anyMatch (trace -> trace .getStatus ().equals (ExecutionStatus .ERROR ));
121
- injectStatus .setName (hasError ? ExecutionStatus .ERROR : ExecutionStatus .SUCCESS );
122
- injectStatus .getTraces ().add (
123
- traceInfo ("caldera" , "Caldera executed the ability on " + (completedActions - failedActions ) + "/" + completedActions + " asset(s)" )
124
- );
133
+ if (injectStatus .getTraces ().stream ().filter (injectStatusExecution -> injectStatusExecution .getStatus ().equals (ExecutionStatus .ERROR )).count () == completedActions ) {
134
+ injectStatus .setName (ExecutionStatus .ERROR );
135
+ } else if (injectStatus .getTraces ().stream ().anyMatch (trace -> trace .getStatus ().equals (ExecutionStatus .ERROR ))) {
136
+ injectStatus .setName (ExecutionStatus .PARTIAL );
137
+ } else {
138
+ injectStatus .setName (ExecutionStatus .SUCCESS );
139
+ }
140
+ injectStatus .getTraces ().add (traceInfo ("caldera" , "Caldera executed the ability on " + (completedActions - failedActions ) + "/" + completedActions + " asset(s)" ));
125
141
long executionTime = (finalExecutionTime .toEpochMilli () - injectStatus .getTrackingSentDate ().toEpochMilli ());
126
142
injectStatus .setTrackingTotalExecutionTime (executionTime );
127
143
injectStatus .setTrackingEndDate (Instant .now ());
@@ -130,7 +146,6 @@ public void computeInjectStatus(
130
146
131
147
// -- INJECT --
132
148
133
- @ Transactional
134
149
public void computeInject (@ NotNull final InjectStatus injectStatus ) {
135
150
Inject relatedInject = injectStatus .getInject ();
136
151
relatedInject .setUpdatedAt (Instant .now ());
0 commit comments