bug: fix picoToken is empty when gateway started by launcher (#2241)
This commit is contained in:
parent
f327859cce
commit
a9c76eca21
4 changed files with 21 additions and 29 deletions
|
|
@ -149,6 +149,7 @@ func Run(debug bool, homePath, configPath string, allowEmptyStartup bool) error
|
||||||
// Enforce singleton: write PID file with generated token.
|
// Enforce singleton: write PID file with generated token.
|
||||||
pidData, err := pid.WritePidFile(homePath, cfg.Gateway.Host, cfg.Gateway.Port)
|
pidData, err := pid.WritePidFile(homePath, cfg.Gateway.Host, cfg.Gateway.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Warnf("write pid file failed: %v", err)
|
||||||
return fmt.Errorf("singleton check failed: %w", err)
|
return fmt.Errorf("singleton check failed: %w", err)
|
||||||
}
|
}
|
||||||
defer pid.RemovePidFile(homePath)
|
defer pid.RemovePidFile(homePath)
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,7 @@ func WritePidFile(homePath, host string, port int) (*PidFileData, error) {
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
return nil, fmt.Errorf("failed to rename pid file: %w", err)
|
return nil, fmt.Errorf("failed to rename pid file: %w", err)
|
||||||
}
|
}
|
||||||
|
logger.Debugf("wrote pid file: %s success", pidPath)
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
@ -108,10 +109,12 @@ func ReadPidFileWithCheck(homePath string) *PidFileData {
|
||||||
pidPath := pidFilePath(homePath)
|
pidPath := pidFilePath(homePath)
|
||||||
data, err := readPidFileUnlocked(pidPath)
|
data, err := readPidFileUnlocked(pidPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Debugf("failed to read pid file: %s", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isProcessRunning(data.PID) {
|
if !isProcessRunning(data.PID) {
|
||||||
|
logger.Debugf("process not running, remove pid file: %s", pidPath)
|
||||||
os.Remove(pidPath)
|
os.Remove(pidPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -628,6 +628,7 @@ func (h *Handler) startGatewayLocked(initialStatus string, existingPid int) (int
|
||||||
gateway.mu.Lock()
|
gateway.mu.Lock()
|
||||||
if gateway.cmd == cmd {
|
if gateway.cmd == cmd {
|
||||||
gateway.pidData = pd
|
gateway.pidData = pd
|
||||||
|
gateway.picoToken = cfg.Channels.Pico.Token.String()
|
||||||
setGatewayRuntimeStatusLocked("running")
|
setGatewayRuntimeStatusLocked("running")
|
||||||
}
|
}
|
||||||
gateway.mu.Unlock()
|
gateway.mu.Unlock()
|
||||||
|
|
@ -922,34 +923,13 @@ func (h *Handler) gatewayStatusData() map[string]any {
|
||||||
data["pid"] = pidData.PID
|
data["pid"] = pidData.PID
|
||||||
gateway.mu.Unlock()
|
gateway.mu.Unlock()
|
||||||
} else {
|
} else {
|
||||||
// Fallback: probe health endpoint to get pid and status
|
// Intentionally skip health probe here; the startup goroutine
|
||||||
_, statusCode, err := h.getGatewayHealth(cfg, 2*time.Second)
|
// (startGatewayLocked) already handles liveness detection via
|
||||||
if err != nil {
|
// pidFile polling and health fallback.
|
||||||
gateway.mu.Lock()
|
gateway.mu.Lock()
|
||||||
data["gateway_status"] = gatewayStatusWithoutHealthLocked()
|
data["gateway_status"] = gatewayStatusWithoutHealthLocked()
|
||||||
gateway.pidData = nil
|
gateway.pidData = nil
|
||||||
gateway.mu.Unlock()
|
gateway.mu.Unlock()
|
||||||
logger.ErrorC("gateway", fmt.Sprintf("Gateway health check failed: %v", err))
|
|
||||||
} else {
|
|
||||||
logger.InfoC("gateway", fmt.Sprintf("Gateway health status: %d", statusCode))
|
|
||||||
if statusCode != http.StatusOK {
|
|
||||||
gateway.mu.Lock()
|
|
||||||
setGatewayRuntimeStatusLocked("error")
|
|
||||||
gateway.pidData = nil
|
|
||||||
gateway.mu.Unlock()
|
|
||||||
data["gateway_status"] = "error"
|
|
||||||
data["status_code"] = statusCode
|
|
||||||
} else {
|
|
||||||
gateway.mu.Lock()
|
|
||||||
setGatewayRuntimeStatusLocked("running")
|
|
||||||
bootDefaultModel := gateway.bootDefaultModel
|
|
||||||
if bootDefaultModel != "" {
|
|
||||||
data["boot_default_model"] = bootDefaultModel
|
|
||||||
}
|
|
||||||
data["gateway_status"] = "running"
|
|
||||||
gateway.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gatewayStatus, _ := data["gateway_status"].(string)
|
gatewayStatus, _ := data["gateway_status"].(string)
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/auth"
|
"github.com/sipeed/picoclaw/pkg/auth"
|
||||||
"github.com/sipeed/picoclaw/pkg/config"
|
"github.com/sipeed/picoclaw/pkg/config"
|
||||||
|
ppid "github.com/sipeed/picoclaw/pkg/pid"
|
||||||
"github.com/sipeed/picoclaw/web/backend/utils"
|
"github.com/sipeed/picoclaw/web/backend/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -444,7 +447,7 @@ func TestGatewayStatusKeepsRunningWhenHealthProbeFailsAfterRunning(t *testing.T)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGatewayStatusReportsRunningFromHealthProbe(t *testing.T) {
|
func TestGatewayStatusReportsRunningFromPidProbe(t *testing.T) {
|
||||||
resetGatewayTestState(t)
|
resetGatewayTestState(t)
|
||||||
|
|
||||||
configPath := filepath.Join(t.TempDir(), "config.json")
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
||||||
|
|
@ -468,6 +471,9 @@ func TestGatewayStatusReportsRunningFromHealthProbe(t *testing.T) {
|
||||||
return mockGatewayHealthResponse(http.StatusOK, cmd.Process.Pid), nil
|
return mockGatewayHealthResponse(http.StatusOK, cmd.Process.Pid), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err := ppid.WritePidFile(globalConfigDir(), "localhost", 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/gateway/status", nil)
|
req := httptest.NewRequest(http.MethodGet, "/api/gateway/status", nil)
|
||||||
mux.ServeHTTP(rec, req)
|
mux.ServeHTTP(rec, req)
|
||||||
|
|
@ -513,6 +519,8 @@ func TestGatewayStatusRequiresRestartAfterDefaultModelChange(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("FindProcess() error = %v", err)
|
t.Fatalf("FindProcess() error = %v", err)
|
||||||
}
|
}
|
||||||
|
_, err = ppid.WritePidFile(globalConfigDir(), "localhost", 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
bootSignature := computeConfigSignature(cfg)
|
bootSignature := computeConfigSignature(cfg)
|
||||||
gateway.mu.Lock()
|
gateway.mu.Lock()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue