diff options
author | Valery Piashchynski <[email protected]> | 2021-01-25 16:18:26 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-25 16:18:26 +0300 |
commit | f1875f5715bf7635e17697ae3513ba3d21e4e524 (patch) | |
tree | f9c0c3876ef542217a8bd7ff17f90bffc018132f /cmd/cli/serve.go | |
parent | a063ad05b1cab8ec71eecc32f836efa4d431c6b8 (diff) | |
parent | 99bf203511b8af4be37186017e2e0c73a030d4f3 (diff) |
Merge pull request #429 from spiral/2.0
Release 2.0-dev
Diffstat (limited to 'cmd/cli/serve.go')
-rw-r--r-- | cmd/cli/serve.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/cmd/cli/serve.go b/cmd/cli/serve.go new file mode 100644 index 00000000..993ec477 --- /dev/null +++ b/cmd/cli/serve.go @@ -0,0 +1,63 @@ +package cli + +import ( + "log" + "os" + "os/signal" + "syscall" + + "github.com/spf13/cobra" + "github.com/spiral/errors" + "go.uber.org/multierr" +) + +func init() { + root.AddCommand(&cobra.Command{ + Use: "serve", + Short: "Start RoadRunner server", + RunE: handler, + }) +} + +func handler(_ *cobra.Command, _ []string) error { + const op = errors.Op("handle_serve_command") + /* + We need to have path to the config at the RegisterTarget stage + But after cobra.Execute, because cobra fills up cli variables on this stage + */ + + err := Container.Init() + if err != nil { + return errors.E(op, err) + } + + errCh, err := Container.Serve() + if err != nil { + return errors.E(op, err) + } + + // https://golang.org/pkg/os/signal/#Notify + // should be of buffer size at least 1 + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) + + for { + select { + case e := <-errCh: + err = multierr.Append(err, e.Error) + log.Printf("error occurred: %v, service: %s", e.Error.Error(), e.VertexID) + er := Container.Stop() + if er != nil { + err = multierr.Append(err, er) + return errors.E(op, err) + } + return errors.E(op, err) + case <-c: + err = Container.Stop() + if err != nil { + return errors.E(op, err) + } + return nil + } + } +} |