@Summary Get AI task logs @ID get-ai-task-logs @Security CoderSessionToken @Produce json @Tags Tasks @Param user path string true "Username, user ID, or 'me' for the authenticated user" @Param task path string true "Task ID, or task name" @Success 200 {object} codersdk.TaskLogsResponse @Router /api/
(rw http.ResponseWriter, r *http.Request)
| 832 | // @Success 200 {object} codersdk.TaskLogsResponse |
| 833 | // @Router /api/v2/tasks/{user}/{task}/logs [get] |
| 834 | func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) { |
| 835 | ctx := r.Context() |
| 836 | task := httpmw.TaskParam(r) |
| 837 | |
| 838 | switch task.Status { |
| 839 | case database.TaskStatusActive: |
| 840 | // Active tasks: fetch live logs from AgentAPI. |
| 841 | out, err := api.fetchLiveTaskLogs(r, task) |
| 842 | if err != nil { |
| 843 | httperror.WriteResponseError(ctx, rw, err) |
| 844 | return |
| 845 | } |
| 846 | |
| 847 | httpapi.Write(ctx, rw, http.StatusOK, out) |
| 848 | |
| 849 | case database.TaskStatusPaused, database.TaskStatusPending, database.TaskStatusInitializing: |
| 850 | // In pause, pending and initializing states, we attempt to fetch |
| 851 | // the snapshot from database to provide continuity. |
| 852 | out, err := api.fetchSnapshotTaskLogs(ctx, task.ID) |
| 853 | if err != nil { |
| 854 | httperror.WriteResponseError(ctx, rw, err) |
| 855 | return |
| 856 | } |
| 857 | |
| 858 | httpapi.Write(ctx, rw, http.StatusOK, out) |
| 859 | |
| 860 | default: |
| 861 | // Cases: database.TaskStatusError, database.TaskStatusUnknown. |
| 862 | // - Error: snapshot would be stale from previous pause. |
| 863 | // - Unknown: cannot determine reliable state. |
| 864 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 865 | Message: "Cannot fetch logs for task in current state.", |
| 866 | Detail: fmt.Sprintf("Task status is %q.", task.Status), |
| 867 | }) |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | func (api *API) fetchLiveTaskLogs(r *http.Request, task database.Task) (codersdk.TaskLogsResponse, error) { |
| 872 | var out codersdk.TaskLogsResponse |
nothing calls this directly
no test coverage detected