(ctx context.Context, taskID uuid.UUID)
| 904 | } |
| 905 | |
| 906 | func (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.", |
no test coverage detected