From 4ef219a8c13524cafb354ad09c32ade9fa189bba Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 23 Mar 2024 23:40:27 +0200 Subject: [PATCH] Implement graceful shutdown --- .../kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt | 13 ++++++++++++- .../nuculabs/imagetagger/ui/MainPageController.kt | 11 +++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt b/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt index 287d241..9c6188d 100644 --- a/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt +++ b/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt @@ -5,12 +5,17 @@ import javafx.application.Application import javafx.fxml.FXMLLoader import javafx.scene.Scene import javafx.stage.Stage +import java.util.logging.Logger class MainPage : Application() { + private val logger: Logger = Logger.getLogger("MainPage") + + private lateinit var fxmlLoader: FXMLLoader + override fun start(stage: Stage) { ImageTagsPrediction.getInstance() - val fxmlLoader = FXMLLoader(MainPage::class.java.getResource("main-window-view.fxml")) + fxmlLoader = FXMLLoader(MainPage::class.java.getResource("main-window-view.fxml")) val scene = Scene(fxmlLoader.load(), 640.0, 760.0) stage.title = "Image Tagger" stage.scene = scene @@ -18,6 +23,12 @@ class MainPage : Application() { stage.minHeight = 760.0 stage.show() } + + override fun stop() { + logger.info("Stop called.") + val controller = fxmlLoader.getController() + controller.shutdown() + } } fun main(args: Array) { diff --git a/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt b/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt index e87f20c..9ed43e2 100644 --- a/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt +++ b/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt @@ -18,6 +18,8 @@ import javax.imageio.ImageIO class MainPageController { + private val logger: Logger = Logger.getLogger("MainPageController") + /** * The thread pool worker pool. */ @@ -49,8 +51,6 @@ class MainPageController { private val imageTagsPrediction = ImageTagsPrediction.getInstance() - private val logger: Logger = Logger.getLogger("MainPageController") - @FXML private lateinit var progressBar: ProgressBar @@ -143,4 +143,11 @@ class MainPageController { logger.info("Finished processing images.") } } + + /** + * Shuts down the MainPageController. + */ + fun shutdown() { + workerPool.shutdownNow() + } } \ No newline at end of file