github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/push.go (about) 1 package model 2 3 import ( 4 "time" 5 6 "github.com/evergreen-ci/evergreen" 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/db/bsonutil" 9 "github.com/evergreen-ci/evergreen/model/task" 10 "github.com/evergreen-ci/evergreen/model/version" 11 "gopkg.in/mgo.v2" 12 "gopkg.in/mgo.v2/bson" 13 ) 14 15 const ( 16 PushlogCollection = "pushes" 17 PushLogSuccess = "success" 18 PushLogFailed = "failed" 19 ) 20 21 type PushLog struct { 22 Id bson.ObjectId `bson:"_id,omitempty"` 23 24 //the permanent location of the pushed file. 25 Location string `bson:"location"` 26 27 //the task id of the push stage 28 TaskId string `bson:"task_id"` 29 30 CreateTime time.Time `bson:"create_time"` 31 Revision string `bson:"githash"` 32 Status string `bson:"status"` 33 34 //copied from version for the task 35 RevisionOrderNumber int `bson:"order"` 36 } 37 38 var ( 39 // bson fields for the push log struct 40 PushLogIdKey = bsonutil.MustHaveTag(PushLog{}, "Id") 41 PushLogLocationKey = bsonutil.MustHaveTag(PushLog{}, "Location") 42 PushLogTaskIdKey = bsonutil.MustHaveTag(PushLog{}, "TaskId") 43 PushLogCreateTimeKey = bsonutil.MustHaveTag(PushLog{}, "CreateTime") 44 PushLogRevisionKey = bsonutil.MustHaveTag(PushLog{}, "Revision") 45 PushLogStatusKey = bsonutil.MustHaveTag(PushLog{}, "Status") 46 PushLogRonKey = bsonutil.MustHaveTag(PushLog{}, "RevisionOrderNumber") 47 ) 48 49 func NewPushLog(v *version.Version, task *task.Task, location string) *PushLog { 50 return &PushLog{ 51 Id: bson.NewObjectId(), 52 Location: location, 53 TaskId: task.Id, 54 CreateTime: time.Now(), 55 Revision: v.Revision, 56 RevisionOrderNumber: v.RevisionOrderNumber, 57 Status: evergreen.PushLogPushing, 58 } 59 } 60 61 func (self *PushLog) Insert() error { 62 return db.Insert(PushlogCollection, self) 63 } 64 65 func (self *PushLog) UpdateStatus(newStatus string) error { 66 return db.Update( 67 PushlogCollection, 68 bson.M{ 69 PushLogIdKey: self.Id, 70 }, 71 bson.M{ 72 "$set": bson.M{ 73 PushLogStatusKey: newStatus, 74 }, 75 }, 76 ) 77 } 78 79 func FindOnePushLog(query interface{}, projection interface{}, 80 sort []string) (*PushLog, error) { 81 pushLog := &PushLog{} 82 err := db.FindOne( 83 PushlogCollection, 84 query, 85 projection, 86 sort, 87 pushLog, 88 ) 89 if err == mgo.ErrNotFound { 90 return nil, nil 91 } 92 return pushLog, err 93 } 94 95 // FindNewerPushLog returns a PushLog item if there is a file pushed from 96 // this version or a newer one, or one already in progress. 97 func FindPushLogAfter(fileLoc string, revisionOrderNumber int) (*PushLog, error) { 98 query := bson.M{ 99 PushLogStatusKey: bson.M{ 100 "$in": []string{ 101 evergreen.PushLogPushing, evergreen.PushLogSuccess, 102 }, 103 }, 104 PushLogLocationKey: fileLoc, 105 PushLogRonKey: bson.M{ 106 "$gte": revisionOrderNumber, 107 }, 108 } 109 existingPushLog, err := FindOnePushLog( 110 query, 111 db.NoProjection, 112 []string{"-" + PushLogRonKey}, 113 ) 114 if err != nil { 115 return nil, err 116 } 117 return existingPushLog, nil 118 }