Skip to content

Commit 27adbac

Browse files
haoqixumvdan
authored andcommitted
cmd/cue: handle signals and perform a graceful shutdown for mod registry
The registry did not handle signals. The default behavior of a Go program for receiving `SIGINT` and `SIGTERM` signals is to exit and return `128 + <signal number>`, regardless of whether it exits successfully or not. Handle signals and perform a graceful shutdown for the registry. Return zero when it exits successfully. Fixes #3224 Change-Id: I90a534d47f43b2d9f93db7f460df8892a695f134 Signed-off-by: haoqixu <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196370 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 695fefc commit 27adbac

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

cmd/cue/cmd/modregistry.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ package cmd
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"net"
2122
"net/http"
23+
"os"
24+
"os/signal"
25+
"syscall"
26+
"time"
2227

2328
"cuelabs.dev/go/oci/ociregistry"
2429
"cuelabs.dev/go/oci/ociregistry/ocimem"
2530
"cuelabs.dev/go/oci/ociregistry/ociserver"
2631
"github.com/spf13/cobra"
2732
)
2833

29-
// TODO: add testing for this command.
30-
3134
func newModRegistryCmd(c *Command) *cobra.Command {
3235
cmd := &cobra.Command{
3336
// This command is hidden, for now at least,
@@ -63,7 +66,28 @@ func runModRegistry(cmd *Command, args []string) error {
6366
r := ocimem.NewWithConfig(&ocimem.Config{
6467
ImmutableTags: true,
6568
})
66-
return http.Serve(l, ociserver.New(ociTagLoggerRegistry{r}, nil))
69+
70+
srv := http.Server{
71+
Handler: ociserver.New(ociTagLoggerRegistry{r}, nil),
72+
}
73+
var serveErr error
74+
go func() {
75+
if err := srv.Serve(l); !errors.Is(err, http.ErrServerClosed) {
76+
serveErr = err
77+
}
78+
}()
79+
80+
sigint := make(chan os.Signal, 1)
81+
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
82+
<-sigint
83+
84+
ctx, cancal := context.WithTimeout(context.Background(), 2*time.Second)
85+
defer cancal()
86+
if err := srv.Shutdown(ctx); err != nil {
87+
fmt.Printf("HTTP server Shutdown: %v\n", err)
88+
return err
89+
}
90+
return serveErr
6791
}
6892

6993
type ociTagLoggerRegistry struct {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TODO: use a random unused port
2+
env CUE_REGISTRY=localhost:41331
3+
exec cue mod registry ${CUE_REGISTRY} &
4+
exec cue mod publish v0.0.1
5+
cmpenv stdout expect-stdout
6+
7+
-- expect-stdout --
8+
published mod.example/[email protected] to ${CUE_REGISTRY}/mod.example/blah:v0.0.1
9+
-- blah.cue --
10+
package blah
11+
blah: 42
12+
-- cue.mod/module.cue --
13+
module: "mod.example/blah@v0"
14+
language: {
15+
version: "v0.9.0"
16+
}
17+
source: {
18+
kind: "self"
19+
}

0 commit comments

Comments
 (0)