From 41e973a4d3d4686b0e58535335f0642649811d71 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sat, 21 Dec 2024 15:33:07 +0800 Subject: [PATCH] fix: misaligned nodes when running tasks from a schedule through enhancement by adding new route and handler for running schedules - Introduced a new POST endpoint '/:id/run' in the router to trigger the execution of a schedule. - Implemented the PostScheduleRun function to handle the execution logic, including validation of input parameters and scheduling tasks. - Refactored the PostSpiderRun function to streamline the scheduling process by directly calling the admin service. - Updated the FsFileInfo struct to clarify the children field description. - Modified the Task model to make NodeIds field optional in JSON serialization. --- core/controllers/router.go | 5 ++++ core/controllers/schedule.go | 48 ++++++++++++++++++++++++++++++++++++ core/controllers/spider.go | 6 ++--- core/controllers/task.go | 6 +++++ core/entity/fs_file_info.go | 2 +- core/models/models/task.go | 2 +- 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/core/controllers/router.go b/core/controllers/router.go index f02299f5..ce4bf778 100644 --- a/core/controllers/router.go +++ b/core/controllers/router.go @@ -101,6 +101,11 @@ func InitRoutes(app *gin.Engine) (err error) { Path: "/:id/disable", HandlerFunc: PostScheduleDisable, }, + { + Method: http.MethodPost, + Path: "/:id/run", + HandlerFunc: PostScheduleRun, + }, }...)) RegisterController(groups.AuthGroup, "/spiders", NewController[models.Spider]([]Action{ { diff --git a/core/controllers/schedule.go b/core/controllers/schedule.go index 74b37aac..a3a0056d 100644 --- a/core/controllers/schedule.go +++ b/core/controllers/schedule.go @@ -2,9 +2,11 @@ package controllers import ( errors2 "errors" + "github.com/crawlab-team/crawlab/core/interfaces" "github.com/crawlab-team/crawlab/core/models/models" "github.com/crawlab-team/crawlab/core/models/service" "github.com/crawlab-team/crawlab/core/schedule" + "github.com/crawlab-team/crawlab/core/spider/admin" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -116,3 +118,49 @@ func postScheduleEnableDisableFunc(isEnable bool) func(c *gin.Context) { HandleSuccess(c) } } + +func PostScheduleRun(c *gin.Context) { + id, err := primitive.ObjectIDFromHex(c.Param("id")) + if err != nil { + HandleErrorBadRequest(c, err) + return + } + + // options + var opts interfaces.SpiderRunOptions + if err := c.ShouldBindJSON(&opts); err != nil { + HandleErrorInternalServerError(c, err) + return + } + if opts.ScheduleId.IsZero() { + opts.ScheduleId = id + } + + // schedule + sch, err := service.NewModelService[models.Schedule]().GetById(id) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + + // spider + s, err := service.NewModelService[models.Spider]().GetById(sch.SpiderId) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + + // user + if u := GetUserFromContext(c); u != nil { + opts.UserId = u.GetId() + } + + // schedule tasks + taskIds, err := admin.GetSpiderAdminService().Schedule(s.Id, &opts) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + + HandleSuccessWithData(c, taskIds) +} diff --git a/core/controllers/spider.go b/core/controllers/spider.go index 163e6c53..36746166 100644 --- a/core/controllers/spider.go +++ b/core/controllers/spider.go @@ -660,10 +660,8 @@ func PostSpiderRun(c *gin.Context) { opts.UserId = u.GetId() } - adminSvc := admin.GetSpiderAdminService() - - // schedule - taskIds, err := adminSvc.Schedule(id, &opts) + // schedule tasks + taskIds, err := admin.GetSpiderAdminService().Schedule(id, &opts) if err != nil { HandleErrorInternalServerError(c, err) return diff --git a/core/controllers/task.go b/core/controllers/task.go index 1d2964f6..7032a476 100644 --- a/core/controllers/task.go +++ b/core/controllers/task.go @@ -339,6 +339,12 @@ func PostTaskRestart(c *gin.Context) { Priority: t.Priority, } + // normalize options for tasks with assigned node + if !t.NodeId.IsZero() { + opts.NodeIds = []primitive.ObjectID{t.NodeId} + opts.Mode = constants.RunTypeSelectedNodes + } + // user if u := GetUserFromContext(c); u != nil { opts.UserId = u.Id diff --git a/core/entity/fs_file_info.go b/core/entity/fs_file_info.go index 826ec131..c2adde5f 100644 --- a/core/entity/fs_file_info.go +++ b/core/entity/fs_file_info.go @@ -13,7 +13,7 @@ type FsFileInfo struct { Extension string `json:"extension"` // file extension IsDir bool `json:"is_dir"` // whether it is directory FileSize int64 `json:"file_size"` // file size (bytes) - Children []interfaces.FsFileInfo `json:"children"` // children for sub-directory + Children []interfaces.FsFileInfo `json:"children"` // children for subdirectory ModTime time.Time `json:"mod_time"` // modification time Mode os.FileMode `json:"mode"` // file mode Hash string `json:"hash"` // file hash diff --git a/core/models/models/task.go b/core/models/models/task.go index 9fa2083e..69b9c78b 100644 --- a/core/models/models/task.go +++ b/core/models/models/task.go @@ -17,8 +17,8 @@ type Task struct { ScheduleId primitive.ObjectID `json:"schedule_id" bson:"schedule_id"` Type string `json:"type" bson:"type"` Mode string `json:"mode" bson:"mode"` - NodeIds []primitive.ObjectID `json:"node_ids" bson:"node_ids"` Priority int `json:"priority" bson:"priority"` + NodeIds []primitive.ObjectID `json:"node_ids,omitempty" bson:"-"` Stat *TaskStat `json:"stat,omitempty" bson:"-"` Spider *Spider `json:"spider,omitempty" bson:"-"` Schedule *Schedule `json:"schedule,omitempty" bson:"-"`