Skip to content

Commit

Permalink
fix: Handling missing log file error in server logs (#838)
Browse files Browse the repository at this point in the history
Signed-off-by: mobley-trent <[email protected]>
  • Loading branch information
mobley-trent authored Jul 26, 2024
1 parent ca1826d commit be58b72
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/daytona_server_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ daytona server logs [flags]
```
--file Read logs from local server log file
-f, --follow Follow logs
-r, --retry Retry connection
```

### Options inherited from parent commands
Expand Down
4 changes: 4 additions & 0 deletions hack/docs/daytona_server_logs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ options:
shorthand: f
default_value: "false"
usage: Follow logs
- name: retry
shorthand: r
default_value: "false"
usage: Retry connection
inherited_options:
- name: help
default_value: "false"
Expand Down
2 changes: 1 addition & 1 deletion internal/util/apiclient/websocket_log_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var workspaceLogsStarted bool

func ReadWorkspaceLogs(activeProfile config.Profile, workspaceId string, projectNames []string, stopLogs *bool) {
var wg sync.WaitGroup
query := "follow=true"
query := "follow=true&retry=true"

logs_view.CalculateLongestPrefixLength(projectNames)

Expand Down
34 changes: 25 additions & 9 deletions internal/util/log_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func ReadLog(ctx context.Context, logReader io.Reader, follow bool, c chan []byt
}
}

func ReadJSONLog(ctx context.Context, logReader io.Reader, follow bool, c chan interface{}, errChan chan error) {
func ReadJSONLog(ctx context.Context, logReader io.Reader, follow bool, retry bool, c chan interface{}, errChan chan error) {
var buffer bytes.Buffer
var err error
reader := bufio.NewReader(logReader)
delimiter := []byte(logs.LogDelimiter)

Expand All @@ -48,16 +49,31 @@ func ReadJSONLog(ctx context.Context, logReader io.Reader, follow bool, c chan i
return
default:
byteChunk := make([]byte, 1024)
n, err := reader.Read(byteChunk)
if err != nil {
if err != io.EOF {
errChan <- err
} else if !follow {
errChan <- io.EOF
return

if retry {
for {
n, err := reader.Read(byteChunk)
if err == nil {
buffer.Write(byteChunk[:n])
break
} else if !follow && err == io.EOF {
errChan <- io.EOF
return
}
}
} else {
n, err := reader.Read(byteChunk)
if err != nil {
if err != io.EOF {
errChan <- err
} else if !follow {
errChan <- io.EOF
return
}
}
buffer.Write(byteChunk[:n])
}
buffer.Write(byteChunk[:n])

data := buffer.Bytes()

index := bytes.Index(data, delimiter)
Expand Down
46 changes: 45 additions & 1 deletion pkg/api/controllers/log/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"io"
"net/http"
"time"

"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/server"
Expand All @@ -15,6 +16,8 @@ import (
log "github.com/sirupsen/logrus"
)

const TIMEOUT = 300 * time.Millisecond

var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
Expand Down Expand Up @@ -97,6 +100,8 @@ func writeJSONToWs(ws *websocket.Conn, c chan interface{}, errChan chan error) {
func readJSONLog(ginCtx *gin.Context, logReader io.Reader) {
followQuery := ginCtx.Query("follow")
follow := followQuery == "true"
retryQuery := ginCtx.Query("retry")
retry := retryQuery == "true"

ws, err := upgrader.Upgrade(ginCtx.Writer, ginCtx.Request, nil)
if err != nil {
Expand All @@ -110,7 +115,7 @@ func readJSONLog(ginCtx *gin.Context, logReader io.Reader) {
ctx, cancel := context.WithCancel(context.Background())

defer cancel()
go util.ReadJSONLog(ctx, logReader, follow, msgChannel, errChannel)
go util.ReadJSONLog(ctx, logReader, follow, retry, msgChannel, errChannel)
go writeJSONToWs(ws, msgChannel, errChannel)

go func() {
Expand Down Expand Up @@ -148,6 +153,19 @@ func readJSONLog(ginCtx *gin.Context, logReader io.Reader) {

func ReadServerLog(ginCtx *gin.Context) {
server := server.GetInstance(nil)
retryQuery := ginCtx.DefaultQuery("retry", "true")
retry := retryQuery == "true"

if retry {
for {
reader, err := server.GetLogReader()
if err == nil {
readLog(ginCtx, reader)
return
}
time.Sleep(TIMEOUT)
}
}

reader, err := server.GetLogReader()
if err != nil {
Expand All @@ -160,9 +178,22 @@ func ReadServerLog(ginCtx *gin.Context) {

func ReadWorkspaceLog(ginCtx *gin.Context) {
workspaceId := ginCtx.Param("workspaceId")
retryQuery := ginCtx.DefaultQuery("retry", "true")
retry := retryQuery == "true"

server := server.GetInstance(nil)

if retry {
for {
wsLogReader, err := server.WorkspaceService.GetWorkspaceLogReader(workspaceId)
if err == nil {
readJSONLog(ginCtx, wsLogReader)
return
}
time.Sleep(TIMEOUT)
}
}

wsLogReader, err := server.WorkspaceService.GetWorkspaceLogReader(workspaceId)
if err != nil {
ginCtx.AbortWithError(http.StatusInternalServerError, err)
Expand All @@ -175,9 +206,22 @@ func ReadWorkspaceLog(ginCtx *gin.Context) {
func ReadProjectLog(ginCtx *gin.Context) {
workspaceId := ginCtx.Param("workspaceId")
projectName := ginCtx.Param("projectName")
retryQuery := ginCtx.DefaultQuery("retry", "true")
retry := retryQuery == "true"

server := server.GetInstance(nil)

if retry {
for {
projectLogReader, err := server.WorkspaceService.GetProjectLogReader(workspaceId, projectName)
if err == nil {
readJSONLog(ginCtx, projectLogReader)
return
}
time.Sleep(TIMEOUT)
}
}

projectLogReader, err := server.WorkspaceService.GetProjectLogReader(workspaceId, projectName)
if err != nil {
ginCtx.AbortWithError(http.StatusInternalServerError, err)
Expand Down
10 changes: 8 additions & 2 deletions pkg/cmd/server/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

var followFlag bool
var retryFlag bool
var fileFlag bool

var logsCmd = &cobra.Command{
Expand All @@ -37,8 +38,12 @@ var logsCmd = &cobra.Command{
}

query := ""
if followFlag {
query = "follow=true"
if retryFlag && followFlag {
query += "follow=true&retry=true"
} else if retryFlag {
query += "retry=true"
} else if followFlag {
query += "follow=true"
}

switch {
Expand Down Expand Up @@ -123,5 +128,6 @@ func readServerLogFile() {

func init() {
logsCmd.Flags().BoolVarP(&followFlag, "follow", "f", false, "Follow logs")
logsCmd.Flags().BoolVarP(&retryFlag, "retry", "r", false, "Retry connection")
logsCmd.Flags().BoolVar(&fileFlag, "file", false, "Read logs from local server log file")
}

0 comments on commit be58b72

Please sign in to comment.