This commit is contained in:
Liang Ding 2022-12-07 20:45:11 +08:00
parent a1c81fda03
commit e9c9f685f8
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
5 changed files with 35 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import (
"github.com/88250/gulu"
figure "github.com/common-nighthawk/go-figure"
"github.com/gofrs/flock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
@ -95,6 +96,10 @@ func Boot() {
SSL = *ssl
LogPath = filepath.Join(TempDir, "siyuan.log")
logging.SetLogPath(LogPath)
// 工作空间仅允许被一个内核进程伺服
tryLockWorkspace()
AppearancePath = filepath.Join(ConfDir, "appearance")
if "dev" == Mode {
ThemesPath = filepath.Join(WorkingDir, "appearance", "themes")
@ -154,6 +159,7 @@ var (
WorkingDir, _ = os.Getwd()
WorkspaceDir string // 工作空间目录路径
WorkspaceLock *flock.Flock // 工作空间锁
ConfDir string // 配置目录路径
DataDir string // 数据目录路径
RepoDir string // 仓库目录路径
@ -437,3 +443,27 @@ func GetDataAssetsAbsPath() (ret string) {
}
return
}
func tryLockWorkspace() {
WorkspaceLock = flock.New(filepath.Join(WorkspaceDir, ".lock"))
if err := WorkspaceLock.Lock(); nil != err {
logging.LogErrorf("lock workspace [%s] failed: %s", WorkspaceDir, err)
os.Exit(ExitCodeWorkspaceLocked)
}
}
func UnlockWorkspace() {
if nil == WorkspaceLock {
return
}
if err := WorkspaceLock.Unlock(); nil != err {
logging.LogErrorf("unlock workspace [%s] failed: %s", WorkspaceDir, err)
return
}
if err := os.Remove(filepath.Join(WorkspaceDir, ".lock")); nil != err {
logging.LogErrorf("remove workspace lock failed: %s", err)
return
}
}