Files
smart-shutdown/pkg/daemon/service.go
UnbalancedCat 3be3e19e49
Some checks failed
Release / Build & Release (push) Has been cancelled
release: v0.2.0
Comprehensive release containing structural, UX, and behavioral upgrades since v0.1.0:

1. Namespace Transition: Renamed core executable and project namespaces from 'smart-monitor' to 'smart-shutdown'.
2. Objective Vocabulary Refactoring: Normalized output strings and logging descriptors system-wide to a strict, professional tone.
3. Advanced Status Query: 'status' subcommand now retrieves the parsed configuration, log locations/sizes, and tails the last 10 lines of the local log file.
4. Runtime Configuration Setter: Introduced 'config set' subcommand to modify the configuration file with strict type validations.
5. Auto-System Deployment: Remapped 'install' to clone the executable into system domains and register global PATH variables.
6. Cleaner Removal: 'uninstall' purges binary clones and clears environmental variables, leaving zero traces.
7. Documentation Rewrite: Generated an objective README file featuring copy-paste ready Markdown blocks.
2026-03-24 16:07:22 +08:00

61 lines
1.1 KiB
Go

package daemon
import (
"context"
"smart-shutdown/pkg/config"
"smart-shutdown/pkg/logger"
"smart-shutdown/pkg/monitor"
"github.com/kardianos/service"
)
type program struct {
exit chan struct{}
cancel context.CancelFunc
cfg *config.Config
}
func (p *program) Start(s service.Service) error {
logger.Info("启动系统监控后台服务")
ctx, cancel := context.WithCancel(context.Background())
p.cancel = cancel
p.exit = make(chan struct{})
go p.run(ctx)
return nil
}
func (p *program) run(ctx context.Context) {
monitor.Run(ctx, p.cfg)
close(p.exit)
}
func (p *program) Stop(s service.Service) error {
logger.Info("停止系统监控后台服务")
if p.cancel != nil {
p.cancel()
}
<-p.exit
return nil
}
func GetService(cfg *config.Config) (service.Service, error) {
svcConfig := &service.Config{
Name: "SmartNetworkMonitor",
DisplayName: "Smart Network Shutdown Monitor",
Description: "A reliable daemon that periodically monitors network states and triggers node suspension logically.",
}
prg := &program{
cfg: cfg,
}
s, err := service.New(prg, svcConfig)
if err != nil {
return nil, err
}
return s, nil
}