-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
221 lines (186 loc) · 4.22 KB
/
task.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import "log"
import (
"time"
"bytes"
"strconv"
"encoding/json"
"github.com/boltdb/bolt"
"github.com/fatih/structs"
)
type Taskinfo struct {
ID uint64
Name string
Criteria string
Enabled bool
}
func (ti Taskinfo) json() []byte {
b, err := json.Marshal(ti)
if err == nil {
return b
} else {
return []byte("")
}
}
func (ti Taskinfo) byteid() []byte {
return []byte(strconv.FormatUint(ti.ID,10))
}
type Task struct {
Taskinfo
db *bolt.DB
State int
queue chan PackageMsg
parsedExp *ParsedExpression
quit chan bool
indx *Indexer
dlm *DownloadManager
Packages []Package
}
type TaskQueue struct {
ID string
TaskinfoID uint
PackageID uint
Package Package `gorm:"foreignkey:PackageID"`
}
func (t* Task) Init(indx *Indexer, dlm *DownloadManager, db *bolt.DB) {
t.db = db
t.State = 1
t.indx = indx
t.dlm = dlm
err,pe := ParseToPE(t.Criteria)
if err != nil || t.Criteria == "" {
} else {
t.parsedExp = pe
}
// t.queue = make(chan Package, 10)
t.quit = make(chan bool)
}
func (t *Task) MatchesCriterias(p Package) bool {
m := structs.Map(p)
m["SizeMbytes"] = p.SizeMbytes()
for k,v := range structs.Map(t.indx.getReleaseForPackage(p).Release) {
m["r" + k] = v
}
for k,v := range structs.Map(t.indx.getReleaseForPackage(p)) {
m["r" + k] = v
}
if t.parsedExp == nil {
return false
}
err, match := t.parsedExp.Eval(m)
if err != nil{
log.Print("Error evaluating " + t.Criteria)
return false
}
//log.Print(p)
return match
}
func (t *Task) enqueue(p Package, block bool) bool {
if len(t.GetAllQueued()) > 10 {
log.Print("queue full, ignore")
return false
}
t.db.Update(func (tx *bolt.Tx) error {
tBucket := tx.Bucket([]byte("tasks"))
c := tBucket.Cursor()
prefix := append(t.Taskinfo.byteid(), []byte(":")...)
for k,v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
if PackageFromJSON(v).key() == p.key() {
log.Print("didnt add bc already in there")
return nil
}
}
nid, _ := tBucket.NextSequence()
tBucket.Put([]byte(strconv.FormatUint(t.ID, 10) + ":" + strconv.FormatUint(nid, 10)), []byte(p.json()))
return nil
})
return true
}
func (t *Task) CheckQuit() bool {
select {
case <-t.quit:
return true
default:
return false
}
}
func (t *Task) GetAllQueued() []Package {
var res []Package
t.db.Update(func (tx *bolt.Tx) error {
tBucket := tx.Bucket([]byte("tasks"))
c := tBucket.Cursor()
prefix := append(t.Taskinfo.byteid(), []byte(":")...)
for k,v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
res = append(res, PackageFromJSON(v))
}
return nil
})
return res
}
func (t *Task) EmptyQueue() {
t.db.Update(func (tx *bolt.Tx) error {
tBucket := tx.Bucket([]byte("tasks"))
c := tBucket.Cursor()
prefix := append(t.Taskinfo.byteid(), []byte(":")...)
for k,_ := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, _ = c.Next() {
tBucket.Delete(k)
}
return nil
})
}
func (t *Task) PullFromQueue() (found bool, p Package) {
t.db.Update(func (tx *bolt.Tx) error {
tBucket := tx.Bucket([]byte("tasks"))
c := tBucket.Cursor()
prefix := append(t.Taskinfo.byteid(), []byte(":")...)
k,v := c.Seek(prefix)
if k != nil && bytes.HasPrefix(k, prefix) {
found = true
p = PackageFromJSON(v)
tBucket.Delete(k)
} else {
found = false
}
return nil
})
return found, p
}
func (t *Task) Worker() {
t.State = 2
for {
if t.CheckQuit() {
t.State = 0
log.Print("Quitting task")
return
}
avail, p := t.PullFromQueue()
if !avail {
time.Sleep(1*time.Second)
continue
}
if t.indx.CheckDownloaded(p) {
continue
}
dlId := t.dlm.CreateDownload(Download{Pack: p, Targetfolder:p.TargetFolder()})
i, dl := t.dlm.GetDownload(dlId)
log.Print("Issued DL ")
for i != -1 && dl.State == 0 { // wait while DL in progress
time.Sleep(1*time.Second)
i, dl = t.dlm.GetDownload(dlId)
if t.CheckQuit() {
t.State = 0
log.Print("Quitting task")
return
}
}
if dl.State == -1 {
t.indx.RemovePackage(&p)
log.Print("Deleted package bc of failure to dl")
} else {
log.Print("add to downloaded")
log.Print(p.Release)
t.indx.AddDownloaded(p.Release)
}
log.Print("Done")
}
}