Take an exclusive lock on DATA_DIR at startup (closes #201) (#220)
check / check (push) Superseded by a newer commit; never tested
check / check (push) Superseded by a newer commit; never tested
This commit was merged in pull request #220.
This commit is contained in:
@@ -2,11 +2,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.uber.org/fx"
|
||||
"sneak.berlin/go/webhooker/internal/config"
|
||||
"sneak.berlin/go/webhooker/internal/database"
|
||||
"sneak.berlin/go/webhooker/internal/datadir"
|
||||
"sneak.berlin/go/webhooker/internal/delivery"
|
||||
"sneak.berlin/go/webhooker/internal/globals"
|
||||
"sneak.berlin/go/webhooker/internal/handlers"
|
||||
@@ -56,7 +60,32 @@ func main() {
|
||||
globals.Appname = appname
|
||||
globals.Version = version
|
||||
|
||||
os.Exit(run(os.Stderr))
|
||||
}
|
||||
|
||||
// run takes the exclusive DATA_DIR lock, then runs the application
|
||||
// under it, and returns the process exit status.
|
||||
//
|
||||
// The lock is taken here rather than inside the fx graph because it has
|
||||
// to be held before anything opens a database, and because a refusal
|
||||
// has to reach the operator as a plain line on standard error rather
|
||||
// than as one entry in an fx failure dump. It is released by the defer
|
||||
// on a clean shutdown, and by the kernel closing the descriptor on any
|
||||
// other exit — including the one fx performs itself when a start or
|
||||
// stop hook fails, which skips deferred calls.
|
||||
func run(stderr io.Writer) int {
|
||||
lock, err := datadir.Acquire(config.DataDir())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(stderr, "%s: %v\n", appname, err)
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
defer func() { _ = lock.Release() }()
|
||||
|
||||
newApp().Run()
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// newApp builds the application graph. It is separate from main so
|
||||
|
||||
Reference in New Issue
Block a user