feat: 优雅退出

This commit is contained in:
tbphp
2025-07-13 20:55:32 +08:00
parent 99fd68d08f
commit ae751eb48a
8 changed files with 142 additions and 54 deletions

View File

@@ -1,6 +1,7 @@
package keypool
import (
"context"
"gpt-load/internal/config"
"gpt-load/internal/models"
"gpt-load/internal/store"
@@ -44,12 +45,23 @@ func (s *CronChecker) Start() {
go s.runLoop()
}
// Stop stops the cron job.
func (s *CronChecker) Stop() {
logrus.Info("Stopping CronChecker...")
// Stop stops the cron job, respecting the context for shutdown timeout.
func (s *CronChecker) Stop(ctx context.Context) {
close(s.stopChan)
s.wg.Wait()
logrus.Info("CronChecker stopped.")
// Wait for the goroutine to finish, or for the shutdown to time out.
done := make(chan struct{})
go func() {
s.wg.Wait()
close(done)
}()
select {
case <-done:
logrus.Info("CronChecker stopped gracefully.")
case <-ctx.Done():
logrus.Warn("CronChecker stop timed out.")
}
}
func (s *CronChecker) runLoop() {
@@ -104,8 +116,12 @@ func (s *CronChecker) submitValidationJobs() {
validatedCount := len(invalidKeys)
becameValidCount := 0
if validatedCount > 0 {
logrus.Debugf("CronChecker: Found %d invalid keys to validate for group %s.", validatedCount, group.Name)
for j := range invalidKeys {
select {
case <-s.stopChan:
return
default:
}
key := &invalidKeys[j]
isValid, _ := s.Validator.ValidateSingleKey(key, group)