github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/alerts/triggers.go (about) 1 package alerts 2 3 import ( 4 //"github.com/evergreen-ci/evergreen" 5 "github.com/evergreen-ci/evergreen/model" 6 "github.com/evergreen-ci/evergreen/model/alertrecord" 7 "github.com/evergreen-ci/evergreen/model/host" 8 "github.com/evergreen-ci/evergreen/model/task" 9 "github.com/evergreen-ci/evergreen/model/version" 10 "gopkg.in/mgo.v2/bson" 11 ) 12 13 // Trigger is a rule that determines if an alert should be queued for some event. 14 type Trigger interface { 15 // Id is a string that identifies this trigger logic. It's used to store in the database for 16 // alerts that are queued to identify the reason the alert was generated. 17 Id() string 18 19 // Display is a human-readable description of the trigger logic. It may be used in log messages 20 // for debugging or for presentation in a UI control panel. 21 Display() string 22 23 // ShouldExecute returns a boolean indicating if this trigger's logic has matched and should 24 // result in an alert being queued. This logic may involve querying for bookkeeping 25 // generated by CreateBookkeepingRecord in previous executions. 26 ShouldExecute(ctx triggerContext) (bool, error) 27 28 // CreateAlertRecord returns an instance of AlertRecord which, after insertion, should allow 29 // this trigger to detect previous executions of itself in its ShouldExecute function. 30 CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord 31 } 32 33 // triggerContext is a set of data about the situation in which a trigger is tested. The trigger 34 // can use this logic to 1) determine if it should execute, and 2) store a bookkeeping record 35 // to prevent it from double-execution. Some fields may be ignored or irrelevant depending on the 36 // trigger in use. 37 type triggerContext struct { 38 projectRef *model.ProjectRef 39 version *version.Version 40 task *task.Task 41 previousCompleted *task.Task 42 host *host.Host 43 } 44 45 var ( 46 // AvailableTaskTriggers is a list of all the supported task triggers, which is used by the UI 47 // package to generate a control panel for configuring how to react to these triggers. 48 AvailableTaskFailTriggers = []Trigger{TaskFailed{}, 49 FirstFailureInVersion{}, 50 FirstFailureInVariant{}, 51 FirstFailureInTaskType{}, 52 TaskFailTransition{}, 53 } 54 55 AvailableProjectTriggers = []Trigger{ 56 LastRevisionNotFound{}, 57 } 58 59 SpawnWarningTriggers = []Trigger{SpawnTwoHourWarning{}, SpawnTwelveHourWarning{}} 60 ) 61 62 // newAlertRecord creates an instance of an alert record for the given alert type, populating it 63 // with as much data from the triggerContext as possible 64 func newAlertRecord(ctx triggerContext, alertType string) *alertrecord.AlertRecord { 65 record := &alertrecord.AlertRecord{ 66 Id: bson.NewObjectId(), 67 Type: alertType, 68 } 69 if ctx.host != nil { 70 record.HostId = ctx.host.Id 71 } 72 if ctx.task != nil { 73 record.ProjectId = ctx.task.Project 74 record.VersionId = ctx.task.Version 75 record.RevisionOrderNumber = ctx.task.RevisionOrderNumber 76 record.TaskName = ctx.task.DisplayName 77 record.Variant = ctx.task.BuildVariant 78 } 79 return record 80 }