Browse Source

Dashboard: Stop|Resume worker pools work at runtime

master
parent
commit
c91986d42d
  1. 34
      src/dashboard/dashboard.go
  2. 43
      src/dashboard/res/index.html
  3. 2
      src/main.go

34
src/dashboard/dashboard.go

@ -43,6 +43,10 @@ type PageData struct {
Stats worker.Statistics
}
type PoolStop struct {
Stop bool `json:"stop"`
}
func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboard {
mux := http.NewServeMux()
res, err := fs.Sub(resFS, "res")
@ -52,6 +56,7 @@ func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboa
}
mux.Handle("/static/", http.FileServer(http.FS(res)))
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
template, err := template.ParseFS(res, "*.html")
if err != nil {
@ -62,6 +67,35 @@ func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboa
template.ExecuteTemplate(w, "index.html", nil)
})
mux.HandleFunc("/stop", func(w http.ResponseWriter, req *http.Request) {
var stop PoolStop
requestBody, err := io.ReadAll(req.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
logger.Error("Failed to read stop|resume signal from dashboard request: %s", err)
return
}
defer req.Body.Close()
err = json.Unmarshal(requestBody, &stop)
if err != nil {
http.Error(w, "Failed to unmarshal stop|resume signal", http.StatusInternalServerError)
logger.Error("Failed to unmarshal stop|resume signal from dashboard UI: %s", err)
return
}
if stop.Stop {
// stop worker pool
pool.Stop()
logger.Info("Stopped worker pool via request from dashboard")
} else {
// resume work
pool.Work()
logger.Info("Resumed work via request from dashboard")
}
})
mux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) {
jsonStats, err := json.MarshalIndent(pool.Stats, "", " ")
if err != nil {

43
src/dashboard/res/index.html

@ -68,6 +68,9 @@
</li>
</ol>
</div>
<button class="btn btn-primary" id="btn_stop">Stop</button>
<button class="btn btn-primary" id="btn_resume" disabled>Resume</button>
</div>
<div style="height: 3rem;"></div>
@ -117,6 +120,44 @@
let applyConfButton = document.getElementById("config_apply_button");
let confQuery = document.getElementById("conf_query");
let confIsRegexp = document.getElementById("conf_is_regexp");
let buttonStop = document.getElementById("btn_stop");
let buttonResume = document.getElementById("btn_resume");
buttonStop.addEventListener("click", (event) => {
buttonStop.disabled = true;
buttonResume.disabled = false;
// stop worker pool
let signal = {
"stop": true,
};
fetch("/stop", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(signal),
});
});
buttonResume.addEventListener("click", (event) => {
buttonResume.disabled = true;
buttonStop.disabled = false;
// resume worker pool's work
let signal = {
"stop": false,
};
fetch("/stop", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(signal),
});
});
applyConfButton.addEventListener("click", (event) => {
let query = String(confQuery.value);
@ -139,8 +180,6 @@
},
};
console.log(newConf);
fetch("/conf", {
method: "POST",
headers: {

2
src/main.go

@ -39,7 +39,7 @@ import (
"unbewohnte/wecr/worker"
)
const version = "v0.3.3"
const version = "v0.3.4"
const (
configFilename string = "conf.json"

Loading…
Cancel
Save