@@ -36,6 +36,8 @@ import scala.concurrent.{Await, ExecutionContext, Future}
36
36
import scala .language .postfixOps
37
37
import scala .util .{Failure , Success , Try }
38
38
39
+
40
+ // scalastyle:off number.of.methods
39
41
class RequestHandler (configuration : Configuration , authDao : AuthDAO , instanceDao : InstanceDAO , connection : DockerConnection ) extends AppLogging {
40
42
41
43
@@ -122,15 +124,15 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
122
124
}
123
125
124
126
def getAllInstancesOfType (compType : Option [ComponentType ]): List [Instance ] = {
125
- if (compType.isDefined){
127
+ if (compType.isDefined) {
126
128
instanceDao.getInstancesOfType(compType.get)
127
129
} else {
128
130
instanceDao.allInstances()
129
131
}
130
132
}
131
133
132
134
def getNumberOfInstances (compType : Option [ComponentType ]): Int = {
133
- if (compType.isDefined){
135
+ if (compType.isDefined) {
134
136
instanceDao.allInstances().count(i => i.componentType == compType.get)
135
137
} else {
136
138
instanceDao.allInstances().length
@@ -242,7 +244,6 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
242
244
OperationResult .IdUnknown
243
245
} else {
244
246
val matchedInstance = instanceDao.getInstance(matchedInstanceId).get
245
-
246
247
// Update list of matching results
247
248
instanceDao.addMatchingResult(matchedInstanceId, matchingSuccess)
248
249
// Update state of matchedInstance accordingly
@@ -257,18 +258,41 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
257
258
258
259
// Update link state
259
260
if (! matchingSuccess) {
260
- val link = InstanceLink (callerId, matchedInstanceId, LinkState . Failed )
261
- instanceDao.updateLink(link ) match {
261
+
262
+ setActiveLinksToFailed(matchedInstanceId ) match {
262
263
case Success (_) =>
263
- fireLinkStateChangedEvent(link)
264
264
OperationResult .Ok
265
- case Failure (_) => OperationResult .InternalError // Should not happen
265
+ case Failure (_) =>
266
+ // Message logged by method
267
+ OperationResult .InternalError
266
268
}
267
269
} else {
268
270
OperationResult .Ok
269
271
}
272
+ }
273
+ }
274
+
275
+
276
+ def setActiveLinksToFailed (failedInstanceId : Long ): Try [Unit ] = {
277
+
278
+ val linksToFailedInstance = instanceDao.getLinksTo(failedInstanceId)
279
+ var errors = false
270
280
281
+ for (link <- linksToFailedInstance) {
282
+ // Do not update outdated links
283
+ if (link.linkState == LinkState .Assigned ) {
284
+ val newLink = InstanceLink (link.idFrom, failedInstanceId, LinkState .Failed )
285
+ instanceDao.updateLink(newLink) match {
286
+ case Success (_) =>
287
+ fireLinkStateChangedEvent(link)
288
+ case Failure (ex) =>
289
+ errors = true
290
+ log.warning(s " There was a failure while updating the link state ${ex.getMessage}" )
291
+ }
292
+ }
271
293
}
294
+
295
+ if (errors) Failure (new RuntimeException (" Link updates unsuccessful" )) else Success ()
272
296
}
273
297
274
298
// scalastyle:off method.length
@@ -332,6 +356,7 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
332
356
Failure (ex)
333
357
}
334
358
}
359
+
335
360
// scalastyle:on method.length
336
361
337
362
/** *
@@ -569,6 +594,7 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
569
594
OperationResult .InvalidStateForOperation
570
595
}
571
596
}
597
+
572
598
// scalastyle:on method.length cyclomatic.complexity
573
599
574
600
/** *
@@ -723,21 +749,21 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
723
749
724
750
val f : Future [(OperationResult .Value , Option [String ])] =
725
751
(dockerActor ? LogsMessage (instance.dockerId.get, stdErrSelected, stream = false )) (configuration.dockerOperationTimeout).map {
726
- logVal : Any =>
727
- val logResult = logVal.asInstanceOf [Try [String ]]
728
- logResult match {
729
- case Success (logContent) =>
730
- (OperationResult .Ok , Some (logContent))
731
- case Failure (ex) =>
732
- log.warning(s " Failed to get logs from actor, exception: ${ex.getMessage}" )
733
- (OperationResult .InternalError , None )
734
- }
752
+ logVal : Any =>
753
+ val logResult = logVal.asInstanceOf [Try [String ]]
754
+ logResult match {
755
+ case Success (logContent) =>
756
+ (OperationResult .Ok , Some (logContent))
757
+ case Failure (ex) =>
758
+ log.warning(s " Failed to get logs from actor, exception: ${ex.getMessage}" )
759
+ (OperationResult .InternalError , None )
760
+ }
735
761
736
- }.recover {
737
- case ex : Exception =>
738
- fireDockerOperationErrorEvent(Some (instance), errorMessage = s " Failed to get logs with message: ${ex.getMessage}" )
739
- (OperationResult .InternalError , None )
740
- }
762
+ }.recover {
763
+ case ex : Exception =>
764
+ fireDockerOperationErrorEvent(Some (instance), errorMessage = s " Failed to get logs with message: ${ex.getMessage}" )
765
+ (OperationResult .InternalError , None )
766
+ }
741
767
Await .result(f, configuration.dockerOperationTimeout.duration)
742
768
}
743
769
}
@@ -752,21 +778,21 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
752
778
753
779
val f : Future [(OperationResult .Value , Option [Publisher [Message ]])] =
754
780
(dockerActor ? LogsMessage (instance.dockerId.get, stdErrSelected, stream = true )) (configuration.dockerOperationTimeout).map {
755
- publisherVal : Any =>
756
- val publisherResult = publisherVal.asInstanceOf [Try [Publisher [Message ]]]
757
- publisherResult match {
758
- case Success (publisher) =>
759
- (OperationResult .Ok , Some (publisher))
760
- case Failure (ex) =>
761
- log.warning(s " Failed to stream logs from actor, exception: ${ex.getMessage}" )
762
- (OperationResult .InternalError , None )
763
- }
781
+ publisherVal : Any =>
782
+ val publisherResult = publisherVal.asInstanceOf [Try [Publisher [Message ]]]
783
+ publisherResult match {
784
+ case Success (publisher) =>
785
+ (OperationResult .Ok , Some (publisher))
786
+ case Failure (ex) =>
787
+ log.warning(s " Failed to stream logs from actor, exception: ${ex.getMessage}" )
788
+ (OperationResult .InternalError , None )
789
+ }
764
790
765
- }.recover {
766
- case ex : Exception =>
767
- fireDockerOperationErrorEvent(Some (instance), errorMessage = s " Failed to stream logs with message: ${ex.getMessage}" )
768
- (OperationResult .InternalError , None )
769
- }
791
+ }.recover {
792
+ case ex : Exception =>
793
+ fireDockerOperationErrorEvent(Some (instance), errorMessage = s " Failed to stream logs with message: ${ex.getMessage}" )
794
+ (OperationResult .InternalError , None )
795
+ }
770
796
Await .result(f, configuration.dockerOperationTimeout.duration)
771
797
}
772
798
}
@@ -839,6 +865,7 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
839
865
}
840
866
}
841
867
}
868
+
842
869
// scalastyle:on method.length cyclomatic.complexity
843
870
844
871
/**
@@ -925,12 +952,12 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
925
952
* Handles a call to /command. container id and command must be present,
926
953
* Will run the command into the container with provide parameters
927
954
*
928
- * @param id container id the command will run on
929
- * @param command the command to run
930
- * Format is a single character [a-Z] or ctrl-<@value> where <v@alue> is one of: a-z, @, [, , or _
931
- * @param privileged runs the process with extended privileges
932
- * @param user A string value specifying the user, and optionally, group to run the process inside the container,
933
- * Format is one of: "user", "user:group", "uid", or "uid:gid".
955
+ * @param id container id the command will run on
956
+ * @param command the command to run
957
+ * Format is a single character [a-Z] or ctrl-<@value> where <v@alue> is one of: a-z, @, [, , or _
958
+ * @param privileged runs the process with extended privileges
959
+ * @param user A string value specifying the user, and optionally, group to run the process inside the container,
960
+ * Format is one of: "user", "user:group", "uid", or "uid:gid".
934
961
* @return
935
962
*/
936
963
def handleCommand (id : Long , command : String , privileged : Option [Boolean ], user : Option [String ]): OperationResult .Value = {
@@ -1082,6 +1109,7 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
1082
1109
instanceDao.addEventFor(link.idTo, event)
1083
1110
}
1084
1111
1112
+
1085
1113
private def countConsecutivePositiveMatchingResults (id : Long ): Int = {
1086
1114
if (! instanceDao.hasInstance(id) || instanceDao.getMatchingResultsFor(id).get.isEmpty) {
1087
1115
0
0 commit comments