diff --git a/.air.toml b/.air.toml index 4e2e9398..6b51df6f 100644 --- a/.air.toml +++ b/.air.toml @@ -17,13 +17,13 @@ tmp_dir = "tmp" include_dir = [] include_ext = ["go", "tpl", "tmpl", "html", "yaml"] include_file = [] - kill_delay = "0s" + kill_delay = "30s" log = "build-errors.log" poll = false poll_interval = 0 rerun = false rerun_delay = 500 - send_interrupt = false + send_interrupt = true stop_on_error = false [color] diff --git a/cmd/server/main.go b/cmd/server/main.go index 51b571be..9ce82fc4 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -59,19 +59,6 @@ func main() { tracer *tracing.Tracer, resourceCleaner interfaces.ResourceCleaner, ) error { - // Create context for resource cleanup - shutdownTimeout := cfg.Server.ShutdownTimeout - if shutdownTimeout == 0 { - shutdownTimeout = 30 * time.Second - } - cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), shutdownTimeout) - defer cleanupCancel() - - // Register tracer cleanup function to resource cleaner - resourceCleaner.RegisterWithName("Tracer", func() error { - return tracer.Cleanup(cleanupCtx) - }) - // Create HTTP server server := &http.Server{ Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port), @@ -86,7 +73,11 @@ func main() { logger.Infof(context.Background(), "Received signal: %v, starting server shutdown...", sig) // Create a context with timeout for server shutdown - shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownTimeout := cfg.Server.ShutdownTimeout + if shutdownTimeout == 0 { + shutdownTimeout = 30 * time.Second + } + shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), shutdownTimeout) defer shutdownCancel() if err := server.Shutdown(shutdownCtx); err != nil { @@ -95,11 +86,10 @@ func main() { // Clean up all registered resources logger.Info(context.Background(), "Cleaning up resources...") - errs := resourceCleaner.Cleanup(cleanupCtx) + errs := resourceCleaner.Cleanup(shutdownCtx) if len(errs) > 0 { logger.Errorf(context.Background(), "Errors occurred during resource cleanup: %v", errs) } - logger.Info(context.Background(), "Server has exited") done() }() diff --git a/internal/container/container.go b/internal/container/container.go index a4ed19bb..2a2e6648 100644 --- a/internal/container/container.go +++ b/internal/container/container.go @@ -88,6 +88,9 @@ func BuildContainer(container *dig.Container) *dig.Container { must(container.Provide(initAntsPool)) must(container.Provide(initContextStorage)) + // Register tracer cleanup handler (tracer needs to be available for cleanup registration) + must(container.Invoke(registerTracerCleanup)) + // Register goroutine pool cleanup handler must(container.Invoke(registerPoolCleanup)) @@ -740,6 +743,19 @@ func registerPoolCleanup(pool *ants.Pool, cleaner interfaces.ResourceCleaner) { }) } +// registerTracerCleanup registers the tracer for cleanup +// Ensures proper cleanup of the tracer when application shuts down +// Parameters: +// - tracer: Tracer instance +// - cleaner: Resource cleaner +func registerTracerCleanup(tracer *tracing.Tracer, cleaner interfaces.ResourceCleaner) { + // Register the cleanup function - actual context will be provided during cleanup + cleaner.RegisterWithName("Tracer", func() error { + // Create context for cleanup with longer timeout for tracer shutdown + return tracer.Cleanup(context.Background()) + }) +} + // initDocReaderClient initializes the DocumentReader client (lightweight API). func initDocReaderClient(cfg *config.Config) (interfaces.DocumentReader, error) { addr := strings.TrimSpace(os.Getenv("DOCREADER_ADDR"))