Skip to content

Commit

Permalink
コンソール上でボットの終了処理を実行した際に発生する不具合を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
kosugikun committed Mar 29, 2023
1 parent a691d0e commit 746c47c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
61 changes: 39 additions & 22 deletions src/main/java/dev/cosgy/textToSpeak/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import java.util.function.Consumer
import javax.swing.JOptionPane
import kotlin.system.exitProcess

class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: SettingsManager) {
Expand Down Expand Up @@ -74,46 +75,62 @@ class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: S
fun shutdown() {
if (shuttingDown) return
shuttingDown = true
threadpool.shutdownNow()
if (jda!!.status != JDA.Status.SHUTTING_DOWN) {
jda!!.guilds.forEach(Consumer { g: Guild ->
g.audioManager.closeAudioConnection()
val ah = g.audioManager.sendingHandler as AudioHandler?
if (ah != null) {
ah.stopAndClear()
ah.player.destroy()
val am = g.audioManager
if (am.isConnected) {
am.closeAudioConnection()
val ah = am.sendingHandler as AudioHandler?
if (ah != null) {
ah.stopAndClear()
ah.player.destroy()
}
}
})

// ここからJDAの終了処理
jda!!.shutdown()
// Wait for any remaining tasks to complete before shutting down the thread pool
threadpool.shutdown()
try {
if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) {
jda!!.shutdownNow()
if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) {
log.warn("JDAのシャットダウンがタイムアウトしました。")
if (!threadpool.awaitTermination(10, TimeUnit.SECONDS)) {
threadpool.shutdownNow()
if (!threadpool.awaitTermination(10, TimeUnit.SECONDS)) {
log.warn("Thread pool did not terminate")
}
}
} catch (e: InterruptedException) {
jda!!.shutdownNow()
threadpool.shutdownNow()
Thread.currentThread().interrupt()
log.warn("JDAのシャットダウンが中断されました。")
log.warn("Thread pool shutdown was interrupted")
}
}

dictionary?.close()
// 一時ファイルを削除
try {
FileUtils.cleanDirectory(File("tmp"))
FileUtils.cleanDirectory(File("wav"))
log.info("一時ファイルを削除しました。")
} catch (e: IOException) {
log.warn("一時ファイルの削除に失敗しました。")
// Shutdown JDA
jda?.shutdown()
try {
if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) {
log.warn("JDA did not shutdown properly")
}
} catch (e: InterruptedException) {
jda!!.shutdownNow()
Thread.currentThread().interrupt()
log.warn("JDA shutdown was interrupted")
}

dictionary?.close()
// Delete temporary files
try {
FileUtils.cleanDirectory(File("tmp"))
FileUtils.cleanDirectory(File("wav"))
log.info("Deleted temporary files")
} catch (e: IOException) {
log.warn("Failed to delete temporary files")
}

if (gui != null) gui!!.dispose()
exitProcess(0)
}


fun setGUI(gui: GUI?) {
this.gui = gui
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.utils.cache.CacheFlag
import org.slf4j.LoggerFactory
import sun.misc.Signal
import java.awt.Color
import java.io.IOException
import java.util.*
Expand Down Expand Up @@ -169,6 +170,10 @@ System.exit(1);
""".trimIndent())
exitProcess(1)
}
Runtime.getRuntime().addShutdownHook(Thread { bot.shutdown() })

Signal.handle(Signal("INT")) { _ ->
println("プログラムを終了しています...")
bot.shutdown()
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
return builder.toString()
}



// テキストファイルを作成するメソッド
@Throws(FileNotFoundException::class, UnsupportedEncodingException::class)
private fun createTmpTextFile(guildId: String, fileId: String, message: String): String {
Expand Down

0 comments on commit 746c47c

Please sign in to comment.