OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package ui | 5 package ui |
6 | 6 |
7 import ( | 7 import ( |
8 "crypto/sha1" | 8 "crypto/sha1" |
9 "encoding/base64" | 9 "encoding/base64" |
10 "fmt" | 10 "fmt" |
11 "net/http" | 11 "net/http" |
12 "time" | 12 "time" |
13 | 13 |
14 mc "github.com/luci/gae/service/memcache" | 14 mc "github.com/luci/gae/service/memcache" |
15 "github.com/luci/luci-go/common/clock" | 15 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/scheduler/appengine/presentation" |
16 "github.com/luci/luci-go/server/auth" | 17 "github.com/luci/luci-go/server/auth" |
17 "github.com/luci/luci-go/server/router" | 18 "github.com/luci/luci-go/server/router" |
18 "github.com/luci/luci-go/server/templates" | 19 "github.com/luci/luci-go/server/templates" |
19 ) | 20 ) |
20 | 21 |
21 func jobPage(ctx *router.Context) { | 22 func jobPage(ctx *router.Context) { |
22 c, w, r, p := ctx.Context, ctx.Writer, ctx.Request, ctx.Params | 23 c, w, r, p := ctx.Context, ctx.Writer, ctx.Request, ctx.Params |
23 | 24 |
24 projectID := p.ByName("ProjectID") | 25 projectID := p.ByName("ProjectID") |
25 » jobID := p.ByName("JobID") | 26 » jobName := p.ByName("JobName") |
26 cursor := r.URL.Query().Get("c") | 27 cursor := r.URL.Query().Get("c") |
27 | 28 |
28 // Grab the job from the datastore. | 29 // Grab the job from the datastore. |
29 » job, err := config(c).Engine.GetJob(c, projectID+"/"+jobID) | 30 » job, err := config(c).Engine.GetJob(c, projectID+"/"+jobName) |
30 if err != nil { | 31 if err != nil { |
31 panic(err) | 32 panic(err) |
32 } | 33 } |
33 if job == nil { | 34 if job == nil { |
34 http.Error(w, "No such job", http.StatusNotFound) | 35 http.Error(w, "No such job", http.StatusNotFound) |
35 return | 36 return |
36 } | 37 } |
37 | 38 |
38 // Grab latest invocations from the datastore. | 39 // Grab latest invocations from the datastore. |
39 invs, nextCursor, err := config(c).Engine.ListInvocations(c, job.JobID,
50, cursor) | 40 invs, nextCursor, err := config(c).Engine.ListInvocations(c, job.JobID,
50, cursor) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 }) | 85 }) |
85 } | 86 } |
86 | 87 |
87 //////////////////////////////////////////////////////////////////////////////// | 88 //////////////////////////////////////////////////////////////////////////////// |
88 // Actions. | 89 // Actions. |
89 | 90 |
90 func runJobAction(ctx *router.Context) { | 91 func runJobAction(ctx *router.Context) { |
91 c, w, r, p := ctx.Context, ctx.Writer, ctx.Request, ctx.Params | 92 c, w, r, p := ctx.Context, ctx.Writer, ctx.Request, ctx.Params |
92 | 93 |
93 projectID := p.ByName("ProjectID") | 94 projectID := p.ByName("ProjectID") |
94 » jobID := p.ByName("JobID") | 95 » jobName := p.ByName("JobName") |
95 » if !isJobOwner(c, projectID, jobID) { | 96 » if !presentation.IsJobOwner(c, projectID, jobName) { |
96 http.Error(w, "Forbidden", 403) | 97 http.Error(w, "Forbidden", 403) |
97 return | 98 return |
98 } | 99 } |
99 | 100 |
100 // genericReply renders "we did something (or we failed to do something)
" | 101 // genericReply renders "we did something (or we failed to do something)
" |
101 // page, shown on error or if invocation is starting for too long. | 102 // page, shown on error or if invocation is starting for too long. |
102 genericReply := func(err error) { | 103 genericReply := func(err error) { |
103 templates.MustRender(c, w, "pages/run_job_result.html", map[stri
ng]interface{}{ | 104 templates.MustRender(c, w, "pages/run_job_result.html", map[stri
ng]interface{}{ |
104 "ProjectID": projectID, | 105 "ProjectID": projectID, |
105 » » » "JobID": jobID, | 106 » » » "JobName": jobName, |
106 "Error": err, | 107 "Error": err, |
107 }) | 108 }) |
108 } | 109 } |
109 | 110 |
110 // Enqueue new invocation request, and wait for corresponding invocation
to | 111 // Enqueue new invocation request, and wait for corresponding invocation
to |
111 // appear. Give up if task queue or datastore indexes are lagging too mu
ch. | 112 // appear. Give up if task queue or datastore indexes are lagging too mu
ch. |
112 e := config(c).Engine | 113 e := config(c).Engine |
113 » fullJobID := projectID + "/" + jobID | 114 » jobID := projectID + "/" + jobName |
114 » invNonce, err := e.TriggerInvocation(c, fullJobID, auth.CurrentIdentity(
c)) | 115 » invNonce, err := e.TriggerInvocation(c, jobID, auth.CurrentIdentity(c)) |
115 if err != nil { | 116 if err != nil { |
116 genericReply(err) | 117 genericReply(err) |
117 return | 118 return |
118 } | 119 } |
119 | 120 |
120 invID := int64(0) | 121 invID := int64(0) |
121 deadline := clock.Now(c).Add(10 * time.Second) | 122 deadline := clock.Now(c).Add(10 * time.Second) |
122 for invID == 0 && deadline.Sub(clock.Now(c)) > 0 { | 123 for invID == 0 && deadline.Sub(clock.Now(c)) > 0 { |
123 // Asking for invocation immediately after triggering it never w
orks, | 124 // Asking for invocation immediately after triggering it never w
orks, |
124 // so sleep a bit first. | 125 // so sleep a bit first. |
125 if tr := clock.Sleep(c, 600*time.Millisecond); tr.Incomplete() { | 126 if tr := clock.Sleep(c, 600*time.Millisecond); tr.Incomplete() { |
126 // The Context was canceled before the Sleep completed.
Terminate the | 127 // The Context was canceled before the Sleep completed.
Terminate the |
127 // loop. | 128 // loop. |
128 break | 129 break |
129 } | 130 } |
130 // Find most recent invocation with requested nonce. Ignore erro
rs here, | 131 // Find most recent invocation with requested nonce. Ignore erro
rs here, |
131 // since GetInvocationsByNonce can return only transient ones. | 132 // since GetInvocationsByNonce can return only transient ones. |
132 invs, _ := e.GetInvocationsByNonce(c, invNonce) | 133 invs, _ := e.GetInvocationsByNonce(c, invNonce) |
133 bestTS := time.Time{} | 134 bestTS := time.Time{} |
134 for _, inv := range invs { | 135 for _, inv := range invs { |
135 » » » if inv.JobKey.StringID() == fullJobID && inv.Started.Sub
(bestTS) > 0 { | 136 » » » if inv.JobKey.StringID() == jobID && inv.Started.Sub(bes
tTS) > 0 { |
136 invID = inv.ID | 137 invID = inv.ID |
137 bestTS = inv.Started | 138 bestTS = inv.Started |
138 } | 139 } |
139 } | 140 } |
140 } | 141 } |
141 | 142 |
142 if invID != 0 { | 143 if invID != 0 { |
143 » » http.Redirect(w, r, fmt.Sprintf("/jobs/%s/%s/%d", projectID, job
ID, invID), http.StatusFound) | 144 » » http.Redirect(w, r, fmt.Sprintf("/jobs/%s/%s/%d", projectID, job
Name, invID), http.StatusFound) |
144 } else { | 145 } else { |
145 genericReply(nil) // deadline | 146 genericReply(nil) // deadline |
146 } | 147 } |
147 } | 148 } |
148 | 149 |
149 func pauseJobAction(c *router.Context) { | 150 func pauseJobAction(c *router.Context) { |
150 handleJobAction(c, func(jobID string) error { | 151 handleJobAction(c, func(jobID string) error { |
151 who := auth.CurrentIdentity(c.Context) | 152 who := auth.CurrentIdentity(c.Context) |
152 return config(c.Context).Engine.PauseJob(c.Context, jobID, who) | 153 return config(c.Context).Engine.PauseJob(c.Context, jobID, who) |
153 }) | 154 }) |
154 } | 155 } |
155 | 156 |
156 func resumeJobAction(c *router.Context) { | 157 func resumeJobAction(c *router.Context) { |
157 handleJobAction(c, func(jobID string) error { | 158 handleJobAction(c, func(jobID string) error { |
158 who := auth.CurrentIdentity(c.Context) | 159 who := auth.CurrentIdentity(c.Context) |
159 return config(c.Context).Engine.ResumeJob(c.Context, jobID, who) | 160 return config(c.Context).Engine.ResumeJob(c.Context, jobID, who) |
160 }) | 161 }) |
161 } | 162 } |
162 | 163 |
163 func abortJobAction(c *router.Context) { | 164 func abortJobAction(c *router.Context) { |
164 handleJobAction(c, func(jobID string) error { | 165 handleJobAction(c, func(jobID string) error { |
165 who := auth.CurrentIdentity(c.Context) | 166 who := auth.CurrentIdentity(c.Context) |
166 return config(c.Context).Engine.AbortJob(c.Context, jobID, who) | 167 return config(c.Context).Engine.AbortJob(c.Context, jobID, who) |
167 }) | 168 }) |
168 } | 169 } |
169 | 170 |
170 func handleJobAction(c *router.Context, cb func(string) error) { | 171 func handleJobAction(c *router.Context, cb func(string) error) { |
171 projectID := c.Params.ByName("ProjectID") | 172 projectID := c.Params.ByName("ProjectID") |
172 » jobID := c.Params.ByName("JobID") | 173 » jobName := c.Params.ByName("JobName") |
173 » if !isJobOwner(c.Context, projectID, jobID) { | 174 » if !presentation.IsJobOwner(c.Context, projectID, jobName) { |
174 http.Error(c.Writer, "Forbidden", 403) | 175 http.Error(c.Writer, "Forbidden", 403) |
175 return | 176 return |
176 } | 177 } |
177 » if err := cb(projectID + "/" + jobID); err != nil { | 178 » if err := cb(projectID + "/" + jobName); err != nil { |
178 panic(err) | 179 panic(err) |
179 } | 180 } |
180 » http.Redirect(c.Writer, c.Request, fmt.Sprintf("/jobs/%s/%s", projectID,
jobID), http.StatusFound) | 181 » http.Redirect(c.Writer, c.Request, fmt.Sprintf("/jobs/%s/%s", projectID,
jobName), http.StatusFound) |
181 } | 182 } |
OLD | NEW |