mirror of
https://github.com/corda/corda.git
synced 2025-06-01 15:10:54 +00:00
Fix node lock to work on windows where automatic file deletion on exit does not work reliably.
This commit is contained in:
parent
8fdfed9392
commit
5a4309abf0
@ -23,9 +23,7 @@ import org.glassfish.jersey.servlet.ServletContainer
|
|||||||
import java.io.RandomAccessFile
|
import java.io.RandomAccessFile
|
||||||
import java.lang.management.ManagementFactory
|
import java.lang.management.ManagementFactory
|
||||||
import java.nio.channels.FileLock
|
import java.nio.channels.FileLock
|
||||||
import java.nio.file.Files
|
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.nio.file.StandardOpenOption
|
|
||||||
import java.time.Clock
|
import java.time.Clock
|
||||||
import javax.management.ObjectName
|
import javax.management.ObjectName
|
||||||
|
|
||||||
@ -155,20 +153,20 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration
|
|||||||
// twice with the same directory: that's a user error and we should bail out.
|
// twice with the same directory: that's a user error and we should bail out.
|
||||||
val pidPath = dir.resolve("process-id")
|
val pidPath = dir.resolve("process-id")
|
||||||
val file = pidPath.toFile()
|
val file = pidPath.toFile()
|
||||||
if (file.exists()) {
|
if (!file.exists()) {
|
||||||
val f = RandomAccessFile(file, "rw")
|
file.createNewFile()
|
||||||
val l = f.channel.tryLock()
|
|
||||||
if (l == null) {
|
|
||||||
println("It appears there is already a node running with the specified data directory $dir")
|
|
||||||
println("Shut that other node down and try again. It may have process ID ${file.readText()}")
|
|
||||||
System.exit(1)
|
|
||||||
}
|
|
||||||
nodeFileLock = l
|
|
||||||
}
|
}
|
||||||
|
file.deleteOnExit()
|
||||||
|
val f = RandomAccessFile(file, "rw")
|
||||||
|
val l = f.channel.tryLock()
|
||||||
|
if (l == null) {
|
||||||
|
println("It appears there is already a node running with the specified data directory $dir")
|
||||||
|
println("Shut that other node down and try again. It may have process ID ${file.readText()}")
|
||||||
|
System.exit(1)
|
||||||
|
}
|
||||||
|
nodeFileLock = l
|
||||||
val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0]
|
val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0]
|
||||||
Files.write(pidPath, ourProcessID.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
|
f.setLength(0)
|
||||||
pidPath.toFile().deleteOnExit()
|
f.write(ourProcessID.toByteArray())
|
||||||
if (nodeFileLock == null)
|
}
|
||||||
nodeFileLock = RandomAccessFile(file, "rw").channel.lock()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user