Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc): adding shutdown method. #108

Merged
merged 6 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/signal"
"syscall"

"github.com/dezh-tech/immortal"
"github.com/dezh-tech/immortal/cmd/relay"
"github.com/dezh-tech/immortal/config"
"github.com/dezh-tech/immortal/pkg/logger"
Expand All @@ -23,6 +24,8 @@ func HandleRun(args []string) {

logger.InitGlobalLogger(&cfg.Logger)

logger.Info("running immortal", "version", immortal.StringVersion())

r, err := relay.New(cfg)
if err != nil {
ExitOnError(err)
Expand All @@ -31,7 +34,9 @@ func HandleRun(args []string) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

errCh := r.Start()
shutdownch := make(chan struct{}, 1)

errCh := r.Start(shutdownch)

select {
case sig := <-sigChan:
Expand All @@ -45,5 +50,11 @@ func HandleRun(args []string) {
if err := r.Stop(); err != nil {
ExitOnError(err)
}

case shsig := <-shutdownch:
logger.Info("Received signal from manager over grpc: Initiating graceful shutdown", "signal", shsig)
if err := r.Stop(); err != nil {
ExitOnError(err)
}
}
}
8 changes: 6 additions & 2 deletions cmd/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func New(cfg *config.Config) (*Relay, error) {
}

// Start runs the relay and its children.
func (r *Relay) Start() chan error {
func (r *Relay) Start(shutdownch chan struct{}) chan error {
logger.Info("starting the relay")

errCh := make(chan error, 2)
Expand All @@ -97,7 +97,7 @@ func (r *Relay) Start() chan error {
}()

go func() {
if err := r.grpcServer.Start(); err != nil {
if err := r.grpcServer.Start(shutdownch); err != nil {
errCh <- err
}
}()
Expand All @@ -121,5 +121,9 @@ func (r *Relay) Stop() error {
return err
}

if err := r.redis.Close(); err != nil {
return err
}

return nil
}
193 changes: 193 additions & 0 deletions delivery/grpc/gen/shutdown.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions delivery/grpc/gen/shutdown_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions delivery/grpc/proto/shutdown.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package relay.v1;

option go_package = "github.com/dezh-tech/immortal/delivery/grpc";

service ShutdownService {
rpc Shutdown (ShutdownRequest) returns (ShutdownResponse);
}

message ShutdownRequest {}

message ShutdownResponse {}
Loading
Loading