@@ -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
@@ -256,35 +258,41 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
256
258
257
259
// Update link state
258
260
if (! matchingSuccess) {
259
- val link = InstanceLink (callerId, matchedInstanceId, LinkState . Failed )
260
- instanceDao.updateLink(link ) match {
261
+
262
+ setActiveLinksToFailed(matchedInstanceId ) match {
261
263
case Success (_) =>
262
- fireLinkStateChangedEvent(link)
263
- handleLinksUpdate(matchedInstanceId)
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
}
270
-
271
272
}
272
273
}
273
274
274
275
275
- def handleLinksUpdate ( matchedInstanceId : Long ): Unit = {
276
+ def setActiveLinksToFailed ( failedInstanceId : Long ): Try [ Unit ] = {
276
277
277
- val getlinkFrom = instanceDao.getLinksTo(matchedInstanceId)
278
- for (linklist <- getlinkFrom) {
279
- if (linklist.linkState != LinkState .Failed ) {
280
- val link = InstanceLink (linklist.idFrom, matchedInstanceId, LinkState .Failed )
281
- instanceDao.updateLink(link) match {
278
+ val linksToFailedInstance = instanceDao.getLinksTo(failedInstanceId)
279
+ var errors = false
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 {
282
286
case Success (_) =>
283
287
fireLinkStateChangedEvent(link)
284
- case Failure (ex) => log.warning(s " There was a failure while updating the link state ${ex.getMessage}" )
288
+ case Failure (ex) =>
289
+ errors = true
290
+ log.warning(s " There was a failure while updating the link state ${ex.getMessage}" )
285
291
}
286
292
}
287
293
}
294
+
295
+ if (errors) Failure (new RuntimeException (" Link updates unsuccessful" )) else Success ()
288
296
}
289
297
290
298
// scalastyle:off method.length
0 commit comments