-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.go
More file actions
55 lines (47 loc) · 1.18 KB
/
lock.go
File metadata and controls
55 lines (47 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package env
import (
"fmt"
"os"
"path/filepath"
"time"
)
// Lock {file}.lock detection
type Lock struct {
Path string // lock directory
TTL time.Duration // default 1hr
}
// Unlock removes the current {file}.lock
func (lk *Lock) Unlock() bool {
return os.Remove(filepath.Join(lk.Path, filepath.Base(os.Args[0])+".lock")) == nil
}
// Lock tests for the presence of a current {file}.Lock and returns true when
// a new {file}.Lock was established; false when an existing one is present
//
// var lock = env.Lock{Path: "/tmp", TTL: time.Hour}
// if !lock.Lock() {
// return // existing lock
// }
// defer lk.Unlock()
func (lk *Lock) Lock() bool {
// default assurances
if lk.TTL == 0 {
lk.TTL = time.Hour
}
if len(lk.Path) == 0 {
lk.Path = "/tmp"
}
os.MkdirAll(filepath.Dir(lk.Path), 0755)
// check existence and/or expired {file}.lock
var target = filepath.Join(lk.Path, filepath.Base(os.Args[0])+".lock")
info, _ := os.Stat(target) // verify exists
if info != nil && info.ModTime().After(time.Now().Add(-lk.TTL)) {
return false
}
// create {file}.lock
f, err := os.Create(target)
if err == nil {
fmt.Fprint(f, os.Getpid())
f.Close()
}
return err == nil
}