-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
319 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package api | ||
|
||
type LogMessage struct { | ||
Message string `json:"message"` | ||
ProcessName string `json:"process_name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/f1bonacc1/process-compose/src/app" | ||
"github.com/f1bonacc1/process-compose/src/pclog" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
"github.com/rs/zerolog/log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
var upgrader = websocket.Upgrader{} | ||
|
||
func HandleLogsStream(c *gin.Context) { | ||
procName := c.Param("name") | ||
follow := c.Param("follow") == "true" | ||
endOffset, err := strconv.Atoi(c.Param("endOffset")) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
done := make(chan struct{}) | ||
logChan := make(chan LogMessage, 256) | ||
connector := pclog.NewConnector(func(messages []string) { | ||
for _, message := range messages { | ||
msg := LogMessage{ | ||
Message: message, | ||
ProcessName: procName, | ||
} | ||
logChan <- msg | ||
} | ||
if !follow { | ||
close(logChan) | ||
} | ||
}, | ||
func(message string) { | ||
msg := LogMessage{ | ||
Message: message, | ||
ProcessName: procName, | ||
} | ||
logChan <- msg | ||
}, | ||
endOffset) | ||
go handleLog(ws, procName, connector, logChan, done) | ||
if follow { | ||
go handleIncoming(ws, done) | ||
} | ||
app.PROJ.GetLogsAndSubscribe(procName, connector) | ||
} | ||
|
||
func handleLog(ws *websocket.Conn, procName string, connector *pclog.Connector, logChan chan LogMessage, done chan struct{}) { | ||
defer app.PROJ.UnSubscribeLogger(procName, connector) | ||
defer ws.Close() | ||
for { | ||
select { | ||
case msg, open := <-logChan: | ||
if err := ws.WriteJSON(&msg); err != nil { | ||
log.Err(err).Msg("Failed to write to socket") | ||
return | ||
} | ||
if !open { | ||
return | ||
} | ||
case <-done: | ||
log.Warn().Msg("Socket closed remotely") | ||
return | ||
} | ||
|
||
} | ||
} | ||
|
||
func handleIncoming(ws *websocket.Conn, done chan struct{}) { | ||
defer close(done) | ||
for { | ||
msgType, _, err := ws.ReadMessage() | ||
if err != nil { | ||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) { | ||
return | ||
} | ||
log.Err(err).Msgf("Failed to read from socket %d", msgType) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"github.com/f1bonacc1/process-compose/src/api" | ||
"github.com/gorilla/websocket" | ||
"github.com/rs/zerolog/log" | ||
"os" | ||
"os/signal" | ||
"time" | ||
) | ||
|
||
func ReadProcessLogs(address string, port int, name string, offset int, follow bool) error { | ||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt) | ||
url := fmt.Sprintf("ws://%s:%d/process/logs/ws/%s/%d/%v", address, port, name, offset, follow) | ||
log.Info().Msgf("Connecting to %s", url) | ||
ws, _, err := websocket.DefaultDialer.Dial(url, nil) | ||
if err != nil { | ||
log.Error().Msgf("failed to dial to %s error: %v", url, err) | ||
return err | ||
} | ||
defer ws.Close() | ||
done := make(chan struct{}) | ||
|
||
go readLogs(done, ws, follow) | ||
|
||
for { | ||
select { | ||
case <-done: | ||
return nil | ||
case <-interrupt: | ||
fmt.Println("interrupt") | ||
|
||
// Cleanly close the connection by sending a close message and then | ||
// waiting (with timeout) for the server to close the connection. | ||
err := ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) | ||
if err != nil { | ||
fmt.Println("write close:", err) | ||
return nil | ||
} | ||
select { | ||
case <-done: | ||
case <-time.After(time.Second): | ||
} | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func readLogs(done chan struct{}, ws *websocket.Conn, follow bool) { | ||
defer close(done) | ||
for { | ||
var message api.LogMessage | ||
if err := ws.ReadJSON(&message); err != nil { | ||
if !follow && websocket.IsCloseError(err, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) { | ||
return | ||
} | ||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) { | ||
return | ||
} | ||
log.Error().Msgf("failed to read message: %v", err) | ||
return | ||
} | ||
if len(message.ProcessName) > 0 { | ||
fmt.Printf("%s:\t%s\n", message.ProcessName, message.Message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/f1bonacc1/process-compose/src/client" | ||
"github.com/rs/zerolog/log" | ||
"math" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
follow bool | ||
tailLength int | ||
) | ||
|
||
// logsCmd represents the logs command | ||
var logsCmd = &cobra.Command{ | ||
Use: "logs [PROCESS]", | ||
Short: "Fetch the logs of a process", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
name := args[0] | ||
err := client.ReadProcessLogs(pcAddress, port, name, tailLength, follow) | ||
if err != nil { | ||
log.Error().Msgf("Failed to fetch logs for process %s: %v", name, err) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
processCmd.AddCommand(logsCmd) | ||
|
||
logsCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow log output") | ||
logsCmd.Flags().IntVarP(&tailLength, "tail", "n", math.MaxInt, "Number of lines to show from the end of the logs (default - all)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pclog | ||
|
||
type multiLineHandler func(string []string) | ||
type lineHandler func(string string) | ||
|
||
type Connector struct { | ||
LogObserver | ||
logLinesHandler multiLineHandler | ||
logMessageHandler lineHandler | ||
uniqueId string | ||
taiLength int | ||
} | ||
|
||
func NewConnector(mlHandler multiLineHandler, slHandler lineHandler, tail int) *Connector { | ||
return &Connector{ | ||
logLinesHandler: mlHandler, | ||
logMessageHandler: slHandler, | ||
uniqueId: GenerateUniqueID(10), | ||
taiLength: tail, | ||
} | ||
} | ||
|
||
func (c *Connector) AddLine(line string) { | ||
c.logMessageHandler(line) | ||
} | ||
func (c *Connector) SetLines(lines []string) { | ||
c.logLinesHandler(lines) | ||
} | ||
func (c *Connector) GetUniqueID() string { | ||
return c.uniqueId | ||
} | ||
|
||
func (c *Connector) GetTailLength() int { | ||
return c.taiLength | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package pclog | ||
|
||
type PcLogObserver interface { | ||
type LogObserver interface { | ||
AddLine(line string) | ||
SetLines(lines []string) | ||
GetTailLength() int | ||
GetUniqueID() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.