github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/alert/alert.go (about)

     1  package alert
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"gopkg.in/mgo.v2"
     8  	"gopkg.in/mgo.v2/bson"
     9  )
    10  
    11  type QueueStatus string
    12  
    13  const (
    14  	Pending    QueueStatus = "pending"
    15  	InProgress             = "in-progress"
    16  	Delivered              = "delivered"
    17  	Failed                 = "failed"
    18  )
    19  
    20  // AlertRequest represents the raw database record of an alert that has been queued into the DB
    21  type AlertRequest struct {
    22  	Id          bson.ObjectId `bson:"_id"`
    23  	QueueStatus QueueStatus   `bson:"queue_status"`
    24  	Trigger     string        `bson:"trigger"`
    25  	TaskId      string        `bson:"task_id,omitempty"`
    26  	HostId      string        `bson:"host_id,omitempty"`
    27  	Execution   int           `bson:"execution,omitempty"`
    28  	BuildId     string        `bson:"build_id,omitempty"`
    29  	VersionId   string        `bson:"version_id,omitempty"`
    30  	ProjectId   string        `bson:"project_id,omitempty"`
    31  	PatchId     string        `bson:"patch_id,omitempty"`
    32  	Display     string        `bson:"display"`
    33  	CreatedAt   time.Time     `bson:"created_at"`
    34  	ProcessedAt time.Time     `bson:"processed_at"`
    35  }
    36  
    37  func DequeueAlertRequest() (*AlertRequest, error) {
    38  	out := AlertRequest{}
    39  	_, err := db.FindAndModify(Collection,
    40  		bson.M{QueueStatusKey: Pending},
    41  		[]string{CreatedAtKey},
    42  		mgo.Change{
    43  			Update:    bson.M{"$set": bson.M{QueueStatusKey: InProgress}},
    44  			Upsert:    false,
    45  			Remove:    false,
    46  			ReturnNew: true,
    47  		}, &out)
    48  
    49  	if err == mgo.ErrNotFound {
    50  		return nil, nil
    51  	}
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return &out, nil
    56  }
    57  
    58  func EnqueueAlertRequest(a *AlertRequest) error {
    59  	a.QueueStatus = Pending
    60  	return db.Insert(Collection, a)
    61  }