graceful shutdown
This commit is contained in:
@@ -7,10 +7,11 @@ import org.freedesktop.dbus.interfaces.DBusInterface
|
||||
import org.freedesktop.dbus.interfaces.DBusSigHandler
|
||||
import org.freedesktop.dbus.messages.DBusSignal
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.Queue
|
||||
import java.util.concurrent.BlockingQueue
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class DBus(private val q: Queue<Op>) {
|
||||
class DBus(private val q: BlockingQueue<Op>) {
|
||||
|
||||
private val thread = thread(
|
||||
start = true,
|
||||
@@ -27,7 +28,11 @@ class DBus(private val q: Queue<Op>) {
|
||||
log.info("signal: ${signal.op}")
|
||||
val op = Op.valueOf(signal.op)
|
||||
log.info("op: ${op}")
|
||||
q.offer(op)
|
||||
try {
|
||||
q.offer(op, 1, TimeUnit.SECONDS)
|
||||
} catch (e:InterruptedException) {
|
||||
log.debug("queue offer interrupted")
|
||||
}
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package net.eksb.obsdc
|
||||
|
||||
fun interface Handler {
|
||||
fun handle(op:Op)
|
||||
}
|
||||
@@ -8,6 +8,6 @@ object Main {
|
||||
fun main(args: Array<String>) {
|
||||
val q:BlockingQueue<Op> = LinkedBlockingQueue()
|
||||
DBus(q) // forks daemon thread
|
||||
ObsCommunity(q).run() // blocks
|
||||
Obs(q).run() // blocks
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,11 @@ import io.obswebsocket.community.client.model.SceneItem.Transform
|
||||
import io.obswebsocket.community.client.model.SceneItem.Transform.TransformBuilder
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.concurrent.BlockingQueue
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
// protocol docs: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md
|
||||
class ObsCommunity(private val q:BlockingQueue<Op>): AutoCloseable {
|
||||
class Obs(private val q:BlockingQueue<Op>): AutoCloseable {
|
||||
|
||||
private val panAmount = 50F
|
||||
|
||||
@@ -31,6 +33,16 @@ class ObsCommunity(private val q:BlockingQueue<Op>): AutoCloseable {
|
||||
}
|
||||
.build()
|
||||
|
||||
private var alive = true
|
||||
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(thread(start=false) {
|
||||
log.info("shutdown")
|
||||
alive = false
|
||||
controller.stop()
|
||||
})
|
||||
}
|
||||
|
||||
fun run() {
|
||||
controller.connect()
|
||||
}
|
||||
@@ -41,9 +53,14 @@ class ObsCommunity(private val q:BlockingQueue<Op>): AutoCloseable {
|
||||
|
||||
private fun ready() {
|
||||
log.info("ready")
|
||||
val op: Op = q.take() // blocks
|
||||
log.info("op: ${op}")
|
||||
op(op)
|
||||
while(alive) {
|
||||
val op: Op? = q.poll(1, TimeUnit.SECONDS) // blocks
|
||||
if (op != null) {
|
||||
log.info("op: ${op}")
|
||||
op(op)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun op(op:Op) {
|
||||
@@ -107,6 +124,6 @@ class ObsCommunity(private val q:BlockingQueue<Op>): AutoCloseable {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val log = LoggerFactory.getLogger(ObsCommunity::class.java)
|
||||
private val log = LoggerFactory.getLogger(Obs::class.java)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user