Fixed the format

This commit is contained in:
mrigangha 2026-05-07 12:37:38 +00:00
parent 326db4ec7c
commit 0edefaa4c1
2 changed files with 90 additions and 89 deletions

View file

@ -6,11 +6,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
"strconv"
"github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/config"
"github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/logger"
@ -50,7 +52,7 @@ func generateToken() string {
// then verifies the reported PID matches expectedPID to confirm the process // then verifies the reported PID matches expectedPID to confirm the process
// is actually a picoclaw gateway and not a foreign service on the same port. // is actually a picoclaw gateway and not a foreign service on the same port.
func isGatewayAlive(host string, port int, expectedPID int) bool { func isGatewayAlive(host string, port int, expectedPID int) bool {
url := fmt.Sprintf("http://%s:%d/health", host, port) url := "http://" + net.JoinHostPort(host, strconv.Itoa(port)) + "/health"
client := &http.Client{Timeout: 2 * time.Second} client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(url) resp, err := client.Get(url)
if err != nil { if err != nil {
@ -94,7 +96,7 @@ func WritePidFile(homePath, host string, port int) (*PidFileData, error) {
// PID file on a shared volume, the host's PID 1 (init) would // PID file on a shared volume, the host's PID 1 (init) would
// pass the isProcessRunning check, blocking new gateway starts. // pass the isProcessRunning check, blocking new gateway starts.
// Treat recorded PID 1 as always stale. // Treat recorded PID 1 as always stale.
if data.PID != 1 && isProcessRunning(data.PID) && isGatewayAlive(data.Host, data.Port, data.PID){ if data.PID != 1 && isProcessRunning(data.PID) && isGatewayAlive(data.Host, data.Port, data.PID) {
return nil, fmt.Errorf("gateway is already running (PID: %d, version: %s)", data.PID, data.Version) return nil, fmt.Errorf("gateway is already running (PID: %d, version: %s)", data.PID, data.Version)
} }
logger.Warnf("not running (PID: %d) so will remove the pid file: %s", data.PID, pidPath) logger.Warnf("not running (PID: %d) so will remove the pid file: %s", data.PID, pidPath)

View file

@ -124,8 +124,7 @@ func TestWritePidFileNonLocalhostHost(t *testing.T) {
} }
} }
// verifies that a foreign process reusing a crashed gateway's PID is treated as stale.
//verifies that a foreign process reusing a crashed gateway's PID is treated as stale.
func TestWritePidFileForeignPIDReuse(t *testing.T) { func TestWritePidFileForeignPIDReuse(t *testing.T) {
dir := tmpDir(t) dir := tmpDir(t)