Skip to content

Commit

Permalink
Feature/issue 97/enhance lock file mechanism (#99)
Browse files Browse the repository at this point in the history
* fixes #97

* fixes #97
  • Loading branch information
dev-claw authored May 24, 2023
1 parent 2aa82a7 commit ed63d5c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
44 changes: 44 additions & 0 deletions vripper-core/src/main/kotlin/me/mnlr/vripper/listeners/AppLock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.mnlr.vripper.listeners

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
import org.springframework.context.ApplicationListener
import java.io.RandomAccessFile
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.io.path.pathString
import kotlin.system.exitProcess

class AppLock : ApplicationListener<ApplicationEnvironmentPreparedEvent> {
override fun onApplicationEvent(event: ApplicationEnvironmentPreparedEvent) {
val baseDir = event.environment.getProperty("base.dir")
val appDirName = event.environment.getProperty("base.dir.name")
if (baseDir == null || appDirName == null) {
if (baseDir == null) {
System.err.println("Property base.dir is undefined")
}
if (appDirName == null) {
System.err.println("Property base.dir.name is undefined")
}
System.err.println("Misconfiguration detected, quitting...")
exitProcess(-1)
}
val lock = Path(baseDir).resolve(appDirName).resolve("lock")
try {
val randomFile = RandomAccessFile(lock.pathString, "rw")
val channel = randomFile.channel
val fileLock = channel.tryLock()
if (fileLock == null) {
System.err.println("Another instance is already running in ${lock.parent.pathString}")
exitProcess(-1)
} else {
Runtime.getRuntime().addShutdownHook(Thread {
fileLock.release()
channel.close()
Files.deleteIfExists(lock)
})
}
} catch (e: Exception) {
println(e.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import javafx.stage.Stage
import javafx.stage.WindowEvent
import me.mnlr.vripper.event.ApplicationInitialized
import me.mnlr.vripper.gui.Styles
import me.mnlr.vripper.listeners.AppListener
import me.mnlr.vripper.listeners.AppLock
import me.mnlr.vripper.view.LoadingView
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
Expand Down Expand Up @@ -44,7 +44,7 @@ class VripperGuiApplication : App(
}
stage.addEventFilter(WindowEvent.WINDOW_SHOWN) {
val contextInitThread = Thread {
context = SpringApplicationBuilder(this.javaClass).listeners(AppListener())
context = SpringApplicationBuilder(this.javaClass).listeners(AppLock())
.run() //We start the application context and let Spring Boot to initialize itself
context.autowireCapableBeanFactory.autowireBean(this) //We ask the context to inject all needed dependencies into the current instence (if needed)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.mnlr.vripper

import me.mnlr.vripper.listeners.AppListener
import me.mnlr.vripper.listeners.AppLock
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder

@SpringBootApplication
class VripperWebApplication

fun main(args: Array<String>) {
SpringApplicationBuilder(VripperWebApplication::class.java).listeners(AppListener()).run(*args)
SpringApplicationBuilder(VripperWebApplication::class.java).listeners(AppLock()).run(*args)
}

0 comments on commit ed63d5c

Please sign in to comment.