Get rid of the fucking signalling
This commit is contained in:
64
main.go
64
main.go
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
"eve-typesense-api/internal/database"
|
||||
@@ -21,7 +19,7 @@ const (
|
||||
func main() {
|
||||
flag.Parse()
|
||||
logger.InitFlag()
|
||||
|
||||
|
||||
logger.Default = logger.Default.ToFile("eve-typesense-api.log")
|
||||
logger.Info("Starting EVE Typesense API...")
|
||||
|
||||
@@ -39,51 +37,27 @@ func main() {
|
||||
}
|
||||
logger.Info("Typesense initialized")
|
||||
|
||||
// Setup context with cancellation for graceful shutdown
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
// Run mirroring
|
||||
results := mirror.Mirror(context.Background())
|
||||
|
||||
// Handle graceful shutdown
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
// Report results
|
||||
successCount := 0
|
||||
errorCount := 0
|
||||
totalDocs := int64(0)
|
||||
|
||||
go func() {
|
||||
<-sigChan
|
||||
logger.Warning("Received shutdown signal, cancelling...")
|
||||
cancel()
|
||||
}()
|
||||
|
||||
// Run mirroring in a goroutine
|
||||
resultChan := make(chan []mirror.Result, 1)
|
||||
go func() {
|
||||
results := mirror.Mirror(ctx)
|
||||
resultChan <- results
|
||||
}()
|
||||
|
||||
// Wait for mirroring to complete or be cancelled
|
||||
select {
|
||||
case results := <-resultChan:
|
||||
successCount := 0
|
||||
errorCount := 0
|
||||
totalDocs := int64(0)
|
||||
|
||||
for _, result := range results {
|
||||
if result.Error != nil {
|
||||
errorCount++
|
||||
logger.Error("Error mirroring %s: %v", result.TableName, result.Error)
|
||||
} else {
|
||||
successCount++
|
||||
totalDocs += result.Count
|
||||
}
|
||||
for _, result := range results {
|
||||
if result.Error != nil {
|
||||
errorCount++
|
||||
logger.Error("Error mirroring %s: %v", result.TableName, result.Error)
|
||||
} else {
|
||||
successCount++
|
||||
totalDocs += result.Count
|
||||
}
|
||||
|
||||
logger.Info("Mirroring complete: %d tables, %d documents, %d errors", successCount, totalDocs, errorCount)
|
||||
case <-ctx.Done():
|
||||
logger.Warning("Mirroring cancelled by user")
|
||||
}
|
||||
|
||||
// Keep the application running
|
||||
logger.Info("Application ready. Press Ctrl+C to exit.")
|
||||
<-sigChan
|
||||
logger.Info("Shutting down...")
|
||||
logger.Info("Mirroring complete: %d tables, %d documents, %d errors", successCount, totalDocs, errorCount)
|
||||
|
||||
if errorCount > 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user