Exit gracefully (run deferred statements in main()) on SIGINT.

This commit is contained in:
miyuko
2025-11-21 15:09:36 +00:00
parent b5a1626a10
commit b01e67f993
3 changed files with 20 additions and 1 deletions

View File

@@ -384,6 +384,11 @@ func Main() {
} else {
log.Println("serve: ready")
}
select {}
interrupt := make(chan struct{})
OnInterrupt(func() { interrupt <- struct{}{} })
<-interrupt
log.Println("exiting gracefully")
}
}

View File

@@ -5,3 +5,7 @@ package git_pages
func OnReload(handler func()) {
// not implemented
}
func OnInterrupt(handler func()) {
// not implemented
}

View File

@@ -18,3 +18,13 @@ func OnReload(handler func()) {
}
}()
}
func OnInterrupt(handler func()) {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT)
go func() {
<-sigint
signal.Stop(sigint)
handler()
}()
}