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

     1  package alerts
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/evergreen-ci/evergreen"
     7  	"github.com/evergreen-ci/evergreen/model/alertrecord"
     8  )
     9  
    10  // Host Triggers
    11  
    12  type SpawnTwoHourWarning struct{}
    13  
    14  func (sthw SpawnTwoHourWarning) Id() string { return alertrecord.SpawnHostTwoHourWarning }
    15  
    16  func (sthw SpawnTwoHourWarning) Display() string {
    17  	return "Spawn host is due to expire in two hours"
    18  }
    19  
    20  func (sthw SpawnTwoHourWarning) CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord {
    21  	rec := newAlertRecord(ctx, alertrecord.SpawnHostTwoHourWarning)
    22  	return rec
    23  }
    24  
    25  func (sthw SpawnTwoHourWarning) ShouldExecute(ctx triggerContext) (bool, error) {
    26  	if ctx.host == nil || ctx.host.ExpirationTime.IsZero() ||
    27  		ctx.host.ExpirationTime.Sub(time.Now()) > (2*time.Hour) {
    28  		return false, nil
    29  	}
    30  	rec, err := alertrecord.FindOne(alertrecord.ByHostAlertRecordType(ctx.host.Id, alertrecord.SpawnHostTwoHourWarning))
    31  	if err != nil {
    32  		return false, err
    33  	}
    34  	return rec == nil, nil
    35  }
    36  
    37  type SpawnTwelveHourWarning struct{}
    38  
    39  func (sthw SpawnTwelveHourWarning) Id() string { return alertrecord.SpawnHostTwoHourWarning }
    40  
    41  func (sthw SpawnTwelveHourWarning) Display() string {
    42  	return "Spawn host is due to expire in twelve hours"
    43  }
    44  
    45  func (sthw SpawnTwelveHourWarning) CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord {
    46  	rec := newAlertRecord(ctx, alertrecord.SpawnHostTwelveHourWarning)
    47  	return rec
    48  }
    49  
    50  func (sthw SpawnTwelveHourWarning) ShouldExecute(ctx triggerContext) (bool, error) {
    51  	if ctx.host == nil || ctx.host.ExpirationTime.IsZero() ||
    52  		ctx.host.ExpirationTime.Sub(time.Now()) > (12*time.Hour) {
    53  		return false, nil
    54  	}
    55  	rec, err := alertrecord.FindOne(alertrecord.ByHostAlertRecordType(ctx.host.Id, alertrecord.SpawnHostTwelveHourWarning))
    56  	if err != nil {
    57  		return false, err
    58  	}
    59  	return rec == nil, nil
    60  }
    61  
    62  type SpawnFailure struct{}
    63  
    64  func (sf SpawnFailure) Id() string { return alertrecord.SpawnFailed }
    65  
    66  func (sf SpawnFailure) Display() string {
    67  	return "Spawn host startup failed."
    68  }
    69  
    70  func (sf SpawnFailure) CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord {
    71  	return nil
    72  }
    73  
    74  func (sf SpawnFailure) ShouldExecute(ctx triggerContext) (bool, error) {
    75  	return true, nil
    76  }
    77  
    78  type SlowProvisionWarning struct{}
    79  
    80  func (sthw SlowProvisionWarning) Id() string { return alertrecord.SlowProvisionWarning }
    81  
    82  func (sthw SlowProvisionWarning) Display() string {
    83  	return "Host is taking a long time to provision"
    84  }
    85  
    86  func (sthw SlowProvisionWarning) CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord {
    87  	rec := newAlertRecord(ctx, alertrecord.SlowProvisionWarning)
    88  	return rec
    89  }
    90  
    91  func (sthw SlowProvisionWarning) ShouldExecute(ctx triggerContext) (bool, error) {
    92  	// don't execute if the host is actually provisioned, or if it's been less than 20 minutes
    93  	// since creation time
    94  	if ctx.host.Provisioned || ctx.host.CreationTime.Before(time.Now().Add(-20*time.Minute)) {
    95  		return false, nil
    96  	}
    97  	rec, err := alertrecord.FindOne(alertrecord.ByHostAlertRecordType(ctx.host.Id, alertrecord.SlowProvisionWarning))
    98  	if err != nil {
    99  		return false, err
   100  	}
   101  	return rec == nil, nil
   102  }
   103  
   104  type ProvisionFailed struct{}
   105  
   106  func (pf ProvisionFailed) Id() string { return alertrecord.ProvisionFailed }
   107  
   108  func (pf ProvisionFailed) Display() string {
   109  	return "Host failed to provision"
   110  }
   111  
   112  func (pf ProvisionFailed) CreateAlertRecord(ctx triggerContext) *alertrecord.AlertRecord {
   113  	// No bookkeeping done for this trigger - since it is triggered synchronously in only one place.
   114  	return nil
   115  }
   116  
   117  func (pf *ProvisionFailed) ShouldExecute(ctx triggerContext) (bool, error) {
   118  	// don't execute if the host is actually provisioned
   119  	if ctx.host.Provisioned || ctx.host.Status == evergreen.HostRunning {
   120  		return false, nil
   121  	}
   122  	return true, nil
   123  }