Fix multiple instances running from same dataDir (#2647)
* Fix multiple instances running from same dataDir * Add exclusive lock on lock file * Unlock lock file on process exit * Fix minor issues in lock file implementation
This commit is contained in:
parent
5e3f3db165
commit
b0a4a9ef84
|
@ -8,7 +8,7 @@
|
||||||
{.push raises: [].}
|
{.push raises: [].}
|
||||||
|
|
||||||
import
|
import
|
||||||
std/[os, enumutils],
|
std/[os, enumutils, exitprocs],
|
||||||
confutils,
|
confutils,
|
||||||
confutils/std/net,
|
confutils/std/net,
|
||||||
chronicles,
|
chronicles,
|
||||||
|
@ -60,7 +60,30 @@ proc run(
|
||||||
if pathExists.isErr():
|
if pathExists.isErr():
|
||||||
fatal "Failed to create data directory",
|
fatal "Failed to create data directory",
|
||||||
dataDir = config.dataDir, error = pathExists.error
|
dataDir = config.dataDir, error = pathExists.error
|
||||||
quit 1
|
quit QuitFailure
|
||||||
|
|
||||||
|
# Make sure multiple instances to the same dataDir do not exist
|
||||||
|
let
|
||||||
|
lockFilePath = config.dataDir.string / "fluffy.lock"
|
||||||
|
lockFlags = {OpenFlags.Create, OpenFlags.Read, OpenFlags.Write}
|
||||||
|
lockFileHandleResult = openFile(lockFilePath, lockFlags)
|
||||||
|
|
||||||
|
if lockFileHandleResult.isErr():
|
||||||
|
fatal "Failed to open lock file", error = ioErrorMsg(lockFileHandleResult.error)
|
||||||
|
quit QuitFailure
|
||||||
|
|
||||||
|
let lockFileHandle = lockFile(lockFileHandleResult.value(), LockType.Exclusive)
|
||||||
|
if lockFileHandle.isErr():
|
||||||
|
fatal "Please ensure no other fluffy instances are running with the same data directory",
|
||||||
|
dataDir = config.dataDir
|
||||||
|
quit QuitFailure
|
||||||
|
|
||||||
|
let lockFileIoHandle = lockFileHandle.value()
|
||||||
|
addExitProc(
|
||||||
|
proc() =
|
||||||
|
discard unlockFile(lockFileIoHandle)
|
||||||
|
discard closeFile(lockFileIoHandle.handle)
|
||||||
|
)
|
||||||
|
|
||||||
## Network configuration
|
## Network configuration
|
||||||
let
|
let
|
||||||
|
|
Loading…
Reference in New Issue