@@ -29,7 +29,7 @@ class ServerService(
2929 private val logger = LogManager .getLogger(ServerService ::class .java)
3030
3131 override suspend fun attachServerHost (request : AttachServerHostRequest ): ServerHostDefinition {
32- val serverHost = ServerHost .fromDefinition(request.serverHost)
32+ val serverHost = ServerHost .fromDefinition(request.serverHost, authCallCredentials )
3333 try {
3434 hostRepository.delete(serverHost)
3535 hostRepository.save(serverHost)
@@ -39,21 +39,17 @@ class ServerService(
3939 logger.info(" Successfully registered ServerHost ${serverHost.id} ." )
4040
4141 coroutineScope {
42- val channel = serverHost.createChannel()
43- val stub =
44- ServerHostServiceGrpcKt .ServerHostServiceCoroutineStub (channel).withCallCredentials(authCallCredentials)
4542 serverRepository.findServersByHostId(serverHost.id).forEach { server ->
4643 logger.info(" Reattaching Server ${server.uniqueId} of group ${server.group} ..." )
4744 try {
48- val result = stub.reattachServer(server.toDefinition())
45+ val result = serverHost. stub.reattachServer(server.toDefinition())
4946 serverRepository.save(Server .fromDefinition(result))
5047 logger.info(" Success!" )
5148 } catch (e: Exception ) {
5249 logger.error(" Server was found to be offline, unregistering..." )
5350 serverRepository.delete(server)
5451 }
5552 }
56- channel.shutdown()
5753 }
5854 return serverHost.toDefinition()
5955 }
@@ -73,7 +69,7 @@ class ServerService(
7369 val server = serverRepository.findServerByNumerical(request.groupName, request.numericalId.toInt())
7470 ? : throw StatusException (Status .NOT_FOUND .withDescription(" No server was found matching this group and numerical id" ))
7571 try {
76- return stopServer(server.toDefinition())
72+ return stopServer(server.toDefinition(), request.stopCause )
7773 } catch (e: Exception ) {
7874 throw StatusException (
7975 Status .INTERNAL .withDescription(" Error occured whilest cleaning up stopped server: " ).withCause(e)
@@ -167,9 +163,7 @@ class ServerService(
167163 val numericalId = numericalIdRepository.findNextNumericalId(group.name)
168164 val server = buildServer(group, numericalId, forwardingSecret)
169165 serverRepository.save(server)
170- val channel = host.createChannel()
171- val stub = ServerHostServiceGrpcKt .ServerHostServiceCoroutineStub (channel)
172- .withCallCredentials(authCallCredentials)
166+ val stub = host.stub
173167 serverRepository.save(server)
174168 try {
175169 val result = stub.startServer(
@@ -179,12 +173,10 @@ class ServerService(
179173 .build()
180174 )
181175 serverRepository.save(Server .fromDefinition(result))
182- channel.shutdown()
183176 return result
184177 } catch (e: Exception ) {
185178 serverRepository.delete(server)
186179 numericalIdRepository.removeNumericalId(group.name, server.numericalId)
187- channel.shutdown()
188180 logger.error(" Error whilst starting server:" , e)
189181 throw e
190182 }
@@ -206,7 +198,7 @@ class ServerService(
206198 .setUniqueId(UUID .randomUUID().toString().replace(" -" , " " )).putAllCloudProperties(
207199 mapOf (
208200 * group.properties.entries.map { it.key to it.value }.toTypedArray(),
209- " forwarding-secret" to forwardingSecret,
201+ " forwarding-secret" to forwardingSecret
210202 )
211203 ).build()
212204 )
@@ -216,32 +208,30 @@ class ServerService(
216208 val server = serverRepository.find(request.serverId)
217209 ? : throw StatusException (Status .NOT_FOUND .withDescription(" No server was found matching this id." ))
218210 try {
219- val stopped = stopServer(server.toDefinition())
220- pubSubClient.publish(
221- " event" , ServerStopEvent .newBuilder()
222- .setServer(stopped)
223- .setStoppedAt(ProtoBufTimestamp .fromLocalDateTime(LocalDateTime .now()))
224- .setStopCause(request.stopCause)
225- .setTerminationMode(ServerTerminationMode .UNKNOWN_MODE ) // TODO: Add proto fields to make changing this possible
226- .build()
227- )
211+ val stopped = stopServer(server.toDefinition(), request.stopCause)
228212 return stopped
229213 } catch (e: Exception ) {
230214 throw StatusException (Status .INTERNAL .withDescription(" Error whilst stopping server" ).withCause(e))
231215 }
232216 }
233217
234- private suspend fun stopServer (server : ServerDefinition ): ServerDefinition {
218+ private suspend fun stopServer (server : ServerDefinition , cause : ServerStopCause = ServerStopCause . NATURAL_STOP ): ServerDefinition {
235219 val host = hostRepository.findServerHostById(server.hostId)
236220 ? : throw Status .NOT_FOUND
237221 .withDescription(" No server host was found matching this server." )
238222 .asRuntimeException()
239- val channel = host.createChannel()
240- val stub = ServerHostServiceGrpcKt .ServerHostServiceCoroutineStub (channel)
241- .withCallCredentials(authCallCredentials)
223+ val stub = host.stub
242224 try {
243225 val stopped = stub.stopServer(server)
244- channel.shutdown()
226+ pubSubClient.publish(
227+ " event" , ServerStopEvent .newBuilder()
228+ .setServer(stopped)
229+ .setStoppedAt(ProtoBufTimestamp .fromLocalDateTime(LocalDateTime .now()))
230+ .setStopCause(cause)
231+ .setTerminationMode(ServerTerminationMode .UNKNOWN_MODE ) // TODO: Add proto fields to make changing this possible
232+ .build()
233+ )
234+ serverRepository.delete(Server .fromDefinition(stopped))
245235 return stopped
246236 } catch (e: Exception ) {
247237 logger.error(" Server stop error occured:" , e)
0 commit comments