Skip to content

Commit

Permalink
feat(grpc): adding shutdown method. (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jan 11, 2025
1 parent 165c5d0 commit 7694ec2
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 6 deletions.
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

0 comments on commit 7694ec2

Please sign in to comment.