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(relay): gracefully shutdown #35

Merged
merged 3 commits into from
Sep 15, 2024
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
18 changes: 17 additions & 1 deletion cmd/commands/help.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package commands

import "fmt"

const help = `Available commands:
Commands:
run <path_to_config> starts the application with the specified configuration file.
help displays this help information.
version displays the version of software.

Usage:
<command> [options]

Examples:
run config.yaml run the application using config.yaml.
help display this help message.
`

func HandleHelp(_ []string) {
// TODO:::
fmt.Print(help) //nolint
}
27 changes: 20 additions & 7 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"errors"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -25,15 +26,27 @@ func HandleRun(args []string) {
ExitOnError(err)
}

if err := r.Start(); err != nil {
ExitOnError(err)
}

errCh := make(chan error)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

if err := r.Stop(); err != nil {
ExitOnError(err)
go func() {
if err := r.Start(); err != nil {
errCh <- err
}
}()

select {
case sig := <-sigChan:
fmt.Printf("Received signal: %s\nInitiating graceful shutdown...\n", sig.String()) //nolint
if err := r.Stop(); err != nil {
ExitOnError(err)
}

case err := <-errCh:
fmt.Printf("Unexpected error: %v\nInitiating shutdown due to the error...\n", err) //nolint
if err := r.Stop(); err != nil {
ExitOnError(err)
}
}
}
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

func main() {
if len(os.Args) < 2 {
commands.ExitOnError(errors.New("at least 1 arguments expected.\nuse help command for more information"))
commands.HandleHelp(os.Args)
commands.ExitOnError(errors.New("at least 1 arguments expected"))
}

switch os.Args[1] {
Expand All @@ -20,6 +21,7 @@ func main() {

case "help":
commands.HandleHelp(os.Args)
os.Exit(0)

case "version":
fmt.Println(immortal.StringVersion()) //nolint
Expand Down
2 changes: 1 addition & 1 deletion relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func New(cfg config.Config) (*Relay, error) {

return &Relay{
config: cfg,
server: server.NewServer(cfg.ServerConf),
server: server.New(cfg.ServerConf),
database: db,
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Server struct {
connsLock sync.RWMutex
}

func NewServer(cfg Config) *Server {
func New(cfg Config) *Server {
return &Server{
config: cfg,
conns: make(map[*websocket.Conn]map[string]filter.Filters),
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *Server) readLoop(ws *websocket.Conn) {
go s.handleEvent(ws, msg)

case "CLOSE":
s.handleClose(ws, msg)
go s.handleClose(ws, msg)
}
}
}
Expand Down