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

     1  package build
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/evergreen-ci/evergreen"
     7  	"github.com/evergreen-ci/evergreen/apimodels"
     8  	"github.com/evergreen-ci/evergreen/util"
     9  	"gopkg.in/mgo.v2/bson"
    10  )
    11  
    12  // Creates a new task cache with the specified id, display name, and value for
    13  // activated.
    14  func NewTaskCache(id string, displayName string, activated bool) TaskCache {
    15  	return TaskCache{
    16  		Id:          id,
    17  		DisplayName: displayName,
    18  		Status:      evergreen.TaskUndispatched,
    19  		StartTime:   util.ZeroTime,
    20  		TimeTaken:   time.Duration(0),
    21  		Activated:   activated,
    22  	}
    23  }
    24  
    25  // SetTasksCache updates one build with the given id
    26  // to contain the given task caches.
    27  func SetTasksCache(buildId string, tasks []TaskCache) error {
    28  	return UpdateOne(
    29  		bson.M{IdKey: buildId},
    30  		bson.M{"$set": bson.M{TasksKey: tasks}},
    31  	)
    32  }
    33  
    34  // updateOneTaskCache is a helper for updating a single cached task for a build.
    35  func updateOneTaskCache(buildId, taskId string, updateDoc bson.M) error {
    36  	return UpdateOne(
    37  		bson.M{
    38  			IdKey: buildId,
    39  			TasksKey + "." + TaskCacheIdKey: taskId,
    40  		},
    41  		updateDoc,
    42  	)
    43  }
    44  
    45  // SetCachedTaskDispatched sets the given task to "dispatched"
    46  // in the cache of the given build.
    47  func SetCachedTaskDispatched(buildId, taskId string) error {
    48  	return updateOneTaskCache(buildId, taskId, bson.M{
    49  		"$set": bson.M{TasksKey + ".$." + TaskCacheStatusKey: evergreen.TaskDispatched},
    50  	})
    51  }
    52  
    53  // SetCachedTaskUndispatched sets the given task to "undispatched"
    54  // in the cache of the given build.
    55  func SetCachedTaskUndispatched(buildId, taskId string) error {
    56  	return updateOneTaskCache(buildId, taskId, bson.M{
    57  		"$set": bson.M{TasksKey + ".$." + TaskCacheStatusKey: evergreen.TaskUndispatched},
    58  	})
    59  }
    60  
    61  // SetCachedTaskStarted sets the given task to "started"
    62  // in the cache of the given build.
    63  func SetCachedTaskStarted(buildId, taskId string, startTime time.Time) error {
    64  	return updateOneTaskCache(buildId, taskId, bson.M{
    65  		"$set": bson.M{
    66  			TasksKey + ".$." + TaskCacheStartTimeKey: startTime,
    67  			TasksKey + ".$." + TaskCacheStatusKey:    evergreen.TaskStarted,
    68  		},
    69  	})
    70  }
    71  
    72  // SetCachedTaskFinished sets the given task to "finished"
    73  // along with a time taken in the cache of the given build.
    74  func SetCachedTaskFinished(buildId, taskId string, detail *apimodels.TaskEndDetail, timeTaken time.Duration) error {
    75  	return updateOneTaskCache(buildId, taskId, bson.M{
    76  		"$set": bson.M{
    77  			TasksKey + ".$." + TaskCacheTimeTakenKey:     timeTaken,
    78  			TasksKey + ".$." + TaskCacheStatusKey:        detail.Status,
    79  			TasksKey + ".$." + TaskCacheStatusDetailsKey: detail,
    80  		},
    81  	})
    82  }
    83  
    84  // SetCachedTaskActivated sets the given task to active or inactive
    85  // in the cache of the given build.
    86  func SetCachedTaskActivated(buildId, taskId string, active bool) error {
    87  	return updateOneTaskCache(buildId, taskId, bson.M{
    88  		"$set": bson.M{TasksKey + ".$." + TaskCacheActivatedKey: active},
    89  	})
    90  }
    91  
    92  // ResetCachedTask resets the given task
    93  // in the cache of the given build.
    94  func ResetCachedTask(buildId, taskId string) error {
    95  	return updateOneTaskCache(buildId, taskId, bson.M{
    96  		"$set": bson.M{
    97  			TasksKey + ".$." + TaskCacheStartTimeKey: util.ZeroTime,
    98  			TasksKey + ".$." + TaskCacheStatusKey:    evergreen.TaskUndispatched,
    99  		},
   100  		"$unset": bson.M{
   101  			TasksKey + ".$." + TaskCacheStatusDetailsKey: "",
   102  		},
   103  	})
   104  }