Skip to content

Commit

Permalink
Added support for retries on java and bedrock status
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Jun 28, 2020
1 parent e925388 commit 31d22f7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
33 changes: 24 additions & 9 deletions bedrock_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"log"
"net"
"strconv"
"time"
)

type statusBedrockCmd struct {
Host string `default:"localhost"`
Port int `default:"19132"`

RetryInterval time.Duration `usage:"if retry-limit is non-zero, status will be retried at this interval" default:"10s"`
RetryLimit int `usage:"if non-zero, failed status will be retried this many times before exiting"`
}

func (c *statusBedrockCmd) Name() string {
Expand All @@ -38,16 +42,27 @@ func (c *statusBedrockCmd) SetFlags(flags *flag.FlagSet) {

func (c *statusBedrockCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
address := net.JoinHostPort(c.Host, strconv.Itoa(c.Port))

info, err := PingBedrockServer(address)
if err != nil {
log.Fatal(err)
return subcommands.ExitFailure
if c.RetryInterval <= 0 {
c.RetryInterval = 1 * time.Second
}

fmt.Printf("%s : version=%s online=%d max=%d",
address,
info.Version, info.Players, info.MaxPlayers)
for {
info, err := PingBedrockServer(address)
if err != nil {
if c.RetryLimit > 0 {
c.RetryLimit--
time.Sleep(c.RetryInterval)
continue
}
log.Fatal(err)
return subcommands.ExitFailure
}

fmt.Printf("%s : version=%s online=%d max=%d",
address,
info.Version, info.Players, info.MaxPlayers)

return subcommands.ExitSuccess
}

return subcommands.ExitSuccess
}
51 changes: 35 additions & 16 deletions java_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"github.com/itzg/go-flagsfiller"
"log"
"os"
"time"
)

type statusCmd struct {
Host string `default:"localhost" usage:"hostname of the Minecraft server" env:"MC_HOST"`
Port int `default:"25565" usage:"port of the Minecraft server" env:"MC_PORT"`

RetryInterval time.Duration `usage:"if retry-limit is non-zero, status will be retried at this interval" default:"10s"`
RetryLimit int `usage:"if non-zero, failed status will be retried this many times before exiting"`
}

func (c *statusCmd) Name() string {
Expand All @@ -38,24 +42,39 @@ func (c *statusCmd) SetFlags(flags *flag.FlagSet) {

func (c *statusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
pinger := mcpinger.New(c.Host, uint16(c.Port))

info, err := pinger.Ping()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to ping %s:%d : %s", c.Host, c.Port, err)
return subcommands.ExitFailure
if c.RetryInterval <= 0 {
c.RetryInterval = 1 * time.Second
}

// While server is starting up it will answer pings, but respond with empty JSON object.
// As such, we'll sanity check the max players value to see if a zero-value has been
// provided for info.
if info.Players.Max == 0 {
_, _ = fmt.Fprintf(os.Stderr, "server not ready %s:%d", c.Host, c.Port)
return subcommands.ExitFailure
}
for {
info, err := pinger.Ping()
if err != nil {
if c.RetryLimit > 0 {
c.RetryLimit--
time.Sleep(c.RetryInterval)
continue
}
_, _ = fmt.Fprintf(os.Stderr, "failed to ping %s:%d : %s", c.Host, c.Port, err)
return subcommands.ExitFailure
}

// While server is starting up it will answer pings, but respond with empty JSON object.
// As such, we'll sanity check the max players value to see if a zero-value has been
// provided for info.
if info.Players.Max == 0 {
if c.RetryLimit > 0 {
c.RetryLimit--
time.Sleep(c.RetryInterval)
continue
}
_, _ = fmt.Fprintf(os.Stderr, "server not ready %s:%d", c.Host, c.Port)
return subcommands.ExitFailure
}

fmt.Printf("%s:%d : version=%s online=%d max=%d motd='%s'",
c.Host, c.Port,
info.Version.Name, info.Players.Online, info.Players.Max, info.Description.Text)
fmt.Printf("%s:%d : version=%s online=%d max=%d motd='%s'",
c.Host, c.Port,
info.Version.Name, info.Players.Online, info.Players.Max, info.Description.Text)

return subcommands.ExitSuccess
return subcommands.ExitSuccess
}
}

0 comments on commit 31d22f7

Please sign in to comment.