Skip to content

Commit

Permalink
admin: added method to rolling restart all shards
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Jul 24, 2020
1 parent 9e2e1dc commit 852eed0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions admin/assets/bot_admin_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ <h2>YAGPDB internal admin panel</h2>
{{template "cp_alerts" .}}

<a href="/admin/config" class="btn btn-sm btn-primary">Internal bot config</a>
<form method="POST" action="/admin/reconnect_all">
<button type="submit" class="btn btn-danger" value="Reconnect all shards">Reconnect all shards</button>
</form>

{{range .Hosts}}
<h3>{{.Name}}</h3>
Expand Down
27 changes: 27 additions & 0 deletions admin/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"sort"
"strconv"
"strings"
"time"

"emperror.dev/errors"
"github.com/jonas747/dshardorchestrator/v2/orchestrator/rest"
"github.com/jonas747/yagpdb/bot/botrest"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/common/internalapi"
Expand Down Expand Up @@ -54,6 +56,8 @@ func (p *Plugin) InitWeb() {
mux.Handle(pat.Get("/host/:host/pid/:pid/shard_sessions"), p.ProxyGetInternalAPI("/shard_sessions"))
mux.Handle(pat.Post("/host/:host/pid/:pid/shard/:shardid/reconnect"), http.HandlerFunc(p.handleReconnectShard))

mux.Handle(pat.Post("/reconnect_all"), http.HandlerFunc(p.handleReconnectAll))

getConfigHandler := web.ControllerHandler(p.handleGetConfig, "bot_admin_config")
mux.Handle(pat.Get("/config"), getConfigHandler)
mux.Handle(pat.Post("/config/edit/:key"), web.ControllerPostHandler(p.handleEditConfig, getConfigHandler, nil))
Expand Down Expand Up @@ -345,3 +349,26 @@ func (p *Plugin) handleReconnectShard(w http.ResponseWriter, r *http.Request) {

io.Copy(w, resp.Body)
}

func (p *Plugin) handleReconnectAll(w http.ResponseWriter, r *http.Request) {

totalShards, err := common.ServicePoller.GetShardCount()
if err != nil {
logger.WithError(err).Error("failed getting total shard count")
w.Write([]byte("failed"))
return
}

for i := 0; i < totalShards; i++ {
err = botrest.SendReconnectShard(i, true)
if err != nil {
fmt.Fprintf(w, "Failed restarting %d\n", i)
logger.WithError(err).Error("failed restarting shard")
} else {
fmt.Fprintf(w, "Restarted %d", i)
logger.Infof("restarted shard %d", i)
}

time.Sleep(time.Second * 5)
}
}

0 comments on commit 852eed0

Please sign in to comment.