]> 127.0.0.1 Git - part/.git/commitdiff
19
authorqydysky <qydysky@foxmail.com>
Sat, 1 Aug 2020 00:16:50 +0000 (08:16 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 1 Aug 2020 00:16:50 +0000 (08:16 +0800)
Lock.go

diff --git a/Lock.go b/Lock.go
index 007df315e50ff59442c8ecea621211df793a5106..eaf8a2f4d0502a7ddd7a271ce6242787debcdfe8 100644 (file)
--- a/Lock.go
+++ b/Lock.go
@@ -1,52 +1,58 @@
 package part
 
-// import (
-//     "os"
-//     "sync"
-// )
-
-// type lock struct {
-//     sync.Mutex
-// }
-
-// var (
-//     lock_file *os.File
-//     lock_md5Key string
-// )
-
-// func Lock() *lock {
-//     return &lock{}
-// }
-
-// func (l *lock) Start(filePath string) int {
-//     l.Lock()
-//     defer l.Unlock()
-
-//     if l.State() {return 1}
-//     if !Checkfile().IsExist(filePath) {return 3}
-
-//     fi, err := os.Stat(filePath)
-//     if err != nil {return 4}
-//     if Sys().Check(fi.Name())[0] != 0 {return 2}
-
-//     // lock_md5Key = ".lock." + fi.Name()
-//     // lock_file, _ = os.Create(lock_md5Key)
-//     return 0
-// }
-
-// func (l *lock) Stop() int {
-//     l.Lock()
-//     defer l.Unlock()
-
-//     if !l.State() {return 1}
-//     if !Checkfile().IsExist(lock_md5Key) {return 2}
-
-//     lock_file.Close()
-//     os.Remove(lock_md5Key)
-//     lock_md5Key = ""
-//     return 0
-// }
-
-// func (*lock) State() bool {
-//     return lock_md5Key != ""
-// }
\ No newline at end of file
+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 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,
+                               Write:true,
+                               Loc:0,
+                               Context:"still alive",
+                       })
+                       Sys().Timeoutf(int(lock_timeout))
+               }
+       }()
+
+       return nil
+}
+
+func (l *lock) Stop() {
+       l.Lock()
+       defer l.Unlock()
+
+       lock_file = ""
+
+       os.RemoveAll(lock_file)
+       
+}
\ No newline at end of file