mirror of
https://github.com/pyihe/go-pkg.git
synced 2025-10-08 09:20:12 +08:00
style(go-pkg): rename pkg
This commit is contained in:
64
monitorpkg/monitor.go
Normal file
64
monitorpkg/monitor.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package monitorpkg
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Monitor interface {
|
||||
AddFile(path string, spec string, handler func()) error
|
||||
DelFile(path string) error
|
||||
}
|
||||
|
||||
myMonitor struct {
|
||||
sync.RWMutex
|
||||
fs Files
|
||||
}
|
||||
|
||||
Files map[string]*myFile
|
||||
)
|
||||
|
||||
func (m *myMonitor) init() {
|
||||
m.fs = make(map[string]*myFile)
|
||||
}
|
||||
|
||||
func NewMonitor() Monitor {
|
||||
mon := new(myMonitor)
|
||||
mon.init()
|
||||
return mon
|
||||
}
|
||||
|
||||
func (m *myMonitor) AddFile(path string, spec string, handler func()) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
if handler == nil {
|
||||
return errors.New("handler cannot be nil")
|
||||
}
|
||||
if _, ok := m.fs[path]; ok {
|
||||
return errors.New("already exist")
|
||||
}
|
||||
f := &myFile{
|
||||
spec: spec,
|
||||
filePath: path,
|
||||
updateOn: time.Now().Unix(),
|
||||
handle: handler,
|
||||
}
|
||||
m.fs[path] = f
|
||||
f.init()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *myMonitor) DelFile(path string) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
f, ok := m.fs[path]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
f.c.Stop()
|
||||
f.c.Remove(f.id)
|
||||
delete(m.fs, path)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user