Skip to content

Commit

Permalink
Improved handling of Pod events and synced square button color. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
lreimer committed Apr 30, 2016
1 parent c5331c7 commit b601bd5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ data class ClusterNode(val row: Int, val column: Int,
return this
}

fun update(p: Phase) {
if (active.get()) {
phase = p
}
}

fun deactivate(): ClusterNode {
active.compareAndSet(true, false)
phase = Phase.Unknown
phase = Phase.Terminated
return this
}

Expand Down
33 changes: 19 additions & 14 deletions src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNodeGrid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.slf4j.Logger
import java.util.concurrent.ExecutorService
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import javax.annotation.PreDestroy
import javax.enterprise.context.ApplicationScoped
import javax.enterprise.event.Event
import javax.enterprise.event.Observes
Expand Down Expand Up @@ -86,19 +87,27 @@ open class ClusterNodeGrid @Inject constructor(@Named("default")
}
}

@PreDestroy
open fun shutdown() {
deployments.clear()
grid.forEach { row ->
row.forEach { it.deactivate() }
}
}

private fun watch(row: Int, labels: MutableMap<String, String>) {
scheduler.scheduleWithFixedDelay({
if (deployments.deployments()[row] == null ) {
throw IllegalStateException("No deployment at $row to watch.")
}

val nodes = grid[row]
val active = nodes.filter { it.active.get() }
pods.all(labels).forEachIndexed { j, pod ->
val nodes = grid[row]
val active = nodes.filter { it.active.get() }

pods.all(labels).reversed().forEachIndexed { j, pod ->
val node = active.elementAtOrElse(j) { nodes[next(row)].activate() }
if (node.active.get()) {
node.phase = phase(pod)
node.update(phase(pod))
updateClusterNode(row, node)
}
}
Expand Down Expand Up @@ -131,10 +140,8 @@ open class ClusterNodeGrid @Inject constructor(@Named("default")
val node = grid[event.row][event.column]
val running = grid[event.row].count { it.active.get() }

executor.execute({
updateClusterNode(event.row, node.activate())
deployments.scale(event.row, running + 1)
})
updateClusterNode(event.row, node.activate())
executor.execute { deployments.scale(event.row, running + 1) }
}

/**
Expand All @@ -148,12 +155,10 @@ open class ClusterNodeGrid @Inject constructor(@Named("default")
val node = grid[event.row][event.column]
val running = grid[event.row].count { it.active.get() }

executor.execute({
deployments.scale(event.row, running - 1)
stopping(event.row, node)
node.deactivate()
stopped(event.row, node)
})
stopping(event.row, node)
node.deactivate()
executor.execute { deployments.scale(event.row, running - 1) }
stopped(event.row, node)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ fun main(args: Array<String>) {
val cdiContainer = getCdiContainer()
cdiContainer.boot();

// ensure we shutdown nicely on exit
Runtime.getRuntime().addShutdownHook(Thread() { cdiContainer.shutdown() })

// and initialize the CDI container control
val contextControl = cdiContainer.contextControl;
contextControl.startContext(Dependent::class.java)
Expand All @@ -71,4 +68,7 @@ fun main(args: Array<String>) {
} else {
logger.warning("No Leap Motion support.")
}

// ensure we shutdown nicely on exit
Runtime.getRuntime().addShutdownHook(Thread() { cdiContainer.shutdown() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ open class KubernetesDeployments @Inject constructor(private val client: Kuberne
operation.watch(this)
}

open fun clear() {
deployments.clear()
names.clear()
}

open fun deployments(): List<Deployment?> = deployments.toList()

open operator fun get(row: Int): Deployment? = deployments[row]
Expand Down

0 comments on commit b601bd5

Please sign in to comment.