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

     1  package event
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/mongodb/grip"
     7  )
     8  
     9  const (
    10  	// resource type
    11  	ResourceTypeHost = "HOST"
    12  
    13  	// event types
    14  	EventHostCreated              = "HOST_CREATED"
    15  	EventHostStatusChanged        = "HOST_STATUS_CHANGED"
    16  	EventHostDNSNameSet           = "HOST_DNS_NAME_SET"
    17  	EventHostProvisionFailed      = "HOST_PROVISION_FAILED"
    18  	EventHostProvisioned          = "HOST_PROVISIONED"
    19  	EventHostRunningTaskSet       = "HOST_RUNNING_TASK_SET"
    20  	EventHostRunningTaskCleared   = "HOST_RUNNING_TASK_CLEARED"
    21  	EventHostTaskPidSet           = "HOST_TASK_PID_SET"
    22  	EventHostMonitorFlag          = "HOST_MONITOR_FLAG"
    23  	EventTaskFinished             = "HOST_TASK_FINISHED"
    24  	EventHostTeardown             = "HOST_TEARDOWN"
    25  	EventHostTerminatedExternally = "HOST_TERMINATED_EXTERNALLY"
    26  )
    27  
    28  // implements EventData
    29  type HostEventData struct {
    30  	// necessary for IsValid
    31  	ResourceType string `bson:"r_type" json:"resource_type"`
    32  
    33  	OldStatus  string        `bson:"o_s,omitempty" json:"old_status,omitempty"`
    34  	NewStatus  string        `bson:"n_s,omitempty" json:"new_status,omitempty"`
    35  	Logs       string        `bson:"log,omitempty" json:"logs,omitempty"`
    36  	Hostname   string        `bson:"hn,omitempty" json:"hostname,omitempty"`
    37  	TaskId     string        `bson:"t_id,omitempty" json:"task_id,omitempty"`
    38  	TaskPid    string        `bson:"t_pid,omitempty" json:"task_pid,omitempty"`
    39  	TaskStatus string        `bson:"t_st,omitempty" json:"task_status,omitempty"`
    40  	MonitorOp  string        `bson:"monitor_op,omitempty" json:"monitor,omitempty"`
    41  	Successful bool          `bson:"successful,omitempty" json:"successful"`
    42  	Duration   time.Duration `bson:"duration,omitempty" json:"duration"`
    43  }
    44  
    45  func (self HostEventData) IsValid() bool {
    46  	return self.ResourceType == ResourceTypeHost
    47  }
    48  
    49  func LogHostEvent(hostId string, eventType string, eventData HostEventData) {
    50  	eventData.ResourceType = ResourceTypeHost
    51  	event := Event{
    52  		Timestamp:  time.Now(),
    53  		ResourceId: hostId,
    54  		EventType:  eventType,
    55  		Data:       DataWrapper{eventData},
    56  	}
    57  
    58  	logger := NewDBEventLogger(AllLogCollection)
    59  	if err := logger.LogEvent(event); err != nil {
    60  		grip.Errorf("Error logging host event: %+v", err)
    61  	}
    62  }
    63  
    64  func LogHostCreated(hostId string) {
    65  	LogHostEvent(hostId, EventHostCreated, HostEventData{})
    66  }
    67  
    68  func LogHostTerminatedExternally(hostId string) {
    69  	LogHostEvent(hostId, EventHostStatusChanged, HostEventData{NewStatus: EventHostTerminatedExternally})
    70  }
    71  
    72  func LogHostStatusChanged(hostId string, oldStatus string, newStatus string) {
    73  	if oldStatus == newStatus {
    74  		return
    75  	}
    76  	LogHostEvent(hostId, EventHostStatusChanged,
    77  		HostEventData{OldStatus: oldStatus, NewStatus: newStatus})
    78  }
    79  
    80  func LogHostDNSNameSet(hostId string, dnsName string) {
    81  	LogHostEvent(hostId, EventHostDNSNameSet,
    82  		HostEventData{Hostname: dnsName})
    83  }
    84  
    85  func LogHostProvisioned(hostId string) {
    86  	LogHostEvent(hostId, EventHostProvisioned, HostEventData{})
    87  }
    88  
    89  func LogHostRunningTaskSet(hostId string, taskId string) {
    90  	LogHostEvent(hostId, EventHostRunningTaskSet,
    91  		HostEventData{TaskId: taskId})
    92  }
    93  
    94  func LogHostRunningTaskCleared(hostId string, taskId string) {
    95  	LogHostEvent(hostId, EventHostRunningTaskCleared,
    96  		HostEventData{TaskId: taskId})
    97  }
    98  
    99  func LogHostTaskPidSet(hostId string, taskPid string) {
   100  	LogHostEvent(hostId, EventHostTaskPidSet, HostEventData{TaskPid: taskPid})
   101  }
   102  
   103  func LogProvisionFailed(hostId string, setupLogs string) {
   104  	LogHostEvent(hostId, EventHostProvisionFailed, HostEventData{Logs: setupLogs})
   105  }
   106  
   107  func LogHostTeardown(hostId, teardownLogs string, success bool, duration time.Duration) {
   108  	LogHostEvent(hostId, EventHostTeardown,
   109  		HostEventData{Logs: teardownLogs, Successful: success, Duration: duration})
   110  }
   111  
   112  func LogMonitorOperation(hostId string, op string) {
   113  	LogHostEvent(hostId, EventHostMonitorFlag, HostEventData{MonitorOp: op})
   114  }