feat: simplify Service interface and standardize Stop arguments (by AI)
This commit is contained in:
parent
b17ab4924d
commit
e797dc846a
@ -1,15 +0,0 @@
|
|||||||
## v1.0.1 (2026-05-12)
|
|
||||||
|
|
||||||
### 🚀 Features
|
|
||||||
- **Secure IPC**: SHA256 token-based authentication for Unix Domain Sockets.
|
|
||||||
- **Precise Signaling**: `kill <svc_name> <signal_num>` command.
|
|
||||||
- **Trace ID Propagation**: Shared Trace ID for service startup logs.
|
|
||||||
- **Enhanced `status`**: Secured detailed health reporting.
|
|
||||||
|
|
||||||
### 🧹 Cleanup
|
|
||||||
- **Minimalist API**: `Register`, `Run`, `AddCommand`, `SetAppInfo`.
|
|
||||||
- **Automated PID**: System temp directory placement.
|
|
||||||
|
|
||||||
### 🛠 Improvements
|
|
||||||
- **Race Condition Fix**: Delayed IPC server activation until startup completion.
|
|
||||||
- **Infrastructure Alignment**: `cast.To[T]`, `timer.Retry`, and `id`.
|
|
||||||
142
CODE-SUMMARY.md
142
CODE-SUMMARY.md
@ -1,142 +0,0 @@
|
|||||||
### starter > starter.go
|
|
||||||
```go
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Default configuration
|
|
||||||
appName = filepath.Base(os.Args[0])
|
|
||||||
appVersion = "1.0.1"
|
|
||||||
|
|
||||||
// Internal state
|
|
||||||
commands = make(map[string]*command)
|
|
||||||
|
|
||||||
// New Service registry
|
|
||||||
services = make(map[int][]*managedService)
|
|
||||||
startedPriorities []int
|
|
||||||
|
|
||||||
// Flags
|
|
||||||
flagSet = flag.NewFlagSet(appName, flag.ContinueOnError)
|
|
||||||
|
|
||||||
// IPC Security
|
|
||||||
ipcSecret = "apigo-starter-secret-2026"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Service defines the lifecycle of a component managed by the starter.
|
|
||||||
type Service interface {
|
|
||||||
// Start starts the service. It should block until the service is ready.
|
|
||||||
Start(ctx context.Context, logger *log.Logger) error
|
|
||||||
// Stop stops the service. It should block until the service is cleaned up.
|
|
||||||
Stop(ctx context.Context) error
|
|
||||||
// Health returns the health status of the service.
|
|
||||||
Health() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reloader defines an optional interface for services that support configuration reloading.
|
|
||||||
type Reloader interface {
|
|
||||||
Reload() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// UserSignalHandler defines an optional interface for services that handle custom user signals.
|
|
||||||
type UserSignalHandler interface {
|
|
||||||
// HandleUserSignal handles a custom signal. Return true if the signal was handled.
|
|
||||||
HandleUserSignal(sig os.Signal) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type managedService struct {
|
|
||||||
Name string
|
|
||||||
svc Service
|
|
||||||
priority int
|
|
||||||
startTimeout time.Duration
|
|
||||||
stopTimeout time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
type command struct {
|
|
||||||
name string
|
|
||||||
desc string
|
|
||||||
fn func()
|
|
||||||
}
|
|
||||||
|
|
||||||
func init()
|
|
||||||
|
|
||||||
|
|
||||||
// Register adds a service to be managed by the starter.
|
|
||||||
func Register(name string, svc Service, priority int, startTimeout, stopTimeout time.Duration)
|
|
||||||
|
|
||||||
|
|
||||||
// SetAppInfo sets the application name and version.
|
|
||||||
func SetAppInfo(name, version string)
|
|
||||||
|
|
||||||
|
|
||||||
// AddCommand adds a custom command.
|
|
||||||
func AddCommand(name, desc string, fn func())
|
|
||||||
|
|
||||||
|
|
||||||
// Run parses arguments and executes the service.
|
|
||||||
func Run()
|
|
||||||
|
|
||||||
|
|
||||||
func showHelp()
|
|
||||||
|
|
||||||
|
|
||||||
func runForeground()
|
|
||||||
|
|
||||||
|
|
||||||
func startServices(ctx context.Context) error
|
|
||||||
|
|
||||||
|
|
||||||
func stopServices()
|
|
||||||
|
|
||||||
|
|
||||||
func reloadServices()
|
|
||||||
|
|
||||||
|
|
||||||
func handleUserSignal(svcName *string, sig os.Signal) bool
|
|
||||||
|
|
||||||
|
|
||||||
func serveIPC(l net.Listener)
|
|
||||||
|
|
||||||
|
|
||||||
func getInternalStatus() string
|
|
||||||
|
|
||||||
|
|
||||||
func startCmd()
|
|
||||||
|
|
||||||
|
|
||||||
func stopCmd()
|
|
||||||
|
|
||||||
|
|
||||||
func restartCmd()
|
|
||||||
|
|
||||||
|
|
||||||
func statusCmd()
|
|
||||||
|
|
||||||
|
|
||||||
func killCmd()
|
|
||||||
|
|
||||||
|
|
||||||
func callIPC(pid int, cmd string) (string, error)
|
|
||||||
|
|
||||||
|
|
||||||
func getIPCToken(pid int) string
|
|
||||||
|
|
||||||
|
|
||||||
func getPidPath() string
|
|
||||||
|
|
||||||
|
|
||||||
func getSockPath() string
|
|
||||||
|
|
||||||
|
|
||||||
func savePid(p int)
|
|
||||||
|
|
||||||
|
|
||||||
func loadPid() int
|
|
||||||
|
|
||||||
|
|
||||||
func removePid()
|
|
||||||
|
|
||||||
|
|
||||||
func isProcessRunning(p int) bool
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
28
service.go
Normal file
28
service.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package starter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"apigo.cc/go/log"
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Service defines the lifecycle of a component managed by the starter.
|
||||||
|
type Service interface {
|
||||||
|
// Start starts the service. It should block until the service is ready.
|
||||||
|
Start(ctx context.Context, logger *log.Logger) error
|
||||||
|
// Stop stops the service. It should block until the service is cleaned up.
|
||||||
|
Stop(ctx context.Context) error
|
||||||
|
// Health returns the health status of the service.
|
||||||
|
Health() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reloader defines an optional interface for services that support configuration reloading.
|
||||||
|
type Reloader interface {
|
||||||
|
Reload() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserSignalHandler defines an optional interface for services that handle custom user signals.
|
||||||
|
type UserSignalHandler interface {
|
||||||
|
// HandleUserSignal handles a custom signal. Return true if the signal was handled.
|
||||||
|
HandleUserSignal(sig os.Signal) bool
|
||||||
|
}
|
||||||
21
starter.go
21
starter.go
@ -44,27 +44,6 @@ var (
|
|||||||
ipcSecret = "apigo-starter-secret-2026"
|
ipcSecret = "apigo-starter-secret-2026"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service defines the lifecycle of a component managed by the starter.
|
|
||||||
type Service interface {
|
|
||||||
// Start starts the service. It should block until the service is ready.
|
|
||||||
Start(ctx context.Context, logger *log.Logger) error
|
|
||||||
// Stop stops the service. It should block until the service is cleaned up.
|
|
||||||
Stop(ctx context.Context) error
|
|
||||||
// Health returns the health status of the service.
|
|
||||||
Health() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reloader defines an optional interface for services that support configuration reloading.
|
|
||||||
type Reloader interface {
|
|
||||||
Reload() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// UserSignalHandler defines an optional interface for services that handle custom user signals.
|
|
||||||
type UserSignalHandler interface {
|
|
||||||
// HandleUserSignal handles a custom signal. Return true if the signal was handled.
|
|
||||||
HandleUserSignal(sig os.Signal) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type managedService struct {
|
type managedService struct {
|
||||||
Name string
|
Name string
|
||||||
svc Service
|
svc Service
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user