diff --git a/src/main/kotlin/failchat/FailchatMain.kt b/src/main/kotlin/failchat/FailchatMain.kt index 7ff4d127..1df711aa 100644 --- a/src/main/kotlin/failchat/FailchatMain.kt +++ b/src/main/kotlin/failchat/FailchatMain.kt @@ -18,6 +18,7 @@ import failchat.reporter.EventReporter import failchat.reporter.UserIdLoader import failchat.twitch.BttvGlobalEmoticonLoader import failchat.twitch.TwitchEmoticonLoader +import failchat.util.submitWithCatch import failchat.viewers.ShowViewersCountWsHandler import failchat.viewers.ViewersCountWsHandler import failchat.ws.server.DeleteWsMessageHandler @@ -33,6 +34,7 @@ import java.net.InetSocketAddress import java.net.ServerSocket import java.nio.file.Path import java.util.concurrent.ExecutorService +import java.util.concurrent.Future import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit import kotlin.concurrent.thread @@ -107,7 +109,7 @@ private fun handleProgramArguments(args: Array) { } } -private fun loadEmoticonsAsync(executor: ExecutorService) = executor.submit { +private fun loadEmoticonsAsync(executor: ExecutorService): Future<*> = executor.submitWithCatch { val manager: EmoticonManager = kodein.instance() val storage: EmoticonStorage = kodein.instance() val loadersAndOptions: List, EmoticonStoreOptions>> = listOf( diff --git a/src/main/kotlin/failchat/gui/GuiEventHandler.kt b/src/main/kotlin/failchat/gui/GuiEventHandler.kt index 3bec125a..f9741ad0 100644 --- a/src/main/kotlin/failchat/gui/GuiEventHandler.kt +++ b/src/main/kotlin/failchat/gui/GuiEventHandler.kt @@ -2,6 +2,7 @@ package failchat.gui import com.fasterxml.jackson.databind.ObjectMapper import failchat.AppStateManager +import failchat.util.submitWithCatch import failchat.ws.server.WsServer import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -24,26 +25,26 @@ class GuiEventHandler( private val executor = Executors.newSingleThreadExecutor() fun startChat() { - executor.submit { + executor.submitWithCatch { appStateManager.startChat() } } fun stopChat() { - executor.submit { + executor.submitWithCatch { appStateManager.stopChat() } } fun shutDown() { - executor.submit { + executor.submitWithCatch { appStateManager.shutDown() } executor.shutdown() } fun notifyViewersCountToggled(show: Boolean) { - executor.submit { + executor.submitWithCatch { val messageNode = objectMapper.createObjectNode().apply { put("type", "show-viewers-count") putObject("content").apply { diff --git a/src/main/kotlin/failchat/util/Concurrent.kt b/src/main/kotlin/failchat/util/Concurrent.kt index aeb66fb1..8fc700ef 100644 --- a/src/main/kotlin/failchat/util/Concurrent.kt +++ b/src/main/kotlin/failchat/util/Concurrent.kt @@ -3,6 +3,8 @@ package failchat.util import org.slf4j.Logger import org.slf4j.LoggerFactory import java.time.Duration +import java.util.concurrent.ExecutorService +import java.util.concurrent.Future import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledFuture import java.util.concurrent.TimeUnit @@ -32,3 +34,13 @@ fun ScheduledExecutorService.scheduleWithCatch(delay: Duration, command: () -> U inline var AtomicReference.value get(): T = this.get() set(value: T) = this.set(value) + +fun ExecutorService.submitWithCatch(task: () -> Unit): Future<*> { + return submit { + try { + task.invoke() + } catch (t: Throwable) { + log.error("Uncaught exception", t) + } + } +} diff --git a/src/test/kotlin/failchat/peka2tv/Peka2tvApiTest.kt b/src/test/kotlin/failchat/peka2tv/Peka2tvApiTest.kt index 9f79e466..78249b46 100644 --- a/src/test/kotlin/failchat/peka2tv/Peka2tvApiTest.kt +++ b/src/test/kotlin/failchat/peka2tv/Peka2tvApiTest.kt @@ -19,7 +19,7 @@ class Peka2tvApiTest { val config = loadConfig() } - val apiClient = Peka2tvApiClient(OkHttpClient(), config.getString("peka2tv.socketio-url")) + val apiClient = Peka2tvApiClient(OkHttpClient(), config.getString("peka2tv.api-url")) @Test fun emoticonApiTest() {