gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/executors/docker/machine/details.go (about)

     1  package machine
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    11  )
    12  
    13  type machineDetails struct {
    14  	Name       string
    15  	Created    time.Time `yaml:"-"`
    16  	Used       time.Time `yaml:"-"`
    17  	UsedCount  int
    18  	State      machineState
    19  	Reason     string
    20  	RetryCount int
    21  	LastSeen   time.Time
    22  }
    23  
    24  func (m *machineDetails) isUsed() bool {
    25  	return m.State != machineStateIdle
    26  }
    27  
    28  func (m *machineDetails) isStuckOnRemove() bool {
    29  	return m.State == machineStateRemoving && m.RetryCount >= removeRetryTries
    30  }
    31  
    32  func (m *machineDetails) isDead() bool {
    33  	return m.State == machineStateIdle &&
    34  		time.Since(m.LastSeen) > machineDeadInterval
    35  }
    36  
    37  func (m *machineDetails) canBeUsed() bool {
    38  	return m.State == machineStateAcquired
    39  }
    40  
    41  func (m *machineDetails) match(machineFilter string) bool {
    42  	var query string
    43  	if n, _ := fmt.Sscanf(m.Name, machineFilter, &query); n != 1 {
    44  		return false
    45  	}
    46  	return true
    47  }
    48  
    49  func (m *machineDetails) writeDebugInformation() {
    50  	if logrus.GetLevel() < logrus.DebugLevel {
    51  		return
    52  	}
    53  
    54  	var details struct {
    55  		Details    machineDetails
    56  		Time       string
    57  		CreatedAgo time.Duration
    58  	}
    59  	details.Details = *m
    60  	details.Time = time.Now().String()
    61  	details.CreatedAgo = time.Since(m.Created)
    62  	data := helpers.ToYAML(&details)
    63  	ioutil.WriteFile("machines/"+details.Details.Name+".yml", []byte(data), 0600)
    64  }
    65  
    66  func (m *machineDetails) logger() *logrus.Entry {
    67  	return logrus.WithFields(logrus.Fields{
    68  		"name":      m.Name,
    69  		"lifetime":  time.Since(m.Created),
    70  		"used":      time.Since(m.Used),
    71  		"usedCount": m.UsedCount,
    72  		"reason":    m.Reason,
    73  	})
    74  }
    75  
    76  type machinesDetails map[string]*machineDetails