Skip to content

Commit

Permalink
fix: refactoring code of graceful exits (openimsdk#1885)
Browse files Browse the repository at this point in the history
* fix: plant a layer

* fix: print chanal

* fix: print sigs

* fix: print the sigs

* fix: reconstruct exit gracefully

* fix: fix the timeout

* fix: fix the netDone

* fix: fix the process exit

* fix: refactor the elegant startup code

* fix: fix the Signal.Notify

* fix: fix the code

* fix: remove not used header import.

* Update init.go

* fix: fix the InitConfig error

* fix: fix branch name

* fix: fix the signal value

* fix: replace the signal with SIGTERM

* fix: fix the script

* fix: fix the unsolve error

* fix: return the SIGTERM received,shutting down

* fix: fix the tranfer exit error

* fix: fix the error

* fix: replace the SIGnal

* fix: del the error return in tranfer

* fix: fix SIGTERM error

* fix: del the unreachalbe code

* fix: fix the make stop print  error

---------

Co-authored-by: OpenIM-Gordon <46924906+FGadvancer@users.noreply.github.com>
Co-authored-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
  • Loading branch information
3 people authored Feb 18, 2024
1 parent cbce4da commit c754ec6
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 268 deletions.
44 changes: 30 additions & 14 deletions cmd/openim-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"context"
"fmt"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
"net"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -46,8 +47,7 @@ func main() {
apiCmd.AddPrometheusPortFlag()
apiCmd.AddApi(run)
if err := apiCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}

Expand Down Expand Up @@ -76,12 +76,21 @@ func run(port int, proPort int) error {
if err = client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, config.Config.EncodeConfig()); err != nil {
return err
}

var (
netDone = make(chan struct{}, 1)
netErr error
)
router := api.NewGinRouter(client, rdb)
if config.Config.Prometheus.Enable {
p := ginprom.NewPrometheus("app", prommetrics.GetGinCusMetrics("Api"))
p.SetListenAddress(fmt.Sprintf(":%d", proPort))
p.Use(router)
go func() {
p := ginprom.NewPrometheus("app", prommetrics.GetGinCusMetrics("Api"))
p.SetListenAddress(fmt.Sprintf(":%d", proPort))
if err = p.Use(router); err != nil && err != http.ErrServerClosed {
netErr = errs.Wrap(err, fmt.Sprintf("prometheus start err: %d", proPort))
netDone <- struct{}{}
}
}()

}

var address string
Expand All @@ -92,24 +101,31 @@ func run(port int, proPort int) error {
}

server := http.Server{Addr: address, Handler: router}

go func() {
err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
os.Exit(1)
netErr = errs.Wrap(err, fmt.Sprintf("api start err: %s", server.Addr))
netDone <- struct{}{}

}
}()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-sigs
signal.Notify(sigs, syscall.SIGTERM)

ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

// graceful shutdown operation.
if err := server.Shutdown(ctx); err != nil {
return err
select {
case <-sigs:
util.SIGUSR1Exit()
err := server.Shutdown(ctx)
if err != nil {
return errs.Wrap(err, "shutdown err")
}
case <-netDone:
close(netDone)
return netErr
}

return nil
}
7 changes: 2 additions & 5 deletions cmd/openim-cmdutils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand Down Expand Up @@ -57,7 +55,6 @@ func main() {
// openIM clear msg --clearAll
msgUtilsCmd.AddCommand(&getCmd.Command, &fixCmd.Command, &clearCmd.Command)
if err := msgUtilsCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-crontask/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/tools"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
cronTaskCmd := cmd.NewCronTaskCmd()
if err := cronTaskCmd.Exec(tools.StartTask); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-msggateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -28,7 +26,6 @@ func main() {
msgGatewayCmd.AddPrometheusPortFlag()

if err := msgGatewayCmd.Exec(); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-msgtransfer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
msgTransferCmd := cmd.NewMsgTransferCmd()
msgTransferCmd.AddPrometheusPortFlag()
msgTransferCmd.AddTransferProgressFlag()
if err := msgTransferCmd.Exec(); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/push"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := pushCmd.StartSvr(config.Config.RpcRegisterName.OpenImPushName, push.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
8 changes: 3 additions & 5 deletions cmd/openim-rpc/openim-rpc-auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/auth"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,7 @@ func main() {
panic(err.Error())
}
if err := authCmd.StartSvr(config.Config.RpcRegisterName.OpenImAuthName, auth.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}

}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-conversation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/conversation"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImConversationName, conversation.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-friend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/friend"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImFriendName, friend.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-group/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/group"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImGroupName, group.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-msg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/msg"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImMsgName, msg.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-third/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/third"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImThirdName, third.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
7 changes: 2 additions & 5 deletions cmd/openim-rpc/openim-rpc-user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package main

import (
"fmt"
"os"

"github.com/openimsdk/open-im-server/v3/internal/rpc/user"
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
)

func main() {
Expand All @@ -31,7 +29,6 @@ func main() {
panic(err.Error())
}
if err := rpcCmd.StartSvr(config.Config.RpcRegisterName.OpenImUserName, user.Start); err != nil {
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
util.ExitWithError(err)
}
}
21 changes: 5 additions & 16 deletions internal/msggateway/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
"fmt"
"time"

"github.com/OpenIMSDK/tools/utils"
"golang.org/x/sync/errgroup"

"github.com/openimsdk/open-im-server/v3/pkg/common/config"
)

Expand All @@ -46,20 +43,12 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error {
}

hubServer := NewServer(rpcPort, prometheusPort, longServer)

wg := errgroup.Group{}
wg.Go(func() error {
netDone := make(chan error)
go func() {
err = hubServer.Start()
if err != nil {
return utils.Wrap1(err)
netDone <- err
}
return err
})

wg.Go(func() error {
return hubServer.LongConnServer.Run()
})

err = wg.Wait()
return err
}()
return hubServer.LongConnServer.Run(netDone)
}
Loading

0 comments on commit c754ec6

Please sign in to comment.