-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/issue 97/enhance lock file mechanism (#99)
- Loading branch information
Showing
3 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
vripper-core/src/main/kotlin/me/mnlr/vripper/listeners/AppLock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
vripper-web/src/main/kotlin/me/mnlr/vripper/VripperWebApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |