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

Method fetchSnapshotTaskLogs

coderd/aitasks.go:906–973  ·  view source on GitHub ↗
(ctx context.Context, taskID uuid.UUID)

Source from the content-addressed store, hash-verified

904}
905
906func (api *API) fetchSnapshotTaskLogs(ctx context.Context, taskID uuid.UUID) (codersdk.TaskLogsResponse, error) {
907 snapshot, err := api.Database.GetTaskSnapshot(ctx, taskID)
908 if err != nil {
909 if httpapi.IsUnauthorizedError(err) {
910 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusNotFound, codersdk.Response{
911 Message: "Resource not found.",
912 })
913 }
914 if errors.Is(err, sql.ErrNoRows) {
915 // No snapshot exists yet, return empty logs. Snapshot is true
916 // because this field indicates whether the data is from the
917 // live task app (false) or not (true). Since the task is
918 // paused/initializing/pending, we cannot fetch live logs, so
919 // snapshot must be true even with no snapshot data.
920 return codersdk.TaskLogsResponse{
921 Logs: []codersdk.TaskLogEntry{},
922 Snapshot: true,
923 }, nil
924 }
925 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
926 Message: "Internal error fetching task snapshot.",
927 Detail: err.Error(),
928 })
929 }
930
931 // Unmarshal envelope with pre-populated data field to decode once.
932 envelope := TaskLogSnapshotEnvelope{
933 Data: &agentapisdk.GetMessagesResponse{},
934 }
935 if err := json.Unmarshal(snapshot.LogSnapshot, &envelope); err != nil {
936 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
937 Message: "Internal error decoding task snapshot.",
938 Detail: err.Error(),
939 })
940 }
941
942 // Validate snapshot format.
943 if envelope.Format != "agentapi" {
944 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
945 Message: "Unsupported task snapshot format.",
946 Detail: fmt.Sprintf("Expected format %q, got %q.", "agentapi", envelope.Format),
947 })
948 }
949
950 // Extract agentapi data from envelope (already decoded into the correct type).
951 messagesResp, ok := envelope.Data.(*agentapisdk.GetMessagesResponse)
952 if !ok {
953 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
954 Message: "Internal error decoding snapshot data.",
955 Detail: "Unexpected data type in envelope.",
956 })
957 }
958
959 // Convert agentapi messages to log entries.
960 logs, err := convertAgentAPIMessagesToLogEntries(messagesResp.Messages)
961 if err != nil {
962 return codersdk.TaskLogsResponse{}, httperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
963 Message: "Invalid snapshot data.",

Callers 1

taskLogsMethod · 0.95

Calls 8

IsUnauthorizedErrorFunction · 0.92
NewResponseErrorFunction · 0.92
RefFunction · 0.92
GetTaskSnapshotMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected