This repository was archived by the owner on Sep 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathremove.go
More file actions
118 lines (111 loc) · 2.52 KB
/
Copy pathremove.go
File metadata and controls
118 lines (111 loc) · 2.52 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package process
import (
"io/ioutil"
"os"
"path"
"time"
"github.com/ModuleAB/ModuleAB/agent/client"
"github.com/ModuleAB/ModuleAB/agent/logger"
"github.com/ModuleAB/ModuleAB/server/models"
)
// RemoveManager module
type RemoveManager struct {
JobList []string
JobChannel map[string]chan int
}
// NewRemoveManager is to create new `RemoveManager` instance
func NewRemoveManager() *RemoveManager {
r := new(RemoveManager)
r.JobList = make([]string, 0)
return r
}
// Update is to configure `RemoveManager` settings.
func (r *RemoveManager) Update(h *models.Hosts) error {
ps := h.Paths
for _, v := range ps {
found := false
for _, v0 := range r.JobList {
if v.Path == v0 {
found = true
break
}
}
if found {
continue
}
for _, v0 := range h.ClientJobs {
for _, v1 := range v0.Paths {
if v1.Path == v.Path && v0.Type == models.ClientJobsTypeDelete {
r.JobList = append(r.JobList, v.Path)
r.JobChannel[v.Path] = make(chan int)
go r.Run(v0.Period, v0.ReservedTime, v, h)
}
}
}
}
for k, v := range r.JobList {
found := false
for _, v0 := range ps {
if v0.Path == v {
found = true
break
}
}
if !found {
r.JobList = append(r.JobList[:k], r.JobList[k+1:]...)
r.JobChannel[v] <- 1 //Go exit
}
}
return nil
}
// Run is to start `RemoveManager`
func (r *RemoveManager) Run(
period, reserve int,
mpath *models.Paths,
host *models.Hosts,
) {
logger.AppLog.Info("Remove monitor of", mpath.Path, "started.")
defer logger.AppLog.Info("Remove monitor of", mpath.Path, "stopped.")
for {
select {
case <-time.Tick(time.Duration(period) * time.Second):
files, err := ioutil.ReadDir(mpath.Path)
if err != nil {
logger.AppLog.Warn("Got error:", err)
continue
}
for _, file := range files {
if time.Now().Sub(file.ModTime()) >
time.Duration(reserve)*time.Second &&
!file.IsDir() {
record := &models.Records{
Path: mpath,
AppSet: host.AppSet,
Host: host,
Filename: file.Name(),
BackupSet: mpath.BackupSet,
}
found, err := client.CheckRecords(record)
if err != nil {
logger.AppLog.Warn("Got error:", err)
continue
}
if found {
err = os.Remove(path.Join(mpath.Path, file.Name()))
if err != nil {
logger.AppLog.Warn("Got error:", err)
continue
}
logger.AppLog.Info(
"File:",
path.Join(mpath.Path, file.Name()),
"removed.",
)
}
}
}
case <-r.JobChannel[mpath.Path]:
return
}
}
}