From 91aead8b81f862fe789f4b4abf03ad2648e5d961 Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Tue, 21 Nov 2023 21:42:58 +0800 Subject: [PATCH] optimize status --- pkg/context/job.go | 4 +- pkg/item.go | 12 ++-- pkg/job.go | 23 ++++--- pkg/request.go | 8 +-- pkg/spider/base_spider.go | 5 +- pkg/spider/job.go | 22 ++++-- pkg/statistics/job/job.go | 4 +- pkg/statistics/statistics.go | 2 +- pkg/task.go | 8 +-- web/ui/src/App.vue | 4 +- web/ui/src/stores/jobs.js | 24 ++++++- web/ui/src/stores/nodes.js | 18 +++++ web/ui/src/stores/records.js | 21 ++++++ web/ui/src/stores/requests.js | 20 ++++++ web/ui/src/stores/spiders.js | 18 +++++ web/ui/src/stores/tasks.js | 23 +++++-- web/ui/src/views/JobsView.vue | 41 ++++------- web/ui/src/views/NodesView.vue | 24 +------ web/ui/src/views/RecordsView.vue | 109 ++++++++++++++++++++++++++++-- web/ui/src/views/RequestsView.vue | 44 +++++++++++- web/ui/src/views/SpidersView.vue | 56 ++++----------- web/ui/src/views/TasksView.vue | 32 ++++----- 22 files changed, 361 insertions(+), 161 deletions(-) diff --git a/pkg/context/job.go b/pkg/context/job.go index f3641e8..e2d4ba6 100644 --- a/pkg/context/job.go +++ b/pkg/context/job.go @@ -55,7 +55,9 @@ func (c *Job) WithStatus(status pkg.JobStatus) pkg.ContextJob { switch status { case pkg.JobStatusRunning: c.withStartTime(t) - case pkg.JobStatusStopped: + case pkg.JobStatusSuccess: + c.withStopTime(t) + case pkg.JobStatusFailure: c.withStopTime(t) } diff --git a/pkg/item.go b/pkg/item.go index b1d289f..5166413 100644 --- a/pkg/item.go +++ b/pkg/item.go @@ -190,15 +190,15 @@ const ( ItemStatusFailure ) -func (s *ItemStatus) String() string { - switch *s { - case 1: +func (s ItemStatus) String() string { + switch s { + case ItemStatusPending: return "pending" - case 2: + case ItemStatusRunning: return "running" - case 3: + case ItemStatusSuccess: return "success" - case 4: + case ItemStatusFailure: return "failure" default: return "unknown" diff --git a/pkg/job.go b/pkg/job.go index c1daa39..9d25632 100644 --- a/pkg/job.go +++ b/pkg/job.go @@ -14,23 +14,26 @@ const ( JobStatusRunning JobStatusIdle JobStatusStopping - JobStatusStopped + JobStatusSuccess + JobStatusFailure ) -func (s *JobStatus) String() string { - switch *s { - case 1: +func (s JobStatus) String() string { + switch s { + case JobStatusReady: return "ready" - case 2: + case JobStatusStarting: return "starting" - case 3: + case JobStatusRunning: return "running" - case 4: + case JobStatusIdle: return "idle" - case 5: + case JobStatusStopping: return "stopping" - case 6: - return "stopped" + case JobStatusSuccess: + return "success" + case JobStatusFailure: + return "failure" default: return "unknown" } diff --git a/pkg/request.go b/pkg/request.go index b90c294..c64c92f 100644 --- a/pkg/request.go +++ b/pkg/request.go @@ -175,13 +175,13 @@ const ( func (s RequestStatus) String() string { switch s { - case 1: + case RequestStatusPending: return "pending" - case 2: + case RequestStatusRunning: return "running" - case 3: + case RequestStatusSuccess: return "success" - case 4: + case RequestStatusFailure: return "failure" default: return "unknown" diff --git a/pkg/spider/base_spider.go b/pkg/spider/base_spider.go index 6a70bdb..5ea564e 100644 --- a/pkg/spider/base_spider.go +++ b/pkg/spider/base_spider.go @@ -424,7 +424,10 @@ func (s *BaseSpider) RerunJob(ctx context.Context, jobId string) (err error) { err = errors.New("job is not exists") return } - if job.GetContext().GetJob().GetStatus() != pkg.JobStatusStopped { + if !utils.InSlice(job.GetContext().GetJob().GetStatus(), []pkg.JobStatus{ + pkg.JobStatusSuccess, + pkg.JobStatusFailure, + }) { err = errors.New("job is not stopped") return } diff --git a/pkg/spider/job.go b/pkg/spider/job.go index d871b35..7a68acb 100644 --- a/pkg/spider/job.go +++ b/pkg/spider/job.go @@ -84,7 +84,8 @@ func (j *Job) run(ctx context.Context) (err error) { if !utils.InSlice(j.context.GetJob().GetStatus(), []pkg.JobStatus{ pkg.JobStatusReady, - pkg.JobStatusStopped, + pkg.JobStatusSuccess, + pkg.JobStatusFailure, }) { err = errors.New("the job can be started in the ready or stopped state") j.logger.Error(err) @@ -103,12 +104,18 @@ func (j *Job) run(ctx context.Context) (err error) { go func() { select { case <-j.context.GetJob().GetContext().Done(): - if j.context.GetJob().GetStatus() != pkg.JobStatusStopped { + if !utils.InSlice(j.context.GetJob().GetStatus(), []pkg.JobStatus{ + pkg.JobStatusSuccess, + pkg.JobStatusFailure, + }) { j.stop(ctx.Err()) } return case <-ctx.Done(): - if j.context.GetJob().GetStatus() != pkg.JobStatusStopped { + if !utils.InSlice(j.context.GetJob().GetStatus(), []pkg.JobStatus{ + pkg.JobStatusSuccess, + pkg.JobStatusFailure, + }) { j.stop(ctx.Err()) } return @@ -146,7 +153,10 @@ func (j *Job) run(ctx context.Context) (err error) { } func (j *Job) stop(err error) { - if j.context.GetJob().GetStatus() == pkg.JobStatusStopped { + if !utils.InSlice(j.context.GetJob().GetStatus(), []pkg.JobStatus{ + pkg.JobStatusSuccess, + pkg.JobStatusFailure, + }) { err = errors.New("job has been finished") j.logger.Error(err) return @@ -162,7 +172,7 @@ func (j *Job) stop(err error) { } j.context.GetJob().WithStopReason(err.Error()) - j.context.GetJob().WithStatus(pkg.JobStatusStopped) + j.context.GetJob().WithStatus(pkg.JobStatusFailure) j.crawler.GetSignal().JobChanged(j.context) j.spider.JobStopped(j.context, err) @@ -171,7 +181,7 @@ func (j *Job) stop(err error) { switch j.context.GetJob().GetMode() { case pkg.JobModeOnce: - j.context.GetJob().WithStatus(pkg.JobStatusStopped) + j.context.GetJob().WithStatus(pkg.JobStatusSuccess) j.crawler.GetSignal().JobChanged(j.context) j.spider.JobStopped(j.context, nil) diff --git a/pkg/statistics/job/job.go b/pkg/statistics/job/job.go index 2df0399..eb85f84 100644 --- a/pkg/statistics/job/job.go +++ b/pkg/statistics/job/job.go @@ -35,7 +35,9 @@ func (s *Job) WithStatusAndTime(status pkg.JobStatus, t time.Time) pkg.Statistic switch status { case pkg.JobStatusRunning: s.withStartTime(t) - case pkg.JobStatusStopped: + case pkg.JobStatusSuccess: + s.withFinishTime(t) + case pkg.JobStatusFailure: s.withFinishTime(t) } diff --git a/pkg/statistics/statistics.go b/pkg/statistics/statistics.go index e45d05d..bb5f7b7 100644 --- a/pkg/statistics/statistics.go +++ b/pkg/statistics/statistics.go @@ -139,7 +139,7 @@ func (s *Statistics) jobChanged(ctx pkg.Context) (err error) { switch j.GetStatus() { case pkg.JobStatusRunning: - case pkg.JobStatusStopped: + case pkg.JobStatusFailure: s.Jobs[id].WithStopReason(j.GetStopReason()) } return diff --git a/pkg/task.go b/pkg/task.go index 83fdbe3..01d3f14 100644 --- a/pkg/task.go +++ b/pkg/task.go @@ -24,13 +24,13 @@ const ( func (s TaskStatus) String() string { switch s { - case 1: + case TaskStatusPending: return "pending" - case 2: + case TaskStatusRunning: return "running" - case 3: + case TaskStatusSuccess: return "success" - case 4: + case TaskStatusFailure: return "failure" default: return "unknown" diff --git a/web/ui/src/App.vue b/web/ui/src/App.vue index 93b3110..838391f 100644 --- a/web/ui/src/App.vue +++ b/web/ui/src/App.vue @@ -83,7 +83,7 @@