// WriteLogs handles an http request from the agent to stream build logs from // the agent to the server to enable real time streamings to the client. func WriteLogs(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.String(500, "Invalid input. %s", err) return } conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { c.String(500, "Cannot upgrade to websocket. %s", err) return } defer conn.Close() wc, err := stream.Writer(c, stream.ToKey(id)) if err != nil { c.String(500, "Cannot create stream writer. %s", err) return } defer func() { wc.Close() stream.Delete(c, stream.ToKey(id)) }() var msg []byte for { _, msg, err = conn.ReadMessage() if err != nil { break } wc.Write(msg) wc.Write(newline) } if err != nil && err != io.EOF { c.String(500, "Error reading logs. %s", err) return } // // rc, err := stream.Reader(c, stream.ToKey(id)) // if err != nil { // c.String(500, "Failed to create stream reader. %s", err) // return // } // // wg := sync.WaitGroup{} // wg.Add(1) // // go func() { // defer recover() // store.WriteLog(c, &model.Job{ID: id}, rc) // wg.Done() // }() // // wc.Close() // wg.Wait() }
func GetStream(c *gin.Context) { repo := session.Repo(c) buildn, _ := strconv.Atoi(c.Param("build")) jobn, _ := strconv.Atoi(c.Param("number")) c.Writer.Header().Set("Content-Type", "text/event-stream") build, err := store.GetBuildNumber(c, repo, buildn) if err != nil { log.Debugln("stream cannot get build number.", err) c.AbortWithError(404, err) return } job, err := store.GetJobNumber(c, build, jobn) if err != nil { log.Debugln("stream cannot get job number.", err) c.AbortWithError(404, err) return } rc, err := stream.Reader(c, stream.ToKey(job.ID)) if err != nil { c.AbortWithError(404, err) return } go func() { <-c.Writer.CloseNotify() rc.Close() }() var line int var scanner = bufio.NewScanner(rc) for scanner.Scan() { line++ var err = sse.Encode(c.Writer, sse.Event{ Id: strconv.Itoa(line), Event: "message", Data: scanner.Text(), }) if err != nil { break } c.Writer.Flush() } log.Debugf("Closed stream %s#%d", repo.FullName, build.Number) }
// Pull is a long request that polls and attemts to pull work off the queue stack. func Pull(c *gin.Context) { logrus.Debugf("Agent %s connected.", c.ClientIP()) w := queue.PullClose(c, c.Writer) if w == nil { logrus.Debugf("Agent %s could not pull work.", c.ClientIP()) } else { // setup the channel to stream logs if err := stream.Create(c, stream.ToKey(w.Job.ID)); err != nil { logrus.Errorf("Unable to create stream. %s", err) } c.JSON(202, w) logrus.Debugf("Agent %s assigned work. %s/%s#%d.%d", c.ClientIP(), w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number, ) } }
// Update handles build updates from the agent and persists to the database. func Update(c *gin.Context) { work := &queue.Work{} if err := c.BindJSON(work); err != nil { logrus.Errorf("Invalid input. %s", err) return } // TODO(bradrydzewski) it is really annoying that we have to do this lookup // and I'd prefer not to. The reason we do this is because the Build and Job // have fields that aren't serialized to json and would be reset to their // empty values if we just saved what was coming in the http.Request body. build, err := store.GetBuild(c, work.Build.ID) if err != nil { c.String(404, "Unable to find build. %s", err) return } job, err := store.GetJob(c, work.Job.ID) if err != nil { c.String(404, "Unable to find job. %s", err) return } build.Started = work.Build.Started build.Finished = work.Build.Finished build.Status = work.Build.Status job.Started = work.Job.Started job.Finished = work.Job.Finished job.Status = work.Job.Status job.ExitCode = work.Job.ExitCode if build.Status == model.StatusPending { build.Status = model.StatusRunning store.UpdateBuild(c, build) } if job.Status == model.StatusRunning { err := stream.Create(c, stream.ToKey(job.ID)) if err != nil { logrus.Errorf("Unable to create stream. %s", err) } } ok, err := store.UpdateBuildJob(c, build, job) if err != nil { c.String(500, "Unable to update job. %s", err) return } if ok && build.Status != model.StatusRunning { // get the user because we transfer the user form the server to agent // and back we lose the token which does not get serialized to json. user, err := store.GetUser(c, work.User.ID) if err != nil { c.String(500, "Unable to find user. %s", err) return } remote.Status(c, user, work.Repo, build, fmt.Sprintf("%s/%s/%d", work.System.Link, work.Repo.FullName, work.Build.Number)) } if build.Status == model.StatusRunning { bus.Publish(c, bus.NewEvent(bus.Started, work.Repo, build, job)) } else { bus.Publish(c, bus.NewEvent(bus.Finished, work.Repo, build, job)) } c.JSON(200, work) }