Reload configuration on SIGHUP (if supported by OS).

On Windows, there is no way to reload configuration at runtime.
This commit is contained in:
Catherine
2025-11-20 03:43:20 +00:00
parent a924dd5116
commit c93d3a0bb5
3 changed files with 47 additions and 0 deletions

View File

@@ -303,6 +303,26 @@ func Main() {
}
default:
// Hook a signal (SIGHUP on *nix, nothing on Windows) for reloading the configuration
// at runtime. This is useful because it preserves S3 backend cache contents. Failed
// configuration reloads will not crash the process; you may want to check the syntax
// first with `git-pages -config ... -print-config` since there is no other feedback.
OnReload(func() {
if newConfig, err := Configure(*configTomlPath); err != nil {
log.Println("config:", err)
log.Println("config: reload failed")
} else {
log.Println("config: reloaded")
// From https://go.dev/ref/mem:
// > A read r of a memory location x holding a value that is not larger than
// > a machine word must observe some write w such that r does not happen before
// > w and there is no write w' such that w happens before w' and w' happens
// > before r. That is, each read must observe a value written by a preceding or
// > concurrent write.
config = newConfig
}
})
// Start listening on all ports before initializing the backend, otherwise if the backend
// spends some time initializing (which the S3 backend does) a proxy like Caddy can race
// with git-pages on startup and return errors for requests that would have been served

7
src/signal_other.go Normal file
View File

@@ -0,0 +1,7 @@
//go:build !unix
package git_pages
func OnReload(handler func()) {
// not implemented
}

20
src/signal_posix.go Normal file
View File

@@ -0,0 +1,20 @@
//go:build unix
package git_pages
import (
"os"
"os/signal"
"syscall"
)
func OnReload(handler func()) {
sighup := make(chan os.Signal, 1)
signal.Notify(sighup, syscall.SIGHUP)
go func() {
for {
<-sighup
handler()
}
}()
}