simplelog/relp_handler.go

275 lines
6.5 KiB
Go
Raw Normal View History

2024-05-14 04:43:47 +00:00
package simplelog
import (
"context"
2024-05-14 04:43:47 +00:00
"encoding/json"
"fmt"
"io/ioutil"
"log"
"log/slog"
2024-05-14 04:43:47 +00:00
"net"
"net/url"
"os"
"path/filepath"
2024-05-22 21:52:20 +00:00
"runtime"
aider: Added getEnvAsInt and getEnvAsDuration implementations. # Aider chat conversation: USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them. ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH ======= import ( "os" "strconv" "time" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ``` ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH ======= import ( "os" "strconv" "time" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ``` USER: OpenAIException - Error code: 403 - {'error': {'message': 'Project `proj_eutN6mO0Ycys1G2tSjMQRQmQ` does not have access to model `gpt-3.5-turbo`', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}} Model: gpt-3.5-turbo API Base: https://api.openai.com Messages: [{'role': 'system', 'content': 'You are an expert software engineer.\nReview the provided context an ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH import ( "encoding/json" "fmt" "io/ioutil ^C KeyboardInterrupt USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them. ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH import ( "encoding/json" "fmt" "io/ioutil" "log" "net" "net/url" "os" "path/filepath" "time" "github.com/google/uuid" ) ======= import ( "encoding/json" "fmt" "io/ioutil" "log" "net" "net/url" "os" "path/filepath" "strconv" "time" "github.com/google/uuid" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ```
2024-05-14 11:27:25 +00:00
"strconv"
2024-05-14 04:43:47 +00:00
"time"
"github.com/google/uuid"
)
aider: Added getEnvAsInt and getEnvAsDuration implementations. # Aider chat conversation: USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them. ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH ======= import ( "os" "strconv" "time" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ``` ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH ======= import ( "os" "strconv" "time" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ``` USER: OpenAIException - Error code: 403 - {'error': {'message': 'Project `proj_eutN6mO0Ycys1G2tSjMQRQmQ` does not have access to model `gpt-3.5-turbo`', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}} Model: gpt-3.5-turbo API Base: https://api.openai.com Messages: [{'role': 'system', 'content': 'You are an expert software engineer.\nReview the provided context an ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH import ( "encoding/json" "fmt" "io/ioutil ^C KeyboardInterrupt USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them. ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`: 1. Add the `getEnvAsInt` function. 2. Add the `getEnvAsDuration` function. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH import ( "encoding/json" "fmt" "io/ioutil" "log" "net" "net/url" "os" "path/filepath" "time" "github.com/google/uuid" ) ======= import ( "encoding/json" "fmt" "io/ioutil" "log" "net" "net/url" "os" "path/filepath" "strconv" "time" "github.com/google/uuid" ) func getEnvAsInt(name string, defaultVal int) int { valStr := os.Getenv(name) if val, err := strconv.Atoi(valStr); err == nil { return val } return defaultVal } func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { valStr := os.Getenv(name) if val, err := time.ParseDuration(valStr); err == nil { return val } return defaultVal } >>>>>>> REPLACE ```
2024-05-14 11:27:25 +00:00
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
var (
cacheDir, _ = os.UserCacheDir()
diskBufferLimit = getEnvAsInt("LOGGER_DISK_BUFFER_LIMIT", 100)
diskWriteInterval = getEnvAsDuration("LOGGER_DISK_WRITE_INTERVAL", time.Second)
relpDebug = os.Getenv("RELP_DEBUG") != ""
2024-05-14 04:43:47 +00:00
)
type RELPHandler struct {
relpServerURL string
2024-05-22 21:52:20 +00:00
relpHost string
relpPort string
2024-05-14 04:43:47 +00:00
conn net.Conn
ch chan Event
done chan struct{}
failedCh chan Event
timer *time.Timer
}
2024-05-22 21:52:20 +00:00
func NewRELPHandler(relpURL string) (ExtendedHandler, error) {
2024-05-14 04:43:47 +00:00
parsedURL, err := url.Parse(relpURL)
if err != nil {
2024-05-22 21:52:20 +00:00
return nil, fmt.Errorf("error parsing RELP URL: %v", err)
2024-05-14 04:43:47 +00:00
}
if parsedURL.Scheme != "tcp" {
2024-05-22 21:52:20 +00:00
return nil, fmt.Errorf("the RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
}
host, port, err := net.SplitHostPort(parsedURL.Host)
if err != nil {
return nil, fmt.Errorf("Error splitting host and port: %v", err)
2024-05-14 04:43:47 +00:00
}
2024-05-22 21:52:20 +00:00
if err := os.MkdirAll(filepath.Join(cacheDir, "simplelog"), 0755); err != nil {
2024-05-22 21:52:20 +00:00
return nil, fmt.Errorf("failed to create cache directory: %v", err)
2024-05-14 04:43:47 +00:00
}
aider: Added additional logging for successful connection and handler instantiation in the relp handler. # Aider chat conversation: USER: the code is not connecting to the relp server when the script in ./tools is run, and it sets RELP_DEBUG=1 and no connection errors are printed. please diagnose the connection issue, and also add additional logging in the relp handler on successful connection and handler instantiation when RELP_DEBUG is set ASSISTANT: To diagnose the connection issue and add additional logging, we need to modify `relp_handler.go`: 1. Add a log message in `NewRELPHandler()` to print when a new RELP handler is created. 2. Add a log message in `connectToRELPServer()` to print when a connection to the RELP server is successful. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH return &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), }, nil ======= r := &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), } if relpDebug { log.Printf("Created new RELP handler for server at %s", r.relpServerURL) } return r, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ```
2024-05-14 11:38:03 +00:00
r := &RELPHandler{
2024-05-22 21:52:20 +00:00
ch: make(chan Event, diskBufferLimit),
done: make(chan struct{}),
failedCh: make(chan Event, diskBufferLimit),
timer: time.NewTimer(diskWriteInterval),
relpHost: host,
relpPort: port,
aider: Added additional logging for successful connection and handler instantiation in the relp handler. # Aider chat conversation: USER: the code is not connecting to the relp server when the script in ./tools is run, and it sets RELP_DEBUG=1 and no connection errors are printed. please diagnose the connection issue, and also add additional logging in the relp handler on successful connection and handler instantiation when RELP_DEBUG is set ASSISTANT: To diagnose the connection issue and add additional logging, we need to modify `relp_handler.go`: 1. Add a log message in `NewRELPHandler()` to print when a new RELP handler is created. 2. Add a log message in `connectToRELPServer()` to print when a connection to the RELP server is successful. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH return &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), }, nil ======= r := &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), } if relpDebug { log.Printf("Created new RELP handler for server at %s", r.relpServerURL) } return r, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ```
2024-05-14 11:38:03 +00:00
}
if relpDebug {
log.Printf("Created new RELP handler for server at %s", r.relpServerURL)
}
2024-05-22 21:52:20 +00:00
err = r.Startup()
if err != nil {
return nil, err
}
aider: Added additional logging for successful connection and handler instantiation in the relp handler. # Aider chat conversation: USER: the code is not connecting to the relp server when the script in ./tools is run, and it sets RELP_DEBUG=1 and no connection errors are printed. please diagnose the connection issue, and also add additional logging in the relp handler on successful connection and handler instantiation when RELP_DEBUG is set ASSISTANT: To diagnose the connection issue and add additional logging, we need to modify `relp_handler.go`: 1. Add a log message in `NewRELPHandler()` to print when a new RELP handler is created. 2. Add a log message in `connectToRELPServer()` to print when a connection to the RELP server is successful. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH return &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), }, nil ======= r := &RELPHandler{ relpServerURL: parsedURL.Host, ch: make(chan Event, diskBufferLimit), done: make(chan struct{}), failedCh: make(chan Event, diskBufferLimit), timer: time.NewTimer(diskWriteInterval), } if relpDebug { log.Printf("Created new RELP handler for server at %s", r.relpServerURL) } return r, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ```
2024-05-14 11:38:03 +00:00
return r, nil
2024-05-14 04:43:47 +00:00
}
func (r *RELPHandler) connectToRELPServer() (net.Conn, error) {
2024-05-22 21:52:20 +00:00
conn, err := net.Dial("tcp", net.JoinHostPort(r.relpHost, r.relpPort))
if err != nil {
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
if relpDebug {
2024-05-22 21:52:20 +00:00
log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(r.relpHost, r.relpPort), err)
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
}
return nil, err
}
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
if relpDebug {
2024-05-22 21:52:20 +00:00
log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(r.relpHost, r.relpPort))
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
}
return conn, nil
}
2024-05-14 04:43:47 +00:00
func (r *RELPHandler) Startup() error {
var err error
r.conn, err = r.connectToRELPServer()
2024-05-14 04:43:47 +00:00
if err != nil {
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
if relpDebug {
log.Printf("Failed to establish TCP connection to RELP server: %v", err)
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
}
return fmt.Errorf("Failed to establish TCP connection to RELP server: %v", err)
2024-05-14 04:43:47 +00:00
}
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
if relpDebug {
log.Printf("Successfully connected to RELP server at %s", r.relpServerURL)
}
2024-05-14 04:43:47 +00:00
go r.receiveEventsFromChannel()
go r.processFailedEvents()
go r.watchDirectoryForFailedEventFiles()
return nil
}
func (r *RELPHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (r *RELPHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return r
}
func (r *RELPHandler) WithGroup(name string) slog.Handler {
return r
}
func (r *RELPHandler) Handle(ctx context.Context, record slog.Record) error {
attrs := make(map[string]interface{})
record.Attrs(func(attr slog.Attr) bool {
attrs[attr.Key] = attr.Value
return true
})
jsonData, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("error marshaling attributes: %v", err)
}
2024-05-22 21:52:20 +00:00
// Get the caller information
_, file, line, ok := runtime.Caller(5)
if !ok {
file = "???"
line = 0
2024-05-14 04:43:47 +00:00
}
2024-05-22 21:52:20 +00:00
event := NewExtendedEvent(record.Level.String(), record.Message, jsonData, file, line)
for _, handler := range cl.handlers {
if err := handler.Handle(ctx, event); err != nil {
return err
}
}
return nil
2024-05-14 04:43:47 +00:00
}
func (r *RELPHandler) receiveEventsFromChannel() {
for {
select {
case event := <-r.ch:
if err := r.sendEventToRELPServer(event); err != nil {
r.failedCh <- event
}
case <-r.done:
return
}
}
}
func (r *RELPHandler) processFailedEvents() {
for {
select {
case event := <-r.failedCh:
if err := r.sendEventToRELPServer(event); err != nil {
// If still failing, write to disk
r.writeFailedToDisk(event)
}
case <-r.timer.C:
// Flush all events in failedCh to disk
for len(r.failedCh) > 0 {
event := <-r.failedCh
r.writeFailedToDisk(event)
}
case <-r.done:
return
}
}
}
func (r *RELPHandler) sendEventToRELPServer(event Event) error {
jsonData, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("error marshaling event: %v", err)
}
_, err = r.conn.Write(jsonData)
2024-05-14 04:43:47 +00:00
if err != nil {
return err
}
// Implement TCP read for the acknowledgment with a timeout
r.conn.SetReadDeadline(time.Now().Add(2 * time.Second))
ack := make([]byte, 256)
n, err := r.conn.Read(ack)
if err != nil {
2024-05-14 04:43:47 +00:00
return err
}
if string(ack[:n]) != "ACK" {
return fmt.Errorf("expected ACK from server, got %s", string(ack[:n]))
}
if relpDebug {
log.Printf("Received ACK from RELP server for event %s", event.ID)
}
2024-05-14 04:43:47 +00:00
return nil
}
func (r *RELPHandler) watchDirectoryForFailedEventFiles() {
for {
select {
case <-r.done:
return
default:
if r.isConnected() {
aider: Added logging for connection success, failure, and reconnection when relpDebug is set. # Aider chat conversation: USER: continuing with the relpDebug theme, make it log messages on connection success, connection failure, and reconnection if relpDebug is set ASSISTANT: To make this change we need to modify `relp_handler.go` to: 1. Add logging for successful connection in the `Startup` method. 2. Add logging for failed connection in the `connectToRELPServer` method. 3. Add logging for reconnection in the `watchDirectoryForFailedEventFiles` method. Here are the *SEARCH/REPLACE* blocks: relp_handler.go ```go <<<<<<< SEARCH r.conn, err = r.connectToRELPServer() if err != nil { return fmt.Errorf("Failed to establish TCP connection: %v", err) } ======= r.conn, err = r.connectToRELPServer() if err != nil { if relpDebug { log.Printf("Failed to establish TCP connection: %v", err) } return fmt.Errorf("Failed to establish TCP connection: %v", err) } if relpDebug { log.Printf("Successfully connected to RELP server at %s", r.relpServerURL) } >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { return nil, err } return conn, nil ======= conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { if relpDebug { log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err) } return nil, err } if relpDebug { log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port)) } return conn, nil >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if r.isConnected() { files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } ======= if r.isConnected() { if relpDebug { log.Printf("Reconnected to RELP server at %s", r.relpServerURL) } files, err := ioutil.ReadDir(cacheDir) if err != nil { log.Printf("Error reading cache directory: %v", err) continue } for _, file := range files { filePath := filepath.Join(cacheDir, file.Name()) data, err := ioutil.ReadFile(filePath) if err != nil { log.Printf("Error reading file %s: %v", file.Name(), err) continue } var event Event if err := json.Unmarshal(data, &event); err != nil { log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err) continue } r.ch <- event os.Remove(filePath) // Remove file after processing } } >>>>>>> REPLACE ```
2024-05-14 11:33:30 +00:00
if relpDebug {
log.Printf("Reconnected to RELP server at %s", r.relpServerURL)
}
2024-05-14 04:43:47 +00:00
files, err := ioutil.ReadDir(cacheDir)
if err != nil {
log.Printf("Error reading cache directory: %v", err)
continue
}
for _, file := range files {
filePath := filepath.Join(cacheDir, file.Name())
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Printf("Error reading event cache file %s: %v", file.Name(), err)
2024-05-14 04:43:47 +00:00
continue
}
var event Event
if err := json.Unmarshal(data, &event); err != nil {
log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err)
continue
}
r.ch <- event
os.Remove(filePath) // Remove file after processing
}
}
time.Sleep(10 * time.Second) // Check disk every 10 seconds
}
}
}
func (r *RELPHandler) isConnected() bool {
_, err := r.conn.Write([]byte{})
return err == nil
}
func (r *RELPHandler) Shutdown() error {
close(r.done)
if r.conn != nil {
if err := r.conn.Close(); err != nil {
return fmt.Errorf("error closing TCP connection: %v", err)
}
}
return nil
2024-05-14 04:43:47 +00:00
}
func (r *RELPHandler) writeFailedToDisk(event Event) {
fileName := filepath.Join(cacheDir, "simplelog", uuid.New().String()+".logevent")
2024-05-14 04:43:47 +00:00
data, _ := json.Marshal(event)
ioutil.WriteFile(fileName, data, 0600)
2024-05-14 04:43:47 +00:00
}