From: qydysky Date: Sun, 27 Jun 2021 07:22:50 +0000 (+0800) Subject: progLock X-Git-Tag: v0.5.37 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=8dd668bda4c31d7f6b7545a8b20d3d62864aedd3;p=part%2F.git progLock --- diff --git a/Lock.go b/Lock.go deleted file mode 100644 index 81d4345..0000000 --- a/Lock.go +++ /dev/null @@ -1,60 +0,0 @@ -package part - -import ( - "os" - "sync" - "time" - "errors" -) - -type lock struct { - sync.Mutex -} - -var ( - lock_file string - lock_timeout int64 = 10 -) - -func Lock() *lock { - return &lock{} -} - -func (l *lock) Start(filePath string,timeout int64) error { - l.Lock() - defer l.Unlock() - - if Checkfile().IsExist(filePath) { - if e,t := Checkfile().GetFileModTime(filePath); e != nil || time.Now().Unix() - t <= lock_timeout { - Logf().E(e.Error(),"or still alive") - return errors.New("still alive") - } - } - - - lock_file = filePath - lock_timeout = timeout - - go func(){ - for lock_file != "" { - File().FileWR(Filel{ - File:filePath, - Loc:0, - Context:[]interface{}{"still alive"}, - }) - Sys().Timeoutf(int(lock_timeout)) - } - }() - - return nil -} - -func (l *lock) Stop() { - l.Lock() - defer l.Unlock() - - os.RemoveAll(lock_file) - - lock_file = "" - -} \ No newline at end of file diff --git a/progLock/Lock.go b/progLock/Lock.go new file mode 100644 index 0000000..c2fa25f --- /dev/null +++ b/progLock/Lock.go @@ -0,0 +1,89 @@ +package part + +import ( + "os" + "sync" + "time" + "errors" + "fmt" + "encoding/json" + + part "github.com/qydysky/part" +) + +type lock struct { + Time int64 `json:"t"` + stopsign bool + b chan struct{} + sync.Mutex +} + +const ( + lock_file = ".lock" + lock_timeout = 10 +) + +func New() *lock { + return &lock{} +} + +func (l *lock) Start() (err error) { + l.Lock() + defer l.Unlock() + + if part.Checkfile().IsExist(lock_file) { + //read time from file + if s := part.File().FileWR(part.Filel{ + File:lock_file, + Loc:0, + });len(s) != 0 { + if e := json.Unmarshal([]byte(s), l);e != nil { + err = e + } + } else {err = errors.New("read error")} + //read time from modtime + if err != nil { + err,l.Time = part.Checkfile().GetFileModTime(lock_file) + } + + if err != nil {panic(err.Error())} + + if time.Now().Unix() - l.Time <= lock_timeout { + return errors.New("still alive") + } + } else { + l.b = make(chan struct{}) + go func(l *lock){ + for !l.stopsign { + l.Time = time.Now().Unix() + if b,e := json.Marshal(l);e != nil { + panic(e.Error()) + } else { + part.File().FileWR(part.Filel{ + File:lock_file, + Loc:0, + Context:[]interface{}{b}, + }) + } + + select{ + case l.b<-struct{}{}:; + default:; + } + + time.Sleep(time.Duration(lock_timeout)*time.Second) + } + }(l) + <- l.b + } + return nil +} + +func (l *lock) Stop() error { + l.Lock() + defer l.Unlock() + + l.stopsign = true + close(l.b) + return os.RemoveAll(lock_file) +} \ No newline at end of file diff --git a/progLock/Lock_test.go b/progLock/Lock_test.go new file mode 100644 index 0000000..e0f6353 --- /dev/null +++ b/progLock/Lock_test.go @@ -0,0 +1,14 @@ +package part + +import ( + "testing" + "time" +) + +func Test(t *testing.T) { + l := New() + t.Log(l.Start()) + t.Log(New().Start()) + time.Sleep(time.Second) + t.Log(l.Stop()) +} \ No newline at end of file