feat: 优雅退出
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/models"
|
||||
@@ -93,7 +94,7 @@ func (gm *GroupManager) Invalidate() error {
|
||||
}
|
||||
|
||||
// Stop gracefully stops the GroupManager's background syncer.
|
||||
func (gm *GroupManager) Stop() {
|
||||
func (gm *GroupManager) Stop(ctx context.Context) {
|
||||
if gm.syncer != nil {
|
||||
gm.syncer.Stop()
|
||||
}
|
||||
|
@@ -1,9 +1,11 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/store"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -16,6 +18,7 @@ type LogCleanupService struct {
|
||||
settingsManager *config.SystemSettingsManager
|
||||
leaderLock *store.LeaderLock
|
||||
stopCh chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewLogCleanupService 创建新的日志清理服务
|
||||
@@ -30,18 +33,32 @@ func NewLogCleanupService(db *gorm.DB, settingsManager *config.SystemSettingsMan
|
||||
|
||||
// Start 启动日志清理服务
|
||||
func (s *LogCleanupService) Start() {
|
||||
s.wg.Add(1)
|
||||
go s.run()
|
||||
logrus.Debug("Log cleanup service started")
|
||||
}
|
||||
|
||||
// Stop 停止日志清理服务
|
||||
func (s *LogCleanupService) Stop() {
|
||||
func (s *LogCleanupService) Stop(ctx context.Context) {
|
||||
close(s.stopCh)
|
||||
logrus.Info("Log cleanup service stopped")
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
logrus.Info("LogCleanupService stopped gracefully.")
|
||||
case <-ctx.Done():
|
||||
logrus.Warn("LogCleanupService stop timed out.")
|
||||
}
|
||||
}
|
||||
|
||||
// run 运行日志清理的主循环
|
||||
func (s *LogCleanupService) run() {
|
||||
defer s.wg.Done()
|
||||
ticker := time.NewTicker(2 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/store"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -28,61 +29,77 @@ type RequestLogService struct {
|
||||
store store.Store
|
||||
settingsManager *config.SystemSettingsManager
|
||||
leaderLock *store.LeaderLock
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
stopChan chan struct{}
|
||||
wg sync.WaitGroup
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
// NewRequestLogService creates a new RequestLogService instance
|
||||
func NewRequestLogService(db *gorm.DB, store store.Store, sm *config.SystemSettingsManager, ls *store.LeaderLock) *RequestLogService {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &RequestLogService{
|
||||
db: db,
|
||||
store: store,
|
||||
settingsManager: sm,
|
||||
leaderLock: ls,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
stopChan: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Start initializes the service and starts the periodic flush routine
|
||||
func (s *RequestLogService) Start() {
|
||||
go s.flush()
|
||||
s.wg.Add(1)
|
||||
go s.runLoop()
|
||||
}
|
||||
|
||||
func (s *RequestLogService) runLoop() {
|
||||
defer s.wg.Done()
|
||||
|
||||
// Initial flush on start
|
||||
s.flush()
|
||||
|
||||
interval := time.Duration(s.settingsManager.GetSettings().RequestLogWriteIntervalMinutes) * time.Minute
|
||||
if interval <= 0 {
|
||||
interval = time.Minute
|
||||
}
|
||||
s.ticker = time.NewTicker(interval)
|
||||
defer s.ticker.Stop()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-s.ticker.C:
|
||||
newInterval := time.Duration(s.settingsManager.GetSettings().RequestLogWriteIntervalMinutes) * time.Minute
|
||||
if newInterval <= 0 {
|
||||
newInterval = time.Minute
|
||||
}
|
||||
if newInterval != interval {
|
||||
s.ticker.Reset(newInterval)
|
||||
interval = newInterval
|
||||
logrus.Debugf("Request log write interval updated to: %v", interval)
|
||||
}
|
||||
s.flush()
|
||||
case <-s.ctx.Done():
|
||||
s.ticker.Stop()
|
||||
logrus.Info("RequestLogService stopped.")
|
||||
return
|
||||
for {
|
||||
select {
|
||||
case <-s.ticker.C:
|
||||
newInterval := time.Duration(s.settingsManager.GetSettings().RequestLogWriteIntervalMinutes) * time.Minute
|
||||
if newInterval <= 0 {
|
||||
newInterval = time.Minute
|
||||
}
|
||||
if newInterval != interval {
|
||||
s.ticker.Reset(newInterval)
|
||||
interval = newInterval
|
||||
logrus.Debugf("Request log write interval updated to: %v", interval)
|
||||
}
|
||||
s.flush()
|
||||
case <-s.stopChan:
|
||||
return
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// Stop gracefully stops the RequestLogService
|
||||
func (s *RequestLogService) Stop() {
|
||||
s.flush()
|
||||
s.cancel()
|
||||
func (s *RequestLogService) Stop(ctx context.Context) {
|
||||
close(s.stopChan)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
s.flush()
|
||||
logrus.Info("RequestLogService stopped gracefully.")
|
||||
case <-ctx.Done():
|
||||
logrus.Warn("RequestLogService stop timed out.")
|
||||
}
|
||||
}
|
||||
|
||||
// Record logs a request to the database and cache
|
||||
|
Reference in New Issue
Block a user