Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · server/web/run_actions.go

Line attribution on main · 91 line(s)

0b26547e
Keenan Nemetz
15d ago
1 package web
2
0c14f078
Keenan Nemetz
6d ago
3 // CI actions on one run: cancel it while it waits, ask a running job to stop, or
4 // enqueue the same pinned job again. A running job is stopped by its runner at the
5 // next heartbeat; the queue cannot kill a process it does not own, but it can say
6 // that a maintainer asked.
0b26547e
Keenan Nemetz
15d ago
7
8 import (
9 "net/http"
10 "time"
11
d37d301f
Keenan Nemetz
17h ago
12 "rodent.software/vole/ci/runs"
13 "rodent.software/vole/forge/repo"
0b26547e
Keenan Nemetz
15d ago
14 )
15
16 func (w *Web) runAction(rw http.ResponseWriter, r *http.Request) (*repo.Meta, *runs.Run, string, bool) {
c717d4eb
Keenan Nemetz
13d ago
17 ctx, ok := w.beginWrite(rw, r, maxSmallForm)
0b26547e
Keenan Nemetz
15d ago
18 if !ok {
19 return nil, nil, "", false
20 }
21 if w.Runs == nil {
22 w.notFound(rw, r)
23 return nil, nil, "", false
24 }
c717d4eb
Keenan Nemetz
13d ago
25 // Managing a run is the maintainer capability, not triage of one item.
26 if !canMaintain(ctx.ACL, ctx.Name) {
27 w.refuse(rw, r, "only a maintainer or admin may manage CI")
0b26547e
Keenan Nemetz
15d ago
28 return nil, nil, "", false
29 }
30 run, err := w.Runs.Get(r.PathValue("id"))
c717d4eb
Keenan Nemetz
13d ago
31 if err != nil || run == nil || run.Repo != ctx.Repo.Name {
0b26547e
Keenan Nemetz
15d ago
32 w.notFound(rw, r)
33 return nil, nil, "", false
34 }
c717d4eb
Keenan Nemetz
13d ago
35 return ctx.Repo, run, ctx.Name, true
0b26547e
Keenan Nemetz
15d ago
36 }
37
38 func (w *Web) CancelRun(rw http.ResponseWriter, r *http.Request) {
39 m, run, accountID, ok := w.runAction(rw, r)
40 if !ok {
41 return
42 }
0c14f078
Keenan Nemetz
6d ago
43 now := time.Now().UTC()
44 var canceled bool
45 var err error
46 switch run.Status {
47 case runs.StatusQueued:
48 // Waiting work is retired outright: no runner has it, so nothing needs to be
49 // asked to stop.
50 canceled, err = w.Runs.CancelQueued(run.ID, m.Name, accountID, now)
51 case runs.StatusRunning:
52 // The runner holds the lease and learns about this on its next heartbeat. The
53 // run is not canceled yet: it becomes canceled when the attempt comes back.
54 canceled, err = w.Runs.CancelRunning(run.ID, m.Name, accountID, now)
55 default:
56 http.Error(rw, "only a queued or running run can be canceled", http.StatusBadRequest)
57 return
58 }
0b26547e
Keenan Nemetz
15d ago
59 if err != nil {
60 w.serverError(rw, r, "cannot cancel the run: "+err.Error())
61 return
62 }
63 if !canceled {
0c14f078
Keenan Nemetz
6d ago
64 http.Error(rw, "this run has already finished; nothing was canceled", http.StatusBadRequest)
0b26547e
Keenan Nemetz
15d ago
65 return
66 }
deb024d4
Keenan Nemetz
6d ago
67 w.audit("run.cancel", accountID, m.Name, run.ID)
ed03b3d1
Keenan Nemetz
4h ago
68 http.Redirect(rw, r, "/"+m.Name+"/runs/"+run.ID, http.StatusSeeOther)
0b26547e
Keenan Nemetz
15d ago
69 }
70
71 func (w *Web) Rerun(rw http.ResponseWriter, r *http.Request) {
72 m, run, accountID, ok := w.runAction(rw, r)
73 if !ok {
74 return
75 }
76 if run.Status == runs.StatusQueued || run.Status == runs.StatusRunning {
77 http.Error(rw, "this run has not finished yet", http.StatusBadRequest)
78 return
79 }
80 queued, err := w.Runs.Requeue(run.ID, m.Name, time.Now().UTC())
81 if err != nil {
82 w.serverError(rw, r, "cannot queue the run: "+err.Error())
83 return
84 }
85 if !queued {
86 http.Error(rw, "only a finished run can be rerun", http.StatusBadRequest)
87 return
88 }
70291860
Keenan Nemetz
6d ago
89 w.audit("run.rerun", accountID, m.Name, run.ID)
ed03b3d1
Keenan Nemetz
4h ago
90 http.Redirect(rw, r, "/"+m.Name+"/runs/"+run.ID, http.StatusSeeOther)
0b26547e
Keenan Nemetz
15d ago
91 }