Mercurial > gemma
changeset 979:7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sends back the id of the job.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 19 Oct 2018 12:14:53 +0200 |
parents | 544a5cfe07cd |
children | 0906371c54c4 |
files | pkg/controllers/imports.go pkg/controllers/json.go pkg/controllers/token.go pkg/imports/queue.go pkg/imports/sr.go |
diffstat | 5 files changed, 35 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/imports.go Thu Oct 18 18:37:19 2018 +0200 +++ b/pkg/controllers/imports.go Fri Oct 19 12:14:53 2018 +0200 @@ -9,7 +9,9 @@ "os" "path/filepath" + "gemma.intevation.de/gemma/pkg/auth" "gemma.intevation.de/gemma/pkg/config" + "gemma.intevation.de/gemma/pkg/imports" ) const ( @@ -62,7 +64,20 @@ return } - // TODO: Enqueue job. + session, _ := auth.GetSession(req) + + sr := &imports.SoundingResult{ + Who: session.User, + Dir: dir, + } - _ = dir + jobID := imports.AddJob(sr) + log.Printf("Added job %d\n", jobID) + + result := struct { + ID int64 `json:"id"` + }{ + ID: jobID, + } + SendJSON(rw, http.StatusCreated, &result) }
--- a/pkg/controllers/json.go Thu Oct 18 18:37:19 2018 +0200 +++ b/pkg/controllers/json.go Fri Oct 19 12:14:53 2018 +0200 @@ -121,3 +121,11 @@ } } } + +func SendJSON(rw http.ResponseWriter, code int, data interface{}) { + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(code) + if err := json.NewEncoder(rw).Encode(data); err != nil { + log.Printf("error: %v\n", err) + } +}
--- a/pkg/controllers/token.go Thu Oct 18 18:37:19 2018 +0200 +++ b/pkg/controllers/token.go Fri Oct 19 12:14:53 2018 +0200 @@ -10,13 +10,6 @@ "gemma.intevation.de/gemma/pkg/models" ) -func sendJSON(rw http.ResponseWriter, data interface{}) { - rw.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(rw).Encode(data); err != nil { - log.Printf("error: %v\n", err) - } -} - func renew(rw http.ResponseWriter, req *http.Request) { token, _ := auth.GetToken(req) newToken, err := auth.Sessions.Renew(token) @@ -44,7 +37,7 @@ Roles: session.Roles, } - sendJSON(rw, &result) + SendJSON(rw, http.StatusOK, &result) } func logout(rw http.ResponseWriter, req *http.Request) { @@ -96,5 +89,5 @@ Roles: session.Roles, } - sendJSON(rw, &result) + SendJSON(rw, http.StatusCreated, &result) }
--- a/pkg/imports/queue.go Thu Oct 18 18:37:19 2018 +0200 +++ b/pkg/imports/queue.go Fri Oct 19 12:14:53 2018 +0200 @@ -12,7 +12,7 @@ ) type Job interface { - Who() string + User() string Do(conn *sql.Conn) error CleanUp() error } @@ -47,7 +47,7 @@ job = queue.Remove(queue.Front()).(Job) queueCond.L.Unlock() - if err := auth.RunAs(job.Who(), context.Background(), job.Do); err != nil { + if err := auth.RunAs(job.User(), context.Background(), job.Do); err != nil { log.Printf("import error: %v\n", err) } if err := job.CleanUp(); err != nil {
--- a/pkg/imports/sr.go Thu Oct 18 18:37:19 2018 +0200 +++ b/pkg/imports/sr.go Fri Oct 19 12:14:53 2018 +0200 @@ -25,8 +25,8 @@ ) type SoundingResult struct { - who string - dir string + Who string + Dir string } const SoundingResultDateFormat = "2006-01-02" @@ -128,12 +128,12 @@ return err } -func (sr *SoundingResult) Who() string { - return sr.who +func (sr *SoundingResult) User() string { + return sr.Who } func (sr *SoundingResult) CleanUp() error { - return os.RemoveAll(sr.dir) + return os.RemoveAll(sr.Dir) } func find(needle string, haystack []*zip.File) *zip.File { @@ -276,7 +276,7 @@ func (sr *SoundingResult) Do(conn *sql.Conn) error { - z, err := zip.OpenReader(filepath.Join(sr.dir, "sr.zip")) + z, err := zip.OpenReader(filepath.Join(sr.Dir, "sr.zip")) if err != nil { return err }