-
Notifications
You must be signed in to change notification settings - Fork 21
/
expire.go
53 lines (46 loc) · 1.5 KB
/
expire.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
package core
import (
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"gopkg.in/mgo.v2/bson"
"regexp"
"time"
)
var (
Ttl *JobReaper
ExpireRegex = regexp.MustCompile(`^(\d+)(M|H|D)$`)
)
func InitReaper() {
Ttl = NewJobReaper()
}
type JobReaper struct{}
func NewJobReaper() *JobReaper {
return &JobReaper{}
}
func (jr *JobReaper) Handle() {
waitDuration := time.Duration(conf.EXPIRE_WAIT) * time.Minute
for {
// sleep
time.Sleep(waitDuration)
// query to get expired jobs
jobs := Jobs{}
query := jr.getQuery()
jobs.GetAllUnsorted(query)
// delete expired jobs
for _, j := range jobs {
logger.Event(event.JOB_EXPIRED, "jobid="+j.Id)
if err := j.Delete(); err != nil {
logger.Error("Err@job_delete: " + err.Error())
}
}
}
}
func (jr *JobReaper) getQuery() (query bson.M) {
isCompOrDel := bson.M{"$or": []bson.M{bson.M{"state": JOB_STAT_COMPLETED}, bson.M{"state": JOB_STAT_DELETED}}} // job in completed or deleted state
hasExpire := bson.M{"expiration": bson.M{"$exists": true}} // has the field
toExpire := bson.M{"expiration": bson.M{"$ne": time.Time{}}} // value has been set, not default
isExpired := bson.M{"expiration": bson.M{"$lt": time.Now()}} // value is too old
query = bson.M{"$and": []bson.M{isCompOrDel, hasExpire, toExpire, isExpired}}
return
}