Skip to content

Commit

Permalink
add file locking
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-obx committed Aug 13, 2024
1 parent 2316321 commit 6606475
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 24 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/objectbox/objectbox-generator

go 1.11

require github.com/google/flatbuffers v23.5.26+incompatible
require (
github.com/gofrs/flock v0.8.1
github.com/google/flatbuffers v23.5.26+incompatible
)

require (
golang.org/x/sys v0.24.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/google/flatbuffers v23.5.26+incompatible h1:M9dgRyhJemaM4Sw8+66GHBu8ioaQmyPLg1b8VwK5WJg=
github.com/google/flatbuffers v23.5.26+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
68 changes: 47 additions & 21 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"time"

"github.com/gofrs/flock"
"github.com/objectbox/objectbox-generator/internal/generator/model"
)

Expand Down Expand Up @@ -74,18 +75,17 @@ type CodeGenerator interface {
}

// WriteFile writes data to targetFile, while using permissions either from the targetFile or permSource
func WriteFile(file string, data []byte, permSource string) error {
func WriteFile(targetFile string, data []byte, permSource string) error {
var perm os.FileMode
// copy permissions either from the existing file or from the source file
if info, _ := os.Stat(file); info != nil {
if info, _ := os.Stat(targetFile); info != nil {
perm = info.Mode()
} else if info, err := os.Stat(permSource); info != nil {
perm = info.Mode()
} else {
return err
}

return ioutil.WriteFile(file, data, perm)
return ioutil.WriteFile(targetFile, data, perm)
}

// Process is the main API method of the package
Expand Down Expand Up @@ -134,31 +134,57 @@ func Process(options Options) error {

var modelInfo *model.ModelInfo

modelInfo, err = model.LoadOrCreateModel(options.ModelInfoFile)
if err != nil {
return fmt.Errorf("can't init ModelInfo: %s", err)
}
var lockPath = options.ModelInfoFile + ".lock"
fileLock := flock.New(lockPath)

modelInfo.Rand = options.Rand
defer modelInfo.Close()
var retries = 5
var locked bool = false

if err = modelInfo.Validate(); err != nil {
return fmt.Errorf("invalid ModelInfo loaded: %s", err)
for !locked && retries > 0 {
locked, err = fileLock.TryLock()
if !locked || err != nil {
fmt.Println("LOCKED.. SLEEPING")
time.Sleep(1 * time.Second)
retries--
}
}

// if the model is valid, upgrade it to the latest version
modelInfo.MinimumParserVersion = model.ModelVersion
modelInfo.ModelVersion = model.ModelVersion
if locked {
defer func() {
if err := fileLock.Unlock(); err != nil {
panic(err)
}
}()

if err = createBinding(options, modelInfo); err != nil {
return err
}
modelInfo, err = model.LoadOrCreateModel(options.ModelInfoFile)
if err != nil {
return fmt.Errorf("can't init ModelInfo: %s", err)
}

modelInfo.Rand = options.Rand
defer modelInfo.Close()

if err = modelInfo.Validate(); err != nil {
return fmt.Errorf("invalid ModelInfo loaded: %s", err)
}

if err = createModel(options, modelInfo); err != nil {
// if the model is valid, upgrade it to the latest version
modelInfo.MinimumParserVersion = model.ModelVersion
modelInfo.ModelVersion = model.ModelVersion

if err = createBinding(options, modelInfo); err != nil {
return err
}

if err = createModel(options, modelInfo); err != nil {
return err
}

return nil

} else {
return err
}

return nil
}

func createBinding(options Options, storedModel *model.ModelInfo) error {
Expand Down
47 changes: 47 additions & 0 deletions internal/generator/model/fileio.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,62 @@ import (

// LoadOrCreateModel reads a model file or creates a new one if it doesn't exist
func LoadOrCreateModel(path string) (model *ModelInfo, err error) {

/*
var lockPath = path + ".lock"
fileLock := flock.New(lockPath)
lockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, 100*time.Millisecond)
if err != nil {
fmt.Println("LOCK FAILED")
return nil, err
}
if locked == false {
fmt.Println("LOCK FAILED")
}
*/

/*
var retries = 5
var locked bool = false
for !locked && retries > 0 {
locked, err = fileLock.TryLock()
if !locked || err != nil {
fmt.Println("LOCKED.. SLEEPING")
time.Sleep(1 * time.Second)
retries--
}
}
if locked {
defer func() {
if err := fileLock.Unlock(); err != nil {
panic(err)
}
}()
*/
if fileExists(path) {
return LoadModelFromJSONFile(path)
}
return createModelJSONFile(path)
/*
} else {
return nil, err
}a
*/
}

// Close and unlock model
func (model *ModelInfo) Close() error {
return model.file.Close()
/*
var err = model.file.Close()
model.file.Sync()
return err
*/
}

// Write current model data to file
Expand Down
4 changes: 2 additions & 2 deletions test/cmake/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (cmake *Cmake) BuildTarget() ([]byte, []byte, error) {
func (cmake *Cmake) BuildDefaults() ([]byte, []byte, error) {
return cmakeExec(cmake.ConfDir,
"--build", cmake.BuildDir,
// "--parallel "+strconv.FormatInt(int64(runtime.NumCPU()/2), 10),
"--parallel "+strconv.FormatInt(int64(runtime.NumCPU()/2), 10),
)
}

Expand All @@ -204,7 +204,7 @@ func (cmake *Cmake) BuildDefaultsWithConfig(config string) ([]byte, []byte, erro
return cmakeExec(cmake.ConfDir,
"--build", cmake.BuildDir,
"--config", config,
// "--parallel "+strconv.FormatInt(int64(runtime.NumCPU()/2), 10),
"--parallel "+strconv.FormatInt(int64(runtime.NumCPU()/2), 10),
)
}

Expand Down

0 comments on commit 6606475

Please sign in to comment.