47 lines
973 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
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-04-28 19:17:44 +00:00
"github.com/aerogo/aero"
)
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.
2019-06-01 04:55:49 +00:00
func Start(ctx aero.Context) error {
2018-04-28 20:10:47 +00:00
jobStartMutex.Lock()
defer jobStartMutex.Unlock()
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2018-04-28 19:17:44 +00:00
if user == nil || (user.Role != "editor" && user.Role != "admin") {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not authorized")
2018-04-28 19:17:44 +00:00
}
jobName := ctx.Get("job")
job := jobInfo[jobName]
if job == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusBadRequest, "Job not available")
2018-04-28 19:17:44 +00:00
}
if job.IsRunning() {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusBadRequest, "Job is currently running!")
2018-04-28 19:17:44 +00:00
}
2019-06-05 06:45:54 +00:00
err := job.Start()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Job could not be started!", err)
}
2018-04-28 19:17:44 +00:00
jobLogs = append(jobLogs, user.Nick+" started "+job.Name+" job ("+arn.DateTimeUTC()+").")
2019-06-01 04:55:49 +00:00
return nil
2018-04-28 19:17:44 +00:00
}