-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
jobs_controller.go
95 lines (77 loc) · 2.64 KB
/
jobs_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package controllers
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/runatlantis/atlantis/server/controllers/web_templates"
"github.com/runatlantis/atlantis/server/controllers/websocket"
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
tally "github.com/uber-go/tally/v4"
)
type JobIDKeyGenerator struct{}
func (g JobIDKeyGenerator) Generate(r *http.Request) (string, error) {
jobID, ok := mux.Vars(r)["job-id"]
if !ok {
return "", fmt.Errorf("internal error: no job-id in route")
}
return jobID, nil
}
type JobsController struct {
AtlantisVersion string
AtlantisURL *url.URL
Logger logging.SimpleLogging
ProjectJobsTemplate web_templates.TemplateWriter
ProjectJobsErrorTemplate web_templates.TemplateWriter
Backend locking.Backend
WsMux *websocket.Multiplexor
KeyGenerator JobIDKeyGenerator
StatsScope tally.Scope
}
func (j *JobsController) getProjectJobs(w http.ResponseWriter, r *http.Request) error {
jobID, err := j.KeyGenerator.Generate(r)
if err != nil {
j.respond(w, logging.Error, http.StatusBadRequest, "%s", err.Error())
return err
}
viewData := web_templates.ProjectJobData{
AtlantisVersion: j.AtlantisVersion,
ProjectPath: jobID,
CleanedBasePath: j.AtlantisURL.Path,
}
return j.ProjectJobsTemplate.Execute(w, viewData)
}
func (j *JobsController) GetProjectJobs(w http.ResponseWriter, r *http.Request) {
errorCounter := j.StatsScope.SubScope("getprojectjobs").Counter(metrics.ExecutionErrorMetric)
err := j.getProjectJobs(w, r)
if err != nil {
j.Logger.Err(err.Error())
errorCounter.Inc(1)
}
}
func (j *JobsController) getProjectJobsWS(w http.ResponseWriter, r *http.Request) error {
err := j.WsMux.Handle(w, r)
if err != nil {
j.respond(w, logging.Error, http.StatusInternalServerError, "%s", err.Error())
return err
}
return nil
}
func (j *JobsController) GetProjectJobsWS(w http.ResponseWriter, r *http.Request) {
jobsMetric := j.StatsScope.SubScope("getprojectjobs")
errorCounter := jobsMetric.Counter(metrics.ExecutionErrorMetric)
executionTime := jobsMetric.Timer(metrics.ExecutionTimeMetric).Start()
defer executionTime.Stop()
err := j.getProjectJobsWS(w, r)
if err != nil {
errorCounter.Inc(1)
}
}
func (j *JobsController) respond(w http.ResponseWriter, lvl logging.LogLevel, responseCode int, format string, args ...interface{}) {
response := fmt.Sprintf(format, args...)
j.Logger.Log(lvl, response)
w.WriteHeader(responseCode)
fmt.Fprintln(w, response)
}