mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-01-23 02:34:42 +00:00
Add custom file lock
This commit is contained in:
parent
2ef1a93eaf
commit
795771e3d6
3 changed files with 50 additions and 22 deletions
37
pkg/os/flock.go
Normal file
37
pkg/os/flock.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package os
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
)
|
||||
|
||||
type Flock struct {
|
||||
f *flock.Flock
|
||||
}
|
||||
|
||||
func NewFileLock(path string) (*Flock, error) {
|
||||
if path == "" {
|
||||
path = os.TempDir() + string(os.PathSeparator) + "cloud_game.lock"
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0770); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
f, err := os.Create(path)
|
||||
defer func() { _ = f.Close() }()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
f := Flock{
|
||||
f: flock.New(path),
|
||||
}
|
||||
|
||||
return &f, nil
|
||||
}
|
||||
|
||||
func (f *Flock) Lock() error { return f.f.Lock() }
|
||||
func (f *Flock) Unlock() error { return f.f.Unlock() }
|
||||
|
|
@ -28,6 +28,10 @@ func CheckCreateDir(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func MakeDirAll(path string) error {
|
||||
return os.MkdirAll(path, os.ModeDir|os.ModePerm)
|
||||
}
|
||||
|
||||
func ExpectTermination() chan struct{} {
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
package manager
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/giongto35/cloud-game/v3/pkg/config"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/logger"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/os"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/worker/caged/libretro/repo"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/worker/caged/libretro/repo/arch"
|
||||
"github.com/gofrs/flock"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
|
|
@ -18,29 +15,20 @@ type Manager struct {
|
|||
repo repo.Repository
|
||||
altRepo repo.Repository
|
||||
client Downloader
|
||||
fmu *flock.Flock
|
||||
fmu *os.Flock
|
||||
log *logger.Logger
|
||||
}
|
||||
|
||||
func NewRemoteHttpManager(conf config.LibretroConfig, log *logger.Logger) Manager {
|
||||
repoConf := conf.Cores.Repo.Main
|
||||
altRepoConf := conf.Cores.Repo.Secondary
|
||||
// used for synchronization of multiple process
|
||||
fileLock := conf.Cores.Repo.ExtLock
|
||||
if fileLock == "" {
|
||||
fileLock = os.TempDir() + string(os.PathSeparator) + "cloud_game.lock"
|
||||
}
|
||||
log.Debug().Msgf("Using .lock file: %v", fileLock)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(fileLock), 0770); err != nil {
|
||||
log.Error().Err(err).Msgf("couldn't create lock")
|
||||
} else {
|
||||
f, err := os.Create(fileLock)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("couldn't create lock")
|
||||
}
|
||||
_ = f.Close()
|
||||
// used for synchronization of multiple process
|
||||
flock, err := os.NewFileLock(conf.Cores.Repo.ExtLock)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("couldn't make file lock")
|
||||
}
|
||||
|
||||
ar, err := arch.Guess()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("couldn't get Libretro core file extension")
|
||||
|
|
@ -50,7 +38,7 @@ func NewRemoteHttpManager(conf config.LibretroConfig, log *logger.Logger) Manage
|
|||
BasicManager: BasicManager{Conf: conf},
|
||||
arch: ar,
|
||||
client: NewDefaultDownloader(log),
|
||||
fmu: flock.New(fileLock),
|
||||
fmu: flock,
|
||||
log: log,
|
||||
}
|
||||
|
||||
|
|
@ -71,8 +59,7 @@ func CheckCores(conf config.Emulator, log *logger.Logger) error {
|
|||
log.Info().Msg("Starting Libretro cores sync...")
|
||||
coreManager := NewRemoteHttpManager(conf.Libretro, log)
|
||||
// make a dir for cores
|
||||
dir := coreManager.Conf.GetCoresStorePath()
|
||||
if err := os.MkdirAll(dir, os.ModeDir|os.ModePerm); err != nil {
|
||||
if err := os.MakeDirAll(coreManager.Conf.GetCoresStorePath()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := coreManager.Sync(); err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue