调度中心嵌入cms
This commit is contained in:
@@ -51,6 +51,7 @@ type executionItem struct {
|
||||
ID string `json:"id"`
|
||||
JobName string `json:"jobName"`
|
||||
TriggerType string `json:"triggerType"`
|
||||
ScheduleType string `json:"scheduleType,omitempty"`
|
||||
Status string `json:"status"`
|
||||
StartedAt string `json:"startedAt,omitempty"`
|
||||
FinishedAt string `json:"finishedAt,omitempty"`
|
||||
@@ -61,6 +62,7 @@ type executionDetailItem struct {
|
||||
ID string `json:"id"`
|
||||
JobConfigID string `json:"jobConfigId"`
|
||||
TriggerType string `json:"triggerType"`
|
||||
ScheduleType string `json:"scheduleType,omitempty"`
|
||||
Status string `json:"status"`
|
||||
ParamsSnapshot string `json:"paramsSnapshot"`
|
||||
StartedAt string `json:"startedAt,omitempty"`
|
||||
@@ -133,6 +135,12 @@ type dashboardOverviewResponse struct {
|
||||
RecentExecutions []executionItem `json:"recentExecutions"`
|
||||
}
|
||||
|
||||
type globalConfigItem struct {
|
||||
Config string `json:"config"`
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||
}
|
||||
|
||||
type JobConfigLister interface {
|
||||
List(ctx context.Context) ([]model.JobConfig, error)
|
||||
GetByID(ctx context.Context, id primitive.ObjectID) (*model.JobConfig, error)
|
||||
@@ -173,14 +181,20 @@ type SystemConfigStore interface {
|
||||
ToggleEnabled(ctx context.Context, id primitive.ObjectID, enabled bool) error
|
||||
}
|
||||
|
||||
type GlobalConfigStore interface {
|
||||
Get(ctx context.Context, key string) (*model.GlobalConfig, error)
|
||||
Upsert(ctx context.Context, item *model.GlobalConfig) error
|
||||
}
|
||||
|
||||
type RouterDeps struct {
|
||||
Registry *jobdef.Registry
|
||||
JobConfigStore JobConfigLister
|
||||
ExecutionStore JobExecutionLister
|
||||
ProfileStore AdminProfileStore
|
||||
AdminUserStore AdminUserStore
|
||||
ConfigStore SystemConfigStore
|
||||
Executor *executor.Service
|
||||
Registry *jobdef.Registry
|
||||
JobConfigStore JobConfigLister
|
||||
ExecutionStore JobExecutionLister
|
||||
ProfileStore AdminProfileStore
|
||||
AdminUserStore AdminUserStore
|
||||
ConfigStore SystemConfigStore
|
||||
GlobalConfigStore GlobalConfigStore
|
||||
Executor *executor.Service
|
||||
}
|
||||
|
||||
type upsertJobRequest struct {
|
||||
@@ -243,6 +257,12 @@ type toggleSystemConfigRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type updateGlobalConfigRequest struct {
|
||||
Config json.RawMessage `json:"config" binding:"required"`
|
||||
}
|
||||
|
||||
const schedulerGlobalConfigKey = "scheduler"
|
||||
|
||||
func NewRouter(deps RouterDeps) *gin.Engine {
|
||||
router := gin.Default()
|
||||
router.Use(func(c *gin.Context) {
|
||||
@@ -579,6 +599,35 @@ func NewRouter(deps RouterDeps) *gin.Engine {
|
||||
"errorMessage": execRecord.ErrorMessage,
|
||||
})
|
||||
})
|
||||
router.GET("/admin/scheduler/global-config", func(c *gin.Context) {
|
||||
item, err := deps.GlobalConfigStore.Get(c.Request.Context(), schedulerGlobalConfigKey)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toGlobalConfigItem(*item))
|
||||
})
|
||||
router.PUT("/admin/scheduler/global-config", func(c *gin.Context) {
|
||||
var req updateGlobalConfigRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if !json.Valid(req.Config) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errInvalidJSON("config").Error()})
|
||||
return
|
||||
}
|
||||
item := &model.GlobalConfig{
|
||||
Key: schedulerGlobalConfigKey,
|
||||
Config: string(req.Config),
|
||||
UpdatedBy: "admin",
|
||||
}
|
||||
if err := deps.GlobalConfigStore.Upsert(c.Request.Context(), item); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toGlobalConfigItem(*item))
|
||||
})
|
||||
router.GET("/admin/scheduler/executions", func(c *gin.Context) {
|
||||
jobs, err := deps.JobConfigStore.List(c.Request.Context())
|
||||
if err != nil {
|
||||
@@ -592,16 +641,20 @@ func NewRouter(deps RouterDeps) *gin.Engine {
|
||||
}
|
||||
|
||||
jobNameMap := make(map[string]string, len(jobs))
|
||||
jobScheduleTypeMap := make(map[string]string, len(jobs))
|
||||
for _, item := range jobs {
|
||||
jobNameMap[item.ID.Hex()] = item.Name
|
||||
jobScheduleTypeMap[item.ID.Hex()] = item.ScheduleType
|
||||
}
|
||||
|
||||
responseItems := make([]executionItem, 0, len(items))
|
||||
for _, item := range items {
|
||||
jobID := item.JobConfigID.Hex()
|
||||
responseItems = append(responseItems, executionItem{
|
||||
ID: objectIDToString(item.ID),
|
||||
JobName: jobDisplayName(jobNameMap, item.JobConfigID),
|
||||
TriggerType: item.TriggerType,
|
||||
ScheduleType: jobScheduleTypeMap[jobID],
|
||||
Status: item.Status,
|
||||
StartedAt: formatTime(item.StartedAt),
|
||||
FinishedAt: formatTime(item.FinishedAt),
|
||||
@@ -631,7 +684,17 @@ func NewRouter(deps RouterDeps) *gin.Engine {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, toExecutionDetailItem(*item))
|
||||
detail := toExecutionDetailItem(*item)
|
||||
job, err := deps.JobConfigStore.GetByID(c.Request.Context(), item.JobConfigID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if job != nil {
|
||||
detail.ScheduleType = job.ScheduleType
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, detail)
|
||||
})
|
||||
router.GET("/admin/scheduler/executions/:id/logfile", func(c *gin.Context) {
|
||||
id, err := primitive.ObjectIDFromHex(c.Param("id"))
|
||||
@@ -900,6 +963,14 @@ func toExecutionDetailItem(item model.JobExecution) executionDetailItem {
|
||||
}
|
||||
}
|
||||
|
||||
func toGlobalConfigItem(item model.GlobalConfig) globalConfigItem {
|
||||
return globalConfigItem{
|
||||
Config: item.Config,
|
||||
UpdatedBy: item.UpdatedBy,
|
||||
UpdatedAt: formatTime(&item.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toProfileItem(item model.AdminProfile) profileItem {
|
||||
return profileItem{
|
||||
ID: objectIDToString(item.ID),
|
||||
|
||||
Reference in New Issue
Block a user