예제 #1
0
파일: queue.go 프로젝트: Ablu/drone
// 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()

}
예제 #2
0
파일: queue.go 프로젝트: Ablu/drone
// Stream streams the logs to disk or memory for broadcasing to listeners. Once
// the stream is closed it is moved to permanent storage in the database.
func Stream(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.String(500, "Invalid input. %s", err)
		return
	}

	key := c.Param("id")
	logrus.Infof("Agent %s creating stream %s.", c.ClientIP(), key)

	wc, err := stream.Writer(c, key)
	if err != nil {
		c.String(500, "Failed to create stream writer. %s", err)
		return
	}

	defer func() {
		wc.Close()
		stream.Delete(c, key)
	}()

	io.Copy(wc, c.Request.Body)

	rc, err := stream.Reader(c, key)
	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()
	c.String(200, "")

	logrus.Debugf("Agent %s wrote stream to database", c.ClientIP())
}