Exit with the shutdown's code and wait for image processing (closes #86)
check / check (push) Successful in 5m14s

fx alone handles SIGINT and SIGTERM; the server's own handler, which
only cancelled a context that fx's stop did not wait for, is gone.
runApp starts the fx app, waits for a signal or a shutdown request,
stops the app and returns the exit code, which main passes to os.Exit.
A listen error asks fx to shut down with exit code 1. A Sentry DSN that
cannot be used fails the server's start hook, so fx stops what had
already started. The server's stop hook stops the HTTP server, then
waits for the images still being processed, both within
ShutdownTimeout; images still being processed after that are logged and
fail the stop, so the exit code is 1.

Model: opus-5-5
This commit is contained in:
2026-10-03 14:42:41 +00:00
parent b6b48bc94a
commit 4f513bede8
7 changed files with 147 additions and 71 deletions
+39 -2
View File
@@ -2,8 +2,11 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"go.uber.org/fx"
@@ -45,7 +48,10 @@ func run(_ *cobra.Command, _ []string) {
_ = os.Setenv("PIXA_CONFIG_PATH", configPath)
}
fx.New(
// A write to a closed stdout or stderr must not end the process.
signal.Ignore(syscall.SIGPIPE)
app := fx.New(
fx.Provide(
config.New,
database.New,
@@ -60,5 +66,36 @@ func run(_ *cobra.Command, _ []string) {
func(log *logger.Logger) { log.Identify() },
func(*server.Server) {},
),
).Run()
)
os.Exit(runApp(app))
}
// runApp starts app, waits for SIGINT, SIGTERM or a shutdown request, then
// stops app. It returns the exit code: the one the shutdown request carries,
// 0 for a signal, or 1 when app fails to start or to stop; fx logs that
// error itself. It does what fx's App.Run does, but returns the exit code
// instead of exiting.
func runApp(app *fx.App) int {
startCtx, cancelStart := context.WithTimeout(
context.Background(), app.StartTimeout())
defer cancelStart()
err := app.Start(startCtx)
if err != nil {
return 1
}
shutdown := <-app.Wait()
stopCtx, cancelStop := context.WithTimeout(
context.Background(), app.StopTimeout())
defer cancelStop()
err = app.Stop(stopCtx)
if err != nil {
return 1
}
return shutdown.ExitCode
}