frontend: return state atomically, remove GetSubmitQueue

This commit is contained in:
Florian Klink 2019-11-25 18:12:38 +01:00
parent 3898495d56
commit b65f56c6e2
2 changed files with 21 additions and 42 deletions

View file

@ -16,20 +16,13 @@ import (
//TODO: log last update //TODO: log last update
// Frontend holds a gin Engine and the Sergequeue object
type Frontend struct {
Router *gin.Engine
SubmitQueue *submitqueue.SubmitQueue
}
//loadTemplate loads a single template from statikFS and returns a template object //loadTemplate loads a single template from statikFS and returns a template object
func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) { func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) {
tmpl := template.New(templateName).Funcs(funcMap)
statikFS, err := fs.New() statikFS, err := fs.New()
if err != nil { if err != nil {
return nil, err return nil, err
} }
tmpl := template.New(templateName).Funcs(funcMap)
r, err := statikFS.Open("/" + templateName) r, err := statikFS.Open("/" + templateName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -43,10 +36,16 @@ func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Temp
} }
// MakeFrontend configures the router and returns a new Frontend struct // MakeFrontend configures the router and returns a new Frontend struct
func MakeFrontend(runner *submitqueue.Runner) *Frontend { func MakeFrontend(runner *submitqueue.Runner) http.Handler {
router := gin.Default() router := gin.Default()
submitQueue := runner.GetSubmitQueue() router.GET("/submit-queue.json", func(c *gin.Context) {
submitQueue, _, _ := runner.GetState()
c.JSON(http.StatusOK, submitQueue)
})
router.GET("/", func(c *gin.Context) {
submitQueue, _, _ := runner.GetState()
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"isAutoSubmittable": func(serie *submitqueue.Serie) bool { "isAutoSubmittable": func(serie *submitqueue.Serie) bool {
@ -59,26 +58,12 @@ func MakeFrontend(runner *submitqueue.Runner) *Frontend {
tmpl := template.Must(loadTemplate("submit-queue.tmpl.html", funcMap)) tmpl := template.Must(loadTemplate("submit-queue.tmpl.html", funcMap))
router.SetHTMLTemplate(tmpl) tmpl.ExecuteTemplate(c.Writer, "submit-queue.tmpl.html", gin.H{
router.GET("/submit-queue.json", func(c *gin.Context) {
c.JSON(http.StatusOK, submitQueue)
})
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "submit-queue.tmpl.html", gin.H{
"series": submitQueue.Series, "series": submitQueue.Series,
"projectName": submitQueue.ProjectName, "projectName": submitQueue.ProjectName,
"branchName": submitQueue.BranchName, "branchName": submitQueue.BranchName,
"HEAD": submitQueue.HEAD, "HEAD": submitQueue.HEAD,
}) })
}) })
return &Frontend{ return router
Router: router,
SubmitQueue: submitQueue,
}
}
func (f *Frontend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.Router.ServeHTTP(w, r)
} }

View file

@ -20,17 +20,11 @@ func NewRunner(sq *SubmitQueue) *Runner {
} }
} }
// For the frontend to consume the data // GetState returns a copy of all the state for the frontend
// TODO: extend to return all the submitQueue results func (r *Runner) GetState() (SubmitQueue, *time.Time, []*Result) {
func (r *Runner) GetResults() (*time.Time, []*Result) {
r.mut.Lock() r.mut.Lock()
defer r.mut.Unlock() defer r.mut.Unlock()
return r.currentlyRunning, r.results return *r.submitQueue, r.currentlyRunning, r.results
}
// GetSubmitQueue returns the submit queue object, to be consumed by the frontend
func (r *Runner) GetSubmitQueue() (*SubmitQueue) {
return r.submitQueue
} }
// Trigger starts a new batch job // Trigger starts a new batch job