|
7 | 7 | "os"
|
8 | 8 | "time"
|
9 | 9 |
|
| 10 | + "github.com/docker/docker/pkg/stdcopy" |
10 | 11 | _claims "github.com/factorysh/microdensity/claims"
|
11 | 12 | "github.com/factorysh/microdensity/task"
|
12 | 13 | "github.com/go-chi/chi/v5"
|
@@ -103,12 +104,12 @@ func (a *Application) addTask(t *task.Task, args map[string]string) error {
|
103 | 104 | return err
|
104 | 105 | }
|
105 | 106 |
|
106 |
| - err = a.storage.Upsert(t) |
| 107 | + err = a.queue.Put(t, args) |
107 | 108 | if err != nil {
|
108 | 109 | return err
|
109 | 110 | }
|
110 | 111 |
|
111 |
| - err = a.queue.Put(t, args) |
| 112 | + err = a.storage.Upsert(t) |
112 | 113 | if err != nil {
|
113 | 114 | return err
|
114 | 115 | }
|
@@ -180,3 +181,52 @@ func (a *Application) TaskIDHandler(w http.ResponseWriter, r *http.Request) {
|
180 | 181 | return
|
181 | 182 | }
|
182 | 183 | }
|
| 184 | + |
| 185 | +// TaskLogsHandler get a logs for a task |
| 186 | +func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *http.Request) { |
| 187 | + |
| 188 | + return func(w http.ResponseWriter, r *http.Request) { |
| 189 | + l := a.logger.With( |
| 190 | + zap.String("url", r.URL.String()), |
| 191 | + zap.String("service", chi.URLParam(r, "serviceID")), |
| 192 | + zap.String("project", chi.URLParam(r, "project")), |
| 193 | + zap.String("branch", chi.URLParam(r, "branch")), |
| 194 | + zap.String("commit", chi.URLParam(r, "commit")), |
| 195 | + ) |
| 196 | + |
| 197 | + t, err := a.storage.GetByCommit( |
| 198 | + chi.URLParam(r, "serviceID"), |
| 199 | + chi.URLParam(r, "project"), |
| 200 | + chi.URLParam(r, "branch"), |
| 201 | + chi.URLParam(r, "commit"), |
| 202 | + latest, |
| 203 | + ) |
| 204 | + |
| 205 | + if err != nil { |
| 206 | + l.Warn("Task get error", zap.Error(err)) |
| 207 | + w.WriteHeader(http.StatusNotFound) |
| 208 | + w.Write([]byte(http.StatusText(http.StatusNotFound))) |
| 209 | + return |
| 210 | + } |
| 211 | + |
| 212 | + reader, err := t.Logs(r.Context(), false) |
| 213 | + if err != nil { |
| 214 | + l.Warn("Task log error", zap.Error(err)) |
| 215 | + w.WriteHeader(http.StatusInternalServerError) |
| 216 | + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + // just stdout for now |
| 221 | + // kudos @ndeloof, @rumpl, @glours |
| 222 | + _, err = stdcopy.StdCopy(w, nil, reader) |
| 223 | + if err != nil { |
| 224 | + l.Error("Task log stdcopy write error", zap.Error(err)) |
| 225 | + w.WriteHeader(http.StatusInternalServerError) |
| 226 | + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) |
| 227 | + } |
| 228 | + |
| 229 | + w.Header().Set("Content-Type", "text/plain") |
| 230 | + } |
| 231 | + |
| 232 | +} |
0 commit comments