sync: migrate ai-operator to Gitea (2026-08-10)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package ari
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type LockFile struct {
|
||||
path string
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func AcquireLock(path string) (*LockFile, error) {
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, fmt.Errorf("ARI events listener lock is already held: %s", path)
|
||||
}
|
||||
_, _ = file.WriteString(fmt.Sprintf("%d\n", os.Getpid()))
|
||||
return &LockFile{path: path, file: file}, nil
|
||||
}
|
||||
|
||||
func (l *LockFile) Release() error {
|
||||
if l == nil || l.file == nil {
|
||||
return nil
|
||||
}
|
||||
_ = syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
|
||||
return l.file.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user