Skip to content

Commit

Permalink
refactor: kcl lib install add add file locks (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy authored Jun 2, 2023
1 parent 173fe8a commit 58e7e39
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main_darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
with:
go-version: 1.17

- run: go run ./cmds/kcl-go run hello.k
- run: go test -p 1 ./...
# Parallel tests
- run: go test ./...
4 changes: 2 additions & 2 deletions .github/workflows/main_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
with:
go-version: 1.17

- run: go run ./cmds/kcl-go run hello.k
- run: go test -p 1 -v -coverprofile=profile.cov ./...
# Parallel tests
- run: go test -v -coverprofile=profile.cov ./...
- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
5 changes: 2 additions & 3 deletions .github/workflows/main_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ jobs:
with:
python-version: '3.10'

- run: go install ./... && C:\Users\runneradmin\go\bin\kcl-go.exe run hello.k
- run: go run ./cmds/kcl-go run hello.k
- run: go test -p 1 ./...
# Parallel tests
- run: go test ./...
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/chai2010/jsonv v1.1.3
github.com/chai2010/protorpc v1.1.4
github.com/gofrs/flock v0.8.1
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
github.com/julienschmidt/httprouter v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
21 changes: 19 additions & 2 deletions pkg/kclvm_runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
package kclvm_runtime

import (
"context"
_ "embed"
"errors"
"os/exec"
"path/filepath"
"runtime"
"time"

"github.com/gofrs/flock"
kclvmArtifact "kusionstack.io/kclvm-artifact-go"
"kusionstack.io/kclvm-go/pkg/logger"
"kusionstack.io/kclvm-go/pkg/path"
)

func init() {

err := kclvmArtifact.InstallKclvm(path.LibPath())
// Get the install lib path.
path := path.LibPath()
// Acquire a file lock for process synchronization
lockPath := filepath.Join(path, "init.lock")
fileLock := flock.New(lockPath)
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, time.Second)
if err == nil && locked {
defer fileLock.Unlock()
}
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
// Install lib
err = kclvmArtifact.InstallKclvm(path)
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/path/lazypath_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
package path

import (
"path/filepath"
"go/build"
"os"
)

func libHome() string {
return filepath.Join(HomeDir(), ".config")
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return gopath
}
2 changes: 1 addition & 1 deletion pkg/path/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.
package path

const lp = lazypath("kcl")
const lp = lazypath("")

// LibPath returns the path where the kcl lib installed location.
func LibPath(elem ...string) string { return lp.libPath(elem...) }

0 comments on commit 58e7e39

Please sign in to comment.