43 lines
902 B
Go
Raw Normal View History

2018-04-28 19:17:44 +00:00
package jobs
import (
"net/http"
2018-04-28 20:10:47 +00:00
"sync"
2018-04-28 19:17:44 +00:00
"github.com/animenotifier/arn"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils"
)
2018-04-28 20:10:47 +00:00
// Only allow one job to be started at a time
var jobStartMutex sync.Mutex
2018-04-28 19:17:44 +00:00
// Start will start the specified background job.
func Start(ctx *aero.Context) string {
2018-04-28 20:10:47 +00:00
jobStartMutex.Lock()
defer jobStartMutex.Unlock()
2018-04-28 19:17:44 +00:00
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"
}