mirror of https://github.com/jetkvm/kvm.git
feat: add process ID to logger
This commit is contained in:
parent
61dcd77313
commit
6dee8a3e24
|
|
@ -1,6 +1,7 @@
|
||||||
package native
|
package native
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -40,8 +41,9 @@ type NativeOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNative(opts NativeOptions) *Native {
|
func NewNative(opts NativeOptions) *Native {
|
||||||
nativeSubLogger := nativeLogger.With().Str("scope", "native").Logger()
|
pid := os.Getpid()
|
||||||
displaySubLogger := displayLogger.With().Str("scope", "native").Logger()
|
nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger()
|
||||||
|
displaySubLogger := displayLogger.With().Int("pid", pid).Str("scope", "native").Logger()
|
||||||
|
|
||||||
onVideoStateChange := opts.OnVideoStateChange
|
onVideoStateChange := opts.OnVideoStateChange
|
||||||
if onVideoStateChange == nil {
|
if onVideoStateChange == nil {
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,7 @@ func (n *NativeOptions) toProxyOptions() *nativeProxyOptions {
|
||||||
// random 16 bytes hex string
|
// random 16 bytes hex string
|
||||||
handshakeMessage := randomId(16)
|
handshakeMessage := randomId(16)
|
||||||
maxRestartAttempts := defaultMaxRestartAttempts
|
maxRestartAttempts := defaultMaxRestartAttempts
|
||||||
// though it's unlikely to be less than 0 as it's uint, we'll add it just in case
|
if n.MaxRestartAttempts > 0 {
|
||||||
// until we have a proper unit test for all things :-(
|
|
||||||
if n.MaxRestartAttempts <= 0 {
|
|
||||||
maxRestartAttempts = n.MaxRestartAttempts
|
maxRestartAttempts = n.MaxRestartAttempts
|
||||||
}
|
}
|
||||||
return &nativeProxyOptions{
|
return &nativeProxyOptions{
|
||||||
|
|
@ -396,7 +394,7 @@ func (p *NativeProxy) monitorProcess() {
|
||||||
}
|
}
|
||||||
p.restartMu.Unlock()
|
p.restartMu.Unlock()
|
||||||
|
|
||||||
p.logger.Warn().Err(err).Msg("native process exited, restarting...")
|
p.logger.Warn().Err(err).Msg("native process exited, restarting ...")
|
||||||
|
|
||||||
// Wait a bit before restarting
|
// Wait a bit before restarting
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
@ -416,6 +414,14 @@ func (p *NativeProxy) restartProcess() error {
|
||||||
p.restartMu.Lock()
|
p.restartMu.Lock()
|
||||||
defer p.restartMu.Unlock()
|
defer p.restartMu.Unlock()
|
||||||
|
|
||||||
|
p.restarts++
|
||||||
|
logger := p.logger.With().Uint("attempt", p.restarts).Uint("maxAttempts", p.options.MaxRestartAttempts).Logger()
|
||||||
|
|
||||||
|
if p.restarts >= p.options.MaxRestartAttempts {
|
||||||
|
logger.Fatal().Msg("max restart attempts reached, exiting")
|
||||||
|
return fmt.Errorf("max restart attempts reached")
|
||||||
|
}
|
||||||
|
|
||||||
if p.stopped {
|
if p.stopped {
|
||||||
return fmt.Errorf("proxy is stopped")
|
return fmt.Errorf("proxy is stopped")
|
||||||
}
|
}
|
||||||
|
|
@ -424,19 +430,14 @@ func (p *NativeProxy) restartProcess() error {
|
||||||
p.clientMu.Lock()
|
p.clientMu.Lock()
|
||||||
if p.client != nil {
|
if p.client != nil {
|
||||||
if err := p.client.Close(); err != nil {
|
if err := p.client.Close(); err != nil {
|
||||||
p.logger.Warn().Err(err).Msg("failed to close gRPC client")
|
logger.Warn().Err(err).Msg("failed to close gRPC client")
|
||||||
}
|
}
|
||||||
p.client = nil // set to nil to avoid closing it again
|
p.client = nil // set to nil to avoid closing it again
|
||||||
}
|
}
|
||||||
p.clientMu.Unlock()
|
p.clientMu.Unlock()
|
||||||
|
logger.Info().Msg("gRPC client closed")
|
||||||
|
|
||||||
p.restarts++
|
logger.Info().Msg("attempting to restart native process")
|
||||||
if p.restarts >= p.options.MaxRestartAttempts {
|
|
||||||
p.logger.Fatal().Msg("max restart attempts reached, exiting")
|
|
||||||
return fmt.Errorf("max restart attempts reached")
|
|
||||||
}
|
|
||||||
|
|
||||||
logger := p.logger.With().Uint("attempt", p.restarts).Uint("maxAttempts", p.options.MaxRestartAttempts).Logger()
|
|
||||||
if err := p.start(); err != nil {
|
if err := p.start(); err != nil {
|
||||||
logger.Error().Err(err).Msg("failed to start native process")
|
logger.Error().Err(err).Msg("failed to start native process")
|
||||||
return fmt.Errorf("failed to start native process: %w", err)
|
return fmt.Errorf("failed to start native process: %w", err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue