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

     1  package model
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"github.com/evergreen-ci/evergreen/db/bsonutil"
     8  	"github.com/mongodb/grip"
     9  	"gopkg.in/mgo.v2"
    10  	"gopkg.in/mgo.v2/bson"
    11  )
    12  
    13  const (
    14  	RuntimesCollection = "process_runtimes"
    15  )
    16  
    17  // ProcessRuntime tracks the most recent success ping by
    18  // a given MCI process by storing it in mongodb.
    19  // Id is a package name (see globals.go), FinishedAt is a time
    20  // representing the most recent completion of that process,
    21  // and Runtime is the duration of time the process took to run
    22  type ProcessRuntime struct {
    23  	Id         string        `bson:"_id"         json:"id"`
    24  	FinishedAt time.Time     `bson:"finished_at" json:"finished_at"`
    25  	Runtime    time.Duration `bson:"runtime"     json:"runtime"`
    26  }
    27  
    28  var (
    29  	ProcRuntimeIdKey         = bsonutil.MustHaveTag(ProcessRuntime{}, "Id")
    30  	ProcRuntimeFinishedAtKey = bsonutil.MustHaveTag(ProcessRuntime{},
    31  		"FinishedAt")
    32  	ProcRuntimeRuntimeKey = bsonutil.MustHaveTag(ProcessRuntime{}, "Runtime")
    33  )
    34  
    35  /******************************************************
    36  Find
    37  ******************************************************/
    38  
    39  func FindAllProcessRuntimes(query interface{},
    40  	projection interface{}) ([]ProcessRuntime, error) {
    41  	runtimes := []ProcessRuntime{}
    42  	err := db.FindAll(
    43  		RuntimesCollection,
    44  		query,
    45  		projection,
    46  		db.NoSort,
    47  		db.NoSkip,
    48  		db.NoLimit,
    49  		&runtimes,
    50  	)
    51  	return runtimes, err
    52  }
    53  
    54  func FindOneProcessRuntime(query interface{},
    55  	projection interface{}) (*ProcessRuntime, error) {
    56  	runtime := &ProcessRuntime{}
    57  	err := db.FindOne(
    58  		RuntimesCollection,
    59  		query,
    60  		projection,
    61  		db.NoSort,
    62  		runtime,
    63  	)
    64  	if err == mgo.ErrNotFound {
    65  		return nil, nil
    66  	}
    67  	return runtime, err
    68  }
    69  
    70  // Finds all runtimes that were updated before (less than) given time.
    71  func FindAllLateProcessRuntimes(cutoff time.Time) ([]ProcessRuntime, error) {
    72  	return FindAllProcessRuntimes(
    73  		bson.M{
    74  			ProcRuntimeFinishedAtKey: bson.M{
    75  				"$lt": cutoff,
    76  			},
    77  		},
    78  		db.NoProjection,
    79  	)
    80  }
    81  
    82  // Finds a process runtime by Id
    83  func FindProcessRuntime(id string) (*ProcessRuntime, error) {
    84  	return FindOneProcessRuntime(
    85  		bson.M{
    86  			ProcRuntimeIdKey: id,
    87  		},
    88  		db.NoProjection,
    89  	)
    90  }
    91  
    92  // Returns list of all process runtime entries
    93  func FindEveryProcessRuntime() ([]ProcessRuntime, error) {
    94  	return FindAllProcessRuntimes(
    95  		bson.M{},
    96  		db.NoProjection,
    97  	)
    98  }
    99  
   100  /******************************************************
   101  Update
   102  ******************************************************/
   103  
   104  func UpsertOneProcessRuntime(query interface{}, update interface{}) error {
   105  	info, err := db.Upsert(
   106  		RuntimesCollection,
   107  		query,
   108  		update,
   109  	)
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	grip.InfoWhenf(info.UpsertedId != nil, "Added '%s' process to ProcessRuntime  db",
   115  		info.UpsertedId)
   116  
   117  	return nil
   118  }
   119  
   120  // Updates a process runtime to set recent_success to the current time.
   121  // If no process with the given name exists, create it.
   122  // Parameter "processName" should be a constant "mci package" name from globals.go
   123  func SetProcessRuntimeCompleted(processName string,
   124  	runtime time.Duration) error {
   125  	return UpsertOneProcessRuntime(
   126  		bson.M{
   127  			ProcRuntimeIdKey: processName,
   128  		},
   129  		bson.M{
   130  			"$set": bson.M{
   131  				ProcRuntimeFinishedAtKey: time.Now(),
   132  				ProcRuntimeRuntimeKey:    runtime,
   133  			},
   134  		},
   135  	)
   136  }