mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
// Package handler — scheduled jobs admin page.
|
|
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/service"
|
|
)
|
|
|
|
func schedulerStatusHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"jobs": svc.Scheduler.Status()})
|
|
}
|
|
}
|
|
|
|
func schedulerRunHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
name := c.Param("name")
|
|
if !triggerSchedulerJob(c, svc, name) {
|
|
return
|
|
}
|
|
c.JSON(http.StatusAccepted, gin.H{"ok": true, "message": "任务已在后台触发"})
|
|
}
|
|
}
|
|
|
|
func triggerSchedulerJob(c *gin.Context, svc *service.Container, name string) bool {
|
|
if err := svc.Scheduler.RunNowAsync(c.Request.Context(), name); err != nil {
|
|
switch {
|
|
case errors.Is(err, service.ErrSchedulerJobAlreadyRunning):
|
|
c.JSON(http.StatusConflict, gin.H{"error": "任务正在运行,请稍后到实时任务查看进度"})
|
|
case errors.Is(err, service.ErrSchedulerJobNotFound):
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "任务不存在"})
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
}
|