Added background jobs UI
This commit is contained in:
@ -1,17 +1,39 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
var running = map[string]bool{}
|
||||
var lastRun = map[string]string{}
|
||||
var jobInfo = map[string]*utils.JobInfo{
|
||||
"anime-ratings": &utils.JobInfo{
|
||||
Name: "anime-ratings",
|
||||
},
|
||||
"twist": &utils.JobInfo{
|
||||
Name: "twist",
|
||||
},
|
||||
"refresh-osu": &utils.JobInfo{
|
||||
Name: "refresh-osu",
|
||||
},
|
||||
}
|
||||
|
||||
var jobLogs = []string{}
|
||||
|
||||
// Overview shows all background jobs.
|
||||
func Overview(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
jobs := []*utils.JobInfo{}
|
||||
|
||||
return ctx.HTML(components.EditorJobs(running, lastRun, ctx.URI(), user))
|
||||
for _, job := range jobInfo {
|
||||
jobs = append(jobs, job)
|
||||
}
|
||||
|
||||
sort.Slice(jobs, func(i, j int) bool {
|
||||
return jobs[i].Name < jobs[j].Name
|
||||
})
|
||||
|
||||
return ctx.HTML(components.EditorJobs(jobs, jobLogs, ctx.URI(), user))
|
||||
}
|
||||
|
@ -1,9 +1,18 @@
|
||||
component EditorJobs(running bool, url string, user *arn.User)
|
||||
component EditorJobs(jobs []*utils.JobInfo, jobLogs []string, url string, user *arn.User)
|
||||
EditorTabs(url, user)
|
||||
|
||||
h1.editor-list-page-title.mountable Background jobs
|
||||
h1.mountable Background jobs
|
||||
|
||||
.buttons
|
||||
button.mountable.action(data-action="startJob", data-trigger="click", data-job="twist", disabled=running)
|
||||
Icon("rocket")
|
||||
span twist
|
||||
each job in jobs
|
||||
button.background-job.mountable.action(data-action="startJob", data-trigger="click", data-job=job.Name, data-running=job.IsRunning())
|
||||
if job.IsRunning()
|
||||
Icon("hourglass-start")
|
||||
else
|
||||
Icon("rocket")
|
||||
|
||||
span= job.Name
|
||||
|
||||
footer.footer
|
||||
for i := 0; i < len(jobLogs); i++
|
||||
p.mountable= jobLogs[len(jobLogs) - 1 - i]
|
5
pages/editor/jobs/jobs.scarlet
Normal file
5
pages/editor/jobs/jobs.scarlet
Normal file
@ -0,0 +1,5 @@
|
||||
.background-job
|
||||
[data-running="true"]
|
||||
&.mounted
|
||||
opacity 0.5 !important
|
||||
pointer-events none
|
35
pages/editor/jobs/start.go
Normal file
35
pages/editor/jobs/start.go
Normal file
@ -0,0 +1,35 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/animenotifier/arn"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
// Start will start the specified background job.
|
||||
func Start(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
if user == nil || (user.Role != "editor" && user.Role != "admin") {
|
||||
return ctx.Error(http.StatusUnauthorized, "Not authorized", nil)
|
||||
}
|
||||
|
||||
jobName := ctx.Get("job")
|
||||
job := jobInfo[jobName]
|
||||
|
||||
if job == nil {
|
||||
return ctx.Error(http.StatusBadRequest, "Job not available", nil)
|
||||
}
|
||||
|
||||
if job.IsRunning() {
|
||||
return ctx.Error(http.StatusBadRequest, "Job is currently running!", nil)
|
||||
}
|
||||
|
||||
job.Start()
|
||||
jobLogs = append(jobLogs, user.Nick+" started "+job.Name+" job ("+arn.DateTimeUTC()+").")
|
||||
|
||||
return "ok"
|
||||
}
|
Reference in New Issue
Block a user