forked from evergreen-ci/evergreen
/
push.go
116 lines (103 loc) · 2.81 KB
/
push.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
package model
import (
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/db/bsonutil"
"github.com/evergreen-ci/evergreen/model/version"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"time"
)
const (
PushlogCollection = "pushes"
PushLogSuccess = "success"
PushLogFailed = "failed"
)
type PushLog struct {
Id bson.ObjectId `bson:"_id,omitempty"`
//the permanent location of the pushed file.
Location string `bson:"location"`
//the task id of the push stage
TaskId string `bson:"task_id"`
CreateTime time.Time `bson:"create_time"`
Revision string `bson:"githash"`
Status string `bson:"status"`
//copied from version for the task
RevisionOrderNumber int `bson:"order"`
}
var (
// bson fields for the push log struct
PushLogIdKey = bsonutil.MustHaveTag(PushLog{}, "Id")
PushLogLocationKey = bsonutil.MustHaveTag(PushLog{}, "Location")
PushLogTaskIdKey = bsonutil.MustHaveTag(PushLog{}, "TaskId")
PushLogCreateTimeKey = bsonutil.MustHaveTag(PushLog{}, "CreateTime")
PushLogRevisionKey = bsonutil.MustHaveTag(PushLog{}, "Revision")
PushLogStatusKey = bsonutil.MustHaveTag(PushLog{}, "Status")
PushLogRonKey = bsonutil.MustHaveTag(PushLog{}, "RevisionOrderNumber")
)
func NewPushLog(v *version.Version, task *Task, location string) *PushLog {
return &PushLog{
Id: bson.NewObjectId(),
Location: location,
TaskId: task.Id,
CreateTime: time.Now(),
Revision: v.Revision,
RevisionOrderNumber: v.RevisionOrderNumber,
Status: evergreen.PushLogPushing,
}
}
func (self *PushLog) Insert() error {
return db.Insert(PushlogCollection, self)
}
func (self *PushLog) UpdateStatus(newStatus string) error {
return db.Update(
PushlogCollection,
bson.M{
PushLogIdKey: self.Id,
},
bson.M{
"$set": bson.M{
PushLogStatusKey: newStatus,
},
},
)
}
func FindOnePushLog(query interface{}, projection interface{},
sort []string) (*PushLog, error) {
pushLog := &PushLog{}
err := db.FindOne(
PushlogCollection,
query,
projection,
sort,
pushLog,
)
if err == mgo.ErrNotFound {
return nil, nil
}
return pushLog, err
}
// FindNewerPushLog returns a PushLog item if there is a file pushed from
// this version or a newer one, or one already in progress.
func FindPushLogAfter(fileLoc string, revisionOrderNumber int) (*PushLog, error) {
query := bson.M{
PushLogStatusKey: bson.M{
"$in": []string{
evergreen.PushLogPushing, evergreen.PushLogSuccess,
},
},
PushLogLocationKey: fileLoc,
PushLogRonKey: bson.M{
"$gte": revisionOrderNumber,
},
}
existingPushLog, err := FindOnePushLog(
query,
db.NoProjection,
[]string{"-" + PushLogRonKey},
)
if err != nil {
return nil, err
}
return existingPushLog, nil
}