github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/engine/local/reducers.go (about)

     1  package local
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/tilt-dev/tilt/internal/store"
     7  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
     8  	"github.com/tilt-dev/tilt/pkg/model"
     9  )
    10  
    11  // When the Cmd controller updates a command, check to see
    12  //
    13  // If the local serve cmd is watching the cmd, update
    14  // the local runtime state to match the cmd status.
    15  func HandleCmdUpdateStatusAction(state *store.EngineState, action CmdUpdateStatusAction) {
    16  	cmd, ok := state.Cmds[action.Cmd.Name]
    17  	if !ok {
    18  		return
    19  	}
    20  	cmd = cmd.DeepCopy()
    21  	cmd.Status = action.Cmd.Status
    22  	state.Cmds[action.Cmd.Name] = cmd
    23  	updateLocalRuntimeStatus(state, cmd)
    24  }
    25  
    26  // If the local serve cmd is watching the cmd, update
    27  // the local runtime state to match the cmd status.
    28  func updateLocalRuntimeStatus(state *store.EngineState, cmd *v1alpha1.Cmd) {
    29  	mn := model.ManifestName(cmd.Annotations[v1alpha1.AnnotationManifest])
    30  	mt, ok := state.ManifestTargets[mn]
    31  	if !ok {
    32  		delete(state.Cmds, cmd.Name)
    33  		return
    34  	}
    35  
    36  	ms := mt.State
    37  	lrs := ms.LocalRuntimeState()
    38  	if lrs.CmdName != cmd.Name {
    39  		return
    40  	}
    41  
    42  	state.Cmds[cmd.Name] = cmd
    43  
    44  	spec := cmd.Spec
    45  	status := cmd.Status
    46  	if status.Running != nil {
    47  		lrs.PID = int(cmd.Status.Running.PID)
    48  		lrs.StartTime = cmd.Status.Running.StartedAt.Time
    49  		lrs.FinishTime = time.Time{}
    50  
    51  		// Currently, Cmd is only used for servers.
    52  		// Make the Status OK when the readiness probe passes (if there is one).
    53  		if spec.ReadinessProbe == nil || cmd.Status.Ready {
    54  			lrs.Status = v1alpha1.RuntimeStatusOK
    55  		} else {
    56  			lrs.Status = v1alpha1.RuntimeStatusPending
    57  		}
    58  
    59  	} else if status.Terminated != nil {
    60  		// Currently, CMD is only used for servers,
    61  		// so any termination is an error.
    62  		lrs.PID = int(status.Terminated.PID)
    63  		lrs.Status = v1alpha1.RuntimeStatusError
    64  		lrs.StartTime = status.Terminated.StartedAt.Time
    65  		lrs.FinishTime = status.Terminated.FinishedAt.Time
    66  	} else {
    67  		lrs.Status = v1alpha1.RuntimeStatusPending
    68  		lrs.StartTime = time.Time{}
    69  		lrs.FinishTime = time.Time{}
    70  	}
    71  
    72  	if lrs.Ready != cmd.Status.Ready {
    73  		lrs.Ready = cmd.Status.Ready
    74  		if lrs.Ready {
    75  			lrs.LastReadyOrSucceededTime = time.Now()
    76  		}
    77  	}
    78  	lrs.SpanID = model.LogSpanID(cmd.ObjectMeta.Annotations[v1alpha1.AnnotationSpanID])
    79  
    80  	ms.RuntimeState = lrs
    81  }
    82  
    83  // When the local controller creates a new command, link
    84  // that command to the Local runtime state.
    85  func HandleCmdCreateAction(state *store.EngineState, action CmdCreateAction) {
    86  	cmd := action.Cmd
    87  	mn := model.ManifestName(cmd.Annotations[v1alpha1.AnnotationManifest])
    88  	mt, ok := state.ManifestTargets[mn]
    89  	if !ok {
    90  		return
    91  	}
    92  
    93  	ms := mt.State
    94  	lrs := ms.LocalRuntimeState()
    95  	lrs.CmdName = cmd.Name
    96  	ms.RuntimeState = lrs
    97  
    98  	updateLocalRuntimeStatus(state, cmd)
    99  }
   100  
   101  // Mark the command for deletion.
   102  func HandleCmdDeleteAction(state *store.EngineState, action CmdDeleteAction) {
   103  	delete(state.Cmds, action.Name)
   104  }