mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(scheduler): add base context for graceful shutdown
- Create application-level context with cancel function - Pass base context to scheduler via WithBaseContext option - Move scheduler.Stop() to explicit shutdown sequence after context cancellation - Upgrade foundation dependency to v0.7.0 for new scheduler options
This commit is contained in:
@@ -85,6 +85,8 @@ func main() {
|
||||
}
|
||||
|
||||
logger, _ := logging.New(logging.Options{Service: "ez-api"})
|
||||
appCtx, appCancel := context.WithCancel(context.Background())
|
||||
defer appCancel()
|
||||
if len(os.Args) > 1 && os.Args[1] == "import" {
|
||||
code := runImport(logger, os.Args[2:])
|
||||
os.Exit(code)
|
||||
@@ -201,7 +203,11 @@ func main() {
|
||||
alertDetector := cron.NewAlertDetector(db, logDB, rdb, service.NewStatsService(rdb), alertDetectorConfig, logger)
|
||||
|
||||
// Setup scheduler (jobs are added incrementally, Start() called after all services initialized)
|
||||
sched := scheduler.New(scheduler.WithLogger(logger), scheduler.WithSkipIfRunning())
|
||||
sched := scheduler.New(
|
||||
scheduler.WithLogger(logger),
|
||||
scheduler.WithSkipIfRunning(),
|
||||
scheduler.WithBaseContext(appCtx),
|
||||
)
|
||||
sched.Every("quota-reset", time.Duration(cfg.Quota.ResetIntervalSeconds)*time.Second, quotaResetter.RunOnce)
|
||||
sched.Every("log-cleanup", time.Hour, logCleaner.RunOnce)
|
||||
sched.Every("token-refresh", time.Duration(cfg.TokenRefresh.IntervalSeconds)*time.Second, tokenRefresher.RunOnce)
|
||||
@@ -209,7 +215,6 @@ func main() {
|
||||
if outboxService != nil && outboxService.Enabled() {
|
||||
sched.Every("sync-outbox", outboxService.Interval(), outboxService.RunOnce)
|
||||
}
|
||||
defer sched.Stop()
|
||||
|
||||
adminService, err := service.NewAdminService()
|
||||
if err != nil {
|
||||
@@ -430,6 +435,8 @@ func main() {
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
logger.Info("shutting down server...")
|
||||
appCancel()
|
||||
sched.Stop()
|
||||
|
||||
// Shutdown with timeout
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
|
||||
Reference in New Issue
Block a user