Skip to content

Commit

Permalink
Add submitWithCatch() for ExecutorService
Browse files Browse the repository at this point in the history
  • Loading branch information
onoderis committed Mar 1, 2018
1 parent e4d58dc commit 3bd1c35
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/failchat/FailchatMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -107,7 +109,7 @@ private fun handleProgramArguments(args: Array<String>) {
}
}

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<Pair<EmoticonLoader<out Emoticon>, EmoticonStoreOptions>> = listOf(
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/failchat/gui/GuiEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/failchat/util/Concurrent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,3 +34,13 @@ fun ScheduledExecutorService.scheduleWithCatch(delay: Duration, command: () -> U
inline var <T> AtomicReference<T>.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)
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/failchat/peka2tv/Peka2tvApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3bd1c35

Please sign in to comment.