initial
This commit is contained in:
91
monitor/drive.go
Executable file
91
monitor/drive.go
Executable file
@@ -0,0 +1,91 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"local/logb"
|
||||
"local/sandbox/arlo-cleaner/arlo"
|
||||
"local/sandbox/arlo-cleaner/config"
|
||||
"local/sandbox/arlo-cleaner/rclone"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Drive struct {
|
||||
rclone *rclone.RClone
|
||||
}
|
||||
|
||||
func NewDrive() (*Drive, error) {
|
||||
rclone, err := rclone.New()
|
||||
return &Drive{
|
||||
rclone: rclone,
|
||||
}, err
|
||||
}
|
||||
|
||||
func (d *Drive) Start() <-chan error {
|
||||
ch := make(chan error)
|
||||
go d.watch(ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
func (d *Drive) watch(ch chan<- error) {
|
||||
if err := d.Clean(); err != nil {
|
||||
ch <- err
|
||||
}
|
||||
ticker := time.NewTicker(config.MonitorInterval)
|
||||
for _ = range ticker.C {
|
||||
if err := d.Clean(); err != nil {
|
||||
ch <- err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Drive) Clean() error {
|
||||
videos, err := d.rclone.List(config.RCloneName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
videos = d.filters(videos)
|
||||
if len(videos) == 0 {
|
||||
logb.Infof("Drive nothing to clean")
|
||||
return nil
|
||||
}
|
||||
logb.Infof("Drive clean commencing...")
|
||||
defer logb.Infof("Drive clean done")
|
||||
return d.deletes(videos)
|
||||
}
|
||||
|
||||
func (d *Drive) filters(videos []string) []string {
|
||||
filtered := []string{}
|
||||
for _, video := range videos {
|
||||
if d.expired(video) {
|
||||
filtered = append(filtered, video)
|
||||
}
|
||||
}
|
||||
logb.Debugf("drive found %v expired videos from %v", len(filtered), len(videos))
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (d *Drive) expired(video string) bool {
|
||||
base := path.Base(video)
|
||||
timestamp := strings.Split(base, ".")[0]
|
||||
created, err := time.Parse(arlo.TSFormat, timestamp)
|
||||
if err != nil {
|
||||
logb.Errorf("not deleting from archive: cannot parse", timestamp, ":", err)
|
||||
return false
|
||||
}
|
||||
return time.Since(created) > config.ArchiveLength
|
||||
}
|
||||
|
||||
func (d *Drive) deletes(videos []string) error {
|
||||
for _, video := range videos {
|
||||
if err := d.delete(video); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Drive) delete(video string) error {
|
||||
logb.Infof("deleting drive video %v", video)
|
||||
return d.rclone.Del(path.Join(config.RCloneName, video))
|
||||
}
|
||||
Reference in New Issue
Block a user