@@ -10,7 +10,7 @@ import de.upb.cs.swt.delphi.instanceregistry.Docker.{ContainerAlreadyStoppedExce
10
10
import akka .stream .scaladsl .{Keep , Sink , Source }
11
11
import akka .stream .{ActorMaterializer , Materializer , OverflowStrategy }
12
12
import de .upb .cs .swt .delphi .instanceregistry .connection .RestClient
13
- import de .upb .cs .swt .delphi .instanceregistry .daos .{ DynamicInstanceDAO , InstanceDAO }
13
+ import de .upb .cs .swt .delphi .instanceregistry .daos .InstanceDAO
14
14
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .InstanceEnums .{ComponentType , InstanceState }
15
15
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .LinkEnums .LinkState
16
16
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model ._
@@ -20,15 +20,14 @@ import scala.concurrent.{Await, ExecutionContext, Future}
20
20
import scala .language .postfixOps
21
21
import scala .util .{Failure , Success , Try }
22
22
23
- class RequestHandler (configuration : Configuration , connection : DockerConnection ) extends AppLogging {
23
+ class RequestHandler (configuration : Configuration , instanceDao : InstanceDAO , connection : DockerConnection ) extends AppLogging {
24
24
25
25
26
26
27
27
implicit val system : ActorSystem = Registry .system
28
28
implicit val materializer : Materializer = ActorMaterializer ()
29
29
implicit val ec : ExecutionContext = system.dispatcher
30
30
31
- private [instanceregistry] val instanceDao : InstanceDAO = new DynamicInstanceDAO (configuration)
32
31
33
32
val (eventActor, eventPublisher) = Source .actorRef[RegistryEvent ](10 , OverflowStrategy .dropNew)
34
33
.toMat(Sink .asPublisher(fanout = true ))(Keep .both)
@@ -67,20 +66,18 @@ class RequestHandler(configuration: Configuration, connection: DockerConnection)
67
66
* @return Newly assigned ID if successful
68
67
*/
69
68
def handleRegister (instance : Instance ): Try [Long ] = {
70
- val newID = generateNextId()
71
69
72
- log.info(s " Assigned new id $newID to registering instance with name ${instance.name}. " )
73
-
74
- val newInstance = Instance (id = Some (newID), name = instance.name, host = instance.host,
70
+ val noIdInstance = Instance (id = None , name = instance.name, host = instance.host,
75
71
portNumber = instance.portNumber, componentType = instance.componentType,
76
72
dockerId = None , instanceState = InstanceState .Running , labels = instance.labels,
77
73
linksTo = List .empty[InstanceLink ], linksFrom = List .empty[InstanceLink ])
78
74
79
- instanceDao.addInstance(newInstance) match {
80
- case Success (_) =>
81
- fireNumbersChangedEvent(newInstance.componentType)
82
- fireInstanceAddedEvent(newInstance)
83
- Success (newID)
75
+ instanceDao.addInstance(noIdInstance) match {
76
+ case Success (id) =>
77
+ fireNumbersChangedEvent(instanceDao.getInstance(id).get.componentType)
78
+ fireInstanceAddedEvent(instanceDao.getInstance(id).get)
79
+ log.info(s " Assigned new id $id to registering instance with name ${instance.name}. " )
80
+ Success (id)
84
81
case Failure (x) => Failure (x)
85
82
}
86
83
}
@@ -243,48 +240,68 @@ class RequestHandler(configuration: Configuration, connection: DockerConnection)
243
240
}
244
241
245
242
def handleDeploy (componentType : ComponentType , name : Option [String ]): Try [Long ] = {
246
- val newId = generateNextId()
247
-
248
243
log.info(s " Deploying container of type $componentType" )
244
+ val instance = Instance (None ,
245
+ " " ,
246
+ - 1L ,
247
+ name.getOrElse(s " Generic $componentType" ),
248
+ componentType,
249
+ None ,
250
+ InstanceState .Deploying ,
251
+ List .empty[String ],
252
+ List .empty[InstanceLink ],
253
+ List .empty[InstanceLink ]
254
+ )
255
+
256
+ instanceDao.addInstance(instance) match {
257
+ case Success (id) =>
258
+ implicit val timeout : Timeout = configuration.dockerOperationTimeout
249
259
250
- implicit val timeout : Timeout = configuration.dockerOperationTimeout
251
-
252
- val future : Future [Any ] = dockerActor ? create(componentType, newId)
253
- val deployResult = Await .result(future, timeout.duration).asInstanceOf [Try [(String , String , Int )]]
254
-
255
- deployResult match {
256
- case Failure (ex) =>
257
- log.error(s " Failed to deploy container, docker host not reachable. " )
258
- fireDockerOperationErrorEvent(None , s " Deploy failed with message: ${ex.getMessage}" )
259
- Failure (new RuntimeException (s " Failed to deploy container, docker host not reachable ( ${ex.getMessage}). " ))
260
- case Success ((dockerId, host, port)) =>
261
- val normalizedHost = host.substring(1 ,host.length - 1 )
262
- log.info(s " Deployed new container with id $dockerId, host $normalizedHost and port $port. " )
263
-
264
- val newInstance = Instance (Some (newId),
265
- normalizedHost,
266
- port,
267
- name.getOrElse(s " Generic $componentType" ),
268
- componentType,
269
- Some (dockerId),
270
- InstanceState .Deploying ,
271
- List .empty[String ],
272
- List .empty[InstanceLink ],
273
- List .empty[InstanceLink ]
274
- )
275
- log.info(s " Registering instance $newInstance.... " )
276
-
277
- instanceDao.addInstance(newInstance) match {
278
- case Success (_) =>
279
- log.info(" Successfully registered." )
280
- fireInstanceAddedEvent(newInstance)
281
- fireNumbersChangedEvent(newInstance.componentType)
282
- Success (newId)
283
- case Failure (x) =>
284
- log.info(s " Failed to register. Exception: $x" )
285
- Failure (x)
260
+ val future : Future [Any ] = dockerActor ? create(componentType, id)
261
+ val deployResult = Await .result(future, timeout.duration).asInstanceOf [Try [(String , String , Int )]]
262
+
263
+ deployResult match {
264
+ case Failure (ex) =>
265
+ log.error(s " Failed to deploy container, docker host not reachable. " )
266
+ instanceDao.removeInstance(id)
267
+ fireDockerOperationErrorEvent(None , s " Deploy failed with message: ${ex.getMessage}" )
268
+ Failure (new RuntimeException (s " Failed to deploy container, docker host not reachable ( ${ex.getMessage}). " ))
269
+ case Success ((dockerId, host, port)) =>
270
+ val normalizedHost = host.substring(1 , host.length - 1 )
271
+ log.info(s " Deployed new container with id $dockerId, host $normalizedHost and port $port. " )
272
+
273
+ val newInstance = Instance (Some (id),
274
+ normalizedHost,
275
+ port,
276
+ name.getOrElse(s " Generic $componentType" ),
277
+ componentType,
278
+ Some (dockerId),
279
+ InstanceState .Deploying ,
280
+ List .empty[String ],
281
+ List .empty[InstanceLink ],
282
+ List .empty[InstanceLink ]
283
+ )
284
+
285
+ instanceDao.updateInstance(newInstance) match {
286
+ case Success (_) =>
287
+ log.info(" Successfully registered." )
288
+ fireInstanceAddedEvent(newInstance)
289
+ fireNumbersChangedEvent(newInstance.componentType)
290
+ Success (id)
291
+ case Failure (x) =>
292
+ log.info(s " Failed to register. Exception: $x" )
293
+ Failure (x)
294
+ }
286
295
}
296
+ case Failure (ex) =>
297
+ Failure (ex)
287
298
}
299
+
300
+
301
+
302
+
303
+
304
+
288
305
}
289
306
290
307
/** *
@@ -962,14 +979,6 @@ class RequestHandler(configuration: Configuration, connection: DockerConnection)
962
979
963
980
}
964
981
965
- private def generateNextId (): Long = {
966
- if (instanceDao.allInstances().isEmpty) {
967
- 0L
968
- } else {
969
- (instanceDao.allInstances().map(i => i.id.getOrElse(0L )) max) + 1L
970
- }
971
- }
972
-
973
982
private def assignmentAllowed (instanceType : ComponentType ) : Boolean = {
974
983
instanceType == ComponentType .Crawler || instanceType == ComponentType .WebApi || instanceType == ComponentType .WebApp
975
984
}
0 commit comments