Compare commits

...

2 Commits

Author SHA1 Message Date
d433179db4 better error handling and log messages 2023-10-24 08:17:13 -04:00
70a7f3768e fix Obs shutdown hook 2023-10-24 07:43:12 -04:00
3 changed files with 57 additions and 34 deletions

View File

@@ -3,6 +3,7 @@ package net.eksb.obsdc
import org.freedesktop.dbus.annotations.DBusInterfaceName
import org.freedesktop.dbus.connections.impl.DBusConnection
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder
import org.freedesktop.dbus.exceptions.DBusException
import org.freedesktop.dbus.interfaces.DBusInterface
import org.freedesktop.dbus.interfaces.DBusSigHandler
import org.freedesktop.dbus.messages.DBusSignal
@@ -27,7 +28,11 @@ class DBus(private val opRunner:OpRunner): AutoCloseable {
init {
// These lines are not necessary to handle signals, but are necessary to register methods.
dbus.requestBusName("net.eksb.obsdc")
try {
dbus.requestBusName(BUS_NAME)
} catch (e:DBusException) {
error("Error requesting bus. Already running?")
}
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal ->
@@ -36,14 +41,19 @@ class DBus(private val opRunner:OpRunner): AutoCloseable {
log.debug("op: ${op}")
opRunner.run(op)
}
log.debug("DBUS initialized")
}
override fun close() {
dbus.releaseBusName(BUS_NAME)
dbus.close()
}
companion object {
private const val BUS_NAME = "net.eksb.obsdc"
private val log = LoggerFactory.getLogger(DBus::class.java)
}
}
inline fun <reified T: DBusSignal> DBusConnection.addSigHandler(handler: DBusSigHandler<T>) {
addSigHandler(T::class.java, handler)

View File

@@ -1,8 +1,11 @@
package net.eksb.obsdc
import org.slf4j.LoggerFactory
object Main {
@JvmStatic
fun main(args: Array<String>) {
try {
val config = CONFIG_FILE.properties()
Obs(
host = config.getProperty("host") ?: "localhost",
@@ -14,5 +17,10 @@ object Main {
waitForShutdown()
}
}
} catch (e:Exception) {
log.error(e.message, e)
}
}
private val log = LoggerFactory.getLogger(Main::class.java)
}

View File

@@ -48,7 +48,7 @@ class Obs(
.host(host)
.port(port)
.password(password)
.autoConnect(true)
.autoConnect(false)
.connectionTimeout(connectionTimeout)
.lifecycle()
.onReady(::onReady)
@@ -67,7 +67,11 @@ class Obs(
// OBSRemoteController starts a non-daemon thread. It probably should not do that.
// Kill it on shutdown.
addShutdownHook {
controller.stop()
close()
}
// connect() blocks until OBS is up, so fork it.
thread(name="obs-init-connect", isDaemon=true, start=true) {
controller.connect()
}
}
@@ -112,24 +116,26 @@ class Obs(
/**
* Thread that runs submitted requests from [q] when [ready].
*/
private val opThread = thread(name="obs-op", isDaemon=true, start=true) {
private val opThread = thread(name="obs-op", isDaemon=false, start=true) {
while(!closed.get()) {
val req = q.take()
val req = try {
q.take()
} catch (e:InterruptedException) {
log.debug("interrupted taking req")
continue
}
log.debug("got req: ${req}, wait for ready")
try {
ready.enter {
log.debug("ready")
if (!req.expired()) {
try {
req.block.invoke(controller)
} catch (e:InterruptedException) {
log.debug("interrupted")
throw e
}
}
} catch (e:Exception) {
log.error("req ${req} failed", e )
}
}
}
}
log.debug("done")
}
@@ -147,7 +153,6 @@ class Obs(
}
override fun close() {
log.debug("close")
closed.set(true)
opThread.interrupt()
controller.disconnect()