Skip to content

Commit e6fbf50

Browse files
authored
feat: add an endpoint to manually pause a coder task (#21889)
Closes coder/internal#1261. This pull request adds an endpoint to pause coder tasks by stopping the underlying workspace. * Instead of `POST /api/v2/tasks/{user}/{task}/pause`, the endpoint is currently experimental. * We do not currently set the build reason to `task_manual_pause`, because build reasons are currently only used on stop transitions.
1 parent d3036d5 commit e6fbf50

11 files changed

Lines changed: 825 additions & 16 deletions

File tree

‎coderd/aitasks.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,3 +1244,63 @@ func (api *API) postWorkspaceAgentTaskLogSnapshot(rw http.ResponseWriter, r *htt
12441244

12451245
rw.WriteHeader(http.StatusNoContent)
12461246
}
1247+
1248+
// @Summary Pause task
1249+
// @ID pause-task
1250+
// @Security CoderSessionToken
1251+
// @Accept json
1252+
// @Tags Tasks
1253+
// @Param user path string true "Username, user ID, or 'me' for the authenticated user"
1254+
// @Param task path string true "Task ID" format(uuid)
1255+
// @Success 202 {object} codersdk.PauseTaskResponse
1256+
// @Router /tasks/{user}/{task}/pause [post]
1257+
func (api *API) pauseTask(rw http.ResponseWriter, r *http.Request) {
1258+
var (
1259+
ctx = r.Context()
1260+
apiKey = httpmw.APIKey(r)
1261+
task = httpmw.TaskParam(r)
1262+
)
1263+
1264+
if !task.WorkspaceID.Valid {
1265+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1266+
Message: "Task does not have a workspace.",
1267+
})
1268+
return
1269+
}
1270+
1271+
workspace, err := api.Database.GetWorkspaceByID(ctx, task.WorkspaceID.UUID)
1272+
if err != nil {
1273+
if httpapi.Is404Error(err) {
1274+
httpapi.ResourceNotFound(rw)
1275+
return
1276+
}
1277+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1278+
Message: "Internal error fetching task workspace.",
1279+
Detail: err.Error(),
1280+
})
1281+
return
1282+
}
1283+
1284+
buildReq := codersdk.CreateWorkspaceBuildRequest{
1285+
Transition: codersdk.WorkspaceTransitionStop,
1286+
Reason: codersdk.CreateWorkspaceBuildReasonTaskManualPause,
1287+
}
1288+
build, err := api.postWorkspaceBuildsInternal(
1289+
ctx,
1290+
apiKey,
1291+
workspace,
1292+
buildReq,
1293+
func(action policy.Action, object rbac.Objecter) bool {
1294+
return api.Authorize(r, action, object)
1295+
},
1296+
audit.WorkspaceBuildBaggageFromRequest(r),
1297+
)
1298+
if err != nil {
1299+
httperror.WriteWorkspaceBuildError(ctx, rw, err)
1300+
return
1301+
}
1302+
1303+
httpapi.Write(ctx, rw, http.StatusAccepted, codersdk.PauseTaskResponse{
1304+
WorkspaceBuild: &build,
1305+
})
1306+
}

0 commit comments

Comments
 (0)