@@ -35,7 +35,8 @@ import io.fabric8.kubernetes.client.informers.SharedIndexInformer
3535import io.fabric8.kubernetes.client.informers.cache.Lister
3636import io.fabric8.kubernetes.client.internal.readiness.Readiness
3737import kotlinx.coroutines.CancellationException
38- import kotlinx.coroutines.GlobalScope
38+ import kotlinx.coroutines.CoroutineScope
39+ import kotlinx.coroutines.Dispatchers
3940import kotlinx.coroutines.channels.Channel
4041import kotlinx.coroutines.delay
4142import kotlinx.coroutines.launch
@@ -59,8 +60,10 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
5960
6061 private val logger = KotlinLogging .logger {}
6162
63+ private val scope = CoroutineScope (Dispatchers .Default )
64+
6265 suspend fun run () {
63- GlobalScope .launch { scheduleAdditionalEvents() }
66+ scope .launch { scheduleAdditionalEvents() }
6467 while (true ) {
6568 try {
6669 receiveAndHandleEvent()
@@ -284,7 +287,7 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
284287 }
285288 }
286289
287- private fun checkForObsoleteInstances () {
290+ private suspend fun checkForObsoleteInstances () {
288291 for (shinyProxy in shinyProxyLister.list()) {
289292 if (shinyProxy.status.instances.size > 1 ) {
290293 // this SP has more than one instance -> check if some of them are obsolete
@@ -317,7 +320,7 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
317320 }
318321 }
319322
320- fun deleteSingleShinyProxyInstance (shinyProxy : ShinyProxy , shinyProxyInstance : ShinyProxyInstance ) {
323+ suspend fun deleteSingleShinyProxyInstance (shinyProxy : ShinyProxy , shinyProxyInstance : ShinyProxyInstance ) {
321324 logger.info { " ${shinyProxy.logPrefix(shinyProxyInstance)} DeleteSingleShinyProxyInstance [Step 1/3]: Update status" }
322325 // Important: update status BEFORE deleting, otherwise we will start reconciling this instance, before it's completely deleted
323326 updateStatus(shinyProxy) {
@@ -329,7 +332,7 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
329332 logger.info { " ${shinyProxy.logPrefix(shinyProxyInstance)} DeleteSingleShinyProxyInstance [Step 2/3]: Update Ingress" }
330333 ingressController.onRemoveInstance(shinyProxy, shinyProxyInstance)
331334
332- GlobalScope .launch { // run async
335+ scope .launch { // run async
333336 // delete resources after delay of 30 seconds to ensure all routes are updated before deleting replicaset
334337 delay(30_000 )
335338 logger.info { " ${shinyProxy.logPrefix(shinyProxyInstance)} DeleteSingleShinyProxyInstance [Step 3/3]: Delete resources" }
0 commit comments