Skip to content

Commit

Permalink
Dashboard: Stop|Resume worker pools work at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Unbewohnte committed Feb 14, 2023
1 parent c2ec207 commit c91986d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
43 changes: 41 additions & 2 deletions src/dashboard/res/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h2>Statistics</h2>
</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>
Expand Down Expand Up @@ -117,6 +120,44 @@ <h2>Configuration</h2>
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);
Expand All @@ -139,8 +180,6 @@ <h2>Configuration</h2>
},
};

console.log(newConf);

fetch("/conf", {
method: "POST",
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"unbewohnte/wecr/worker"
)

const version = "v0.3.3"
const version = "v0.3.4"

const (
configFilename string = "conf.json"
Expand Down

0 comments on commit c91986d

Please sign in to comment.