forked from concourse/concourse
/
clear_task_cache.go
81 lines (69 loc) · 2.13 KB
/
clear_task_cache.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
package jobserver
import (
"encoding/json"
"fmt"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/db"
"github.com/google/jsonapi"
)
func (s *Server) ClearTaskCache(pipeline db.Pipeline) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("clear-task-cache")
jobName := r.FormValue(":job_name")
stepName := r.FormValue(":step_name")
cachePath := r.FormValue(atc.ClearTaskCacheQueryPath)
job, found, err := pipeline.Job(jobName)
if err != nil {
logger.Error("failed-to-get-job", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Debug("could-not-find-job", lager.Data{
"jobName": jobName,
"stepName": stepName,
"cachePath": cachePath,
})
w.Header().Set("Content-Type", jsonapi.MediaType)
w.WriteHeader(http.StatusNotFound)
_ = jsonapi.MarshalErrors(w, []*jsonapi.ErrorObject{{
Title: "Job Not Found Error",
Detail: fmt.Sprintf("Job with name '%s' not found.", jobName),
Status: "404",
}})
return
}
rowsDeleted, err := job.ClearTaskCache(stepName, cachePath)
if err != nil {
logger.Error("failed-to-clear-task-cache", err)
w.Header().Set("Content-Type", jsonapi.MediaType)
w.WriteHeader(http.StatusInternalServerError)
_ = jsonapi.MarshalErrors(w, []*jsonapi.ErrorObject{{
Title: "Clear Task Cache Error",
Detail: err.Error(),
Status: "500",
}})
return
}
s.writeJSONResponse(w, atc.ClearTaskCacheResponse{CachesRemoved: rowsDeleted})
})
}
func (s *Server) writeJSONResponse(w http.ResponseWriter, obj interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
responseJSON, err := json.Marshal(obj)
if err != nil {
s.logger.Error("failed-to-marshal-response", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "failed to generate error response: %s", err)
return
}
_, err = w.Write(responseJSON)
if err != nil {
s.logger.Error("failed-to-write-response", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}