Skip to content

Commit

Permalink
🔥 rm useless code and format
Browse files Browse the repository at this point in the history
  • Loading branch information
daijinru committed Jul 16, 2024
1 parent 0cb1512 commit 0330e95
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions runner/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@ package runner

import (
"fmt"
"github.com/gofrs/flock"
"os"
"path/filepath"
"github.com/gofrs/flock"
)

var (
PID_FILE_NAME = ".pid.lock"
)

type Lock struct {
FilePath string
FileLock *flock.Flock
FilePath string
FileLock *flock.Flock
}

// NewLock
func NewLock(path string) *Lock {
return &Lock{
FilePath: filepath.Join(path, "running.lock"),
}
return &Lock{
FilePath: filepath.Join(path, "running.lock"),
}
}

func (lock *Lock) Lock() error {
_, err := os.OpenFile(lock.FilePath, os.O_CREATE, 0666)
if err != nil {
return err
}
fileLock := flock.New(lock.FilePath)
locked, err := fileLock.TryLock()
if err != nil {
return fmt.Errorf("failed to lock file: %v", lock.FilePath)
}
if locked {
lock.FileLock = fileLock
} else {
return fmt.Errorf("file is already locked by another process")
}
return nil
_, err := os.OpenFile(lock.FilePath, os.O_CREATE, 0666)
if err != nil {
return err
}
fileLock := flock.New(lock.FilePath)
locked, err := fileLock.TryLock()
if err != nil {
return fmt.Errorf("failed to lock file: %v", lock.FilePath)
}
if locked {
lock.FileLock = fileLock
} else {
return fmt.Errorf("file is already locked by another process")
}
return nil
}

func (lock *Lock) Unlock() error {
defer lock.FileLock.Unlock()
return nil
defer lock.FileLock.Unlock()
return nil
}

0 comments on commit 0330e95

Please sign in to comment.