MCPcopy Index your code
hub / github.com/coder/coder / reportLifecycle

Method reportLifecycle

agent/agent.go:822–874  ·  view source on GitHub ↗

reportLifecycle reports the current lifecycle state once. All state changes are reported in order.

(ctx context.Context, aAPI proto.DRPCAgentClient28)

Source from the content-addressed store, hash-verified

820// reportLifecycle reports the current lifecycle state once. All state
821// changes are reported in order.
822func (a *agent) reportLifecycle(ctx context.Context, aAPI proto.DRPCAgentClient28) error {
823 for {
824 select {
825 case <-a.lifecycleUpdate:
826 case <-ctx.Done():
827 return ctx.Err()
828 }
829
830 for {
831 a.lifecycleMu.RLock()
832 lastIndex := len(a.lifecycleStates) - 1
833 report := a.lifecycleStates[a.lifecycleLastReportedIndex]
834 if len(a.lifecycleStates) > a.lifecycleLastReportedIndex+1 {
835 report = a.lifecycleStates[a.lifecycleLastReportedIndex+1]
836 }
837 a.lifecycleMu.RUnlock()
838
839 if lastIndex == a.lifecycleLastReportedIndex {
840 break
841 }
842 l, err := agentsdk.ProtoFromLifecycle(report)
843 if err != nil {
844 a.logger.Critical(ctx, "failed to convert lifecycle state", slog.F("report", report))
845 // Skip this report; there is no point retrying. Maybe we can successfully convert the next one?
846 a.lifecycleLastReportedIndex++
847 continue
848 }
849 payload := &proto.UpdateLifecycleRequest{Lifecycle: l}
850 logger := a.logger.With(slog.F("payload", payload))
851 logger.Debug(ctx, "reporting lifecycle state")
852
853 _, err = aAPI.UpdateLifecycle(ctx, payload)
854 if err != nil {
855 return xerrors.Errorf("failed to update lifecycle: %w", err)
856 }
857
858 logger.Debug(ctx, "successfully reported lifecycle state")
859 a.lifecycleLastReportedIndex++
860 select {
861 case a.lifecycleReported <- report.State:
862 case <-a.lifecycleReported:
863 a.lifecycleReported <- report.State
864 }
865 if a.lifecycleLastReportedIndex < lastIndex {
866 // Keep reporting until we've sent all messages, we can't
867 // rely on the channel triggering us before the backlog is
868 // consumed.
869 continue
870 }
871 break
872 }
873 }
874}
875
876// setLifecycle sets the lifecycle state and notifies the lifecycle loop.
877// The state is only updated if it's a valid state transition.

Callers

nothing calls this directly

Calls 5

ProtoFromLifecycleFunction · 0.92
ErrMethod · 0.80
UpdateLifecycleMethod · 0.65
DoneMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected