Skip to content

Commit

Permalink
Added an option to not remove admin executions
Browse files Browse the repository at this point in the history
Signed-off-by: Florent Poinsard <[email protected]>
  • Loading branch information
frouioui committed Nov 14, 2024
1 parent 3d78057 commit 5ea420d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
9 changes: 7 additions & 2 deletions go/admin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -66,7 +67,8 @@ type (
}

clearQueueRequest struct {
Auth string `json:"auth"`
Auth string `json:"auth"`
RemoveAdminExecutions bool `json:"remove_admin_executions"`
}
)

Expand Down Expand Up @@ -332,9 +334,12 @@ func (a *Admin) handleClearQueue(c *gin.Context) {
}

requestPayload := clearQueueRequest{
Auth: encryptedToken,
Auth: encryptedToken,
RemoveAdminExecutions: c.PostForm("remove_admin") == "true",
}

log.Println(requestPayload)

jsonData, err := json.Marshal(requestPayload)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go/admin/templates/add_new_executions.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<form>
<label class="text-lg font-semibold mt-4">Information</label>
<div>
<label class="label">Source (execution's trigger/reason)</label>
<label class="label">Source (leave it to 'admin' unless you don't want these executions to be treated as admin executions)</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
type="text"
Expand Down
19 changes: 14 additions & 5 deletions go/admin/templates/clear_queue.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<div id="content">
<form>
<label class="text-lg font-semibold mt-4">Settings</label>
<div class="flex flex-row gap-4">
<div class="flex flex-col">
<label>
<input class="accent-orange-500" type="checkbox" name="remove_admin" value="true" />
Remove admin executions
</label>
</div>
</div>
<br>
<button
hx-post="/admin/executions/clear"
hx-target="#response"
class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
type="submit"
hx-post="/admin/executions/clear"
hx-target="#response"
class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
type="submit"
>
Remove every pending executions in the execution queue
</button>
</form>
<div id="response" class="mt-4"></div>
</div>

<script>
document.body.addEventListener("htmx:responseError", function(e) {
error = e.detail.xhr.response;
Expand Down
5 changes: 3 additions & 2 deletions go/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,8 @@ func (s *Server) addExecutions(c *gin.Context) {

func (s *Server) clearExecutionQueue(c *gin.Context) {
var req struct {
Auth string `json:"auth"`
Auth string `json:"auth"`
RemoveAdminExecutions bool `json:"remove_admin_executions"`
}

if err := c.ShouldBindJSON(&req); err != nil {
Expand All @@ -620,7 +621,7 @@ func (s *Server) clearExecutionQueue(c *gin.Context) {
return
}

s.clearQueue()
s.clearQueue(req.RemoveAdminExecutions)

c.JSON(http.StatusAccepted, "")
}
Expand Down
5 changes: 4 additions & 1 deletion go/server/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,16 @@ func (s *Server) appendToQueue(execElements []*executionQueueElement) {
}
}

func (s *Server) clearQueue() {
func (s *Server) clearQueue(removeAdmin bool) {
mtx.Lock()
defer mtx.Unlock()
for id, e := range queue {
if e.Executing {
continue
}
if id.Source == "admin" && !removeAdmin {
continue
}
delete(queue, id)
}
}

0 comments on commit 5ea420d

Please sign in to comment.