Skip to content

Commit

Permalink
optimize status
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Nov 21, 2023
1 parent 3710006 commit 91aead8
Show file tree
Hide file tree
Showing 22 changed files with 361 additions and 161 deletions.
4 changes: 3 additions & 1 deletion pkg/context/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 13 additions & 10 deletions pkg/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion pkg/spider/base_spider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 16 additions & 6 deletions pkg/spider/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/statistics/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions web/ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<script lang='jsx' setup>
import {h, reactive, ref, watch} from 'vue';
import {
BarsOutlined,
BarsOutlined, CloudDownloadOutlined,
ClusterOutlined,
DatabaseOutlined,
DownOutlined,
Expand Down Expand Up @@ -188,7 +188,7 @@ const items = reactive([
},
{
key: '6',
icon: () => h(DatabaseOutlined),
icon: () => h(CloudDownloadOutlined),
label: <RouterLink to="/requests">Requests</RouterLink>,
title: 'Requests',
},
Expand Down
24 changes: 23 additions & 1 deletion web/ui/src/stores/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@ export const JobStatusStarting = 2
export const JobStatusRunning = 3
export const JobStatusIdle = 4
export const JobStatusStopping = 5
export const JobStatusStopped = 6
export const JobStatusSuccess = 6
export const JobStatusFailure = 7

export const JobStatusName = (status) => {
switch (status) {
case JobStatusReady:
return 'ready'
case JobStatusStarting:
return 'starting'
case JobStatusRunning:
return 'running'
case JobStatusIdle:
return 'idle'
case JobStatusStopping:
return 'stopping'
case JobStatusSuccess:
return 'success'
case JobStatusFailure:
return 'failure'
default:
return 'unknown'
}
}

export const useJobsStore = defineStore('jobs', () => {
const jobs = reactive([])
Expand Down
18 changes: 18 additions & 0 deletions web/ui/src/stores/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export const NodeStatusRunning = 3
export const NodeStatusIdle = 4
export const NodeStatusStopping = 5
export const NodeStatusStopped = 6
export const NodeStatusName = (status) => {
switch (status) {
case NodeStatusReady:
return 'ready'
case NodeStatusStarting:
return 'starting'
case NodeStatusRunning:
return 'running'
case NodeStatusIdle:
return 'idle'
case NodeStatusStopping:
return 'stopping'
case NodeStatusStopped:
return 'stopped'
default:
return 'unknown'
}
}

export const useNodesStore = defineStore('nodes', () => {
const nodes = reactive([])
Expand Down
21 changes: 21 additions & 0 deletions web/ui/src/stores/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import {defineStore} from 'pinia'
import {computed, reactive} from 'vue';
import {getRecords} from "@/requests/api";

export const ItemStatusUnknown = 0
export const ItemStatusPending = 1
export const ItemStatusRunning = 2
export const ItemStatusSuccess = 3
export const ItemStatusFailure = 4

export const ItemStatusName = (status) => {
switch (status) {
case ItemStatusPending:
return 'pending'
case ItemStatusRunning:
return 'running'
case ItemStatusSuccess:
return 'success'
case ItemStatusFailure:
return 'failure'
default:
return 'unknown'
}
}
export const useRecordsStore = defineStore('records', () => {
const records = reactive([])

Expand All @@ -21,5 +41,6 @@ export const useRecordsStore = defineStore('records', () => {
return records.length
})


return {records, GetRecords, Count}
})
20 changes: 20 additions & 0 deletions web/ui/src/stores/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import {defineStore} from 'pinia'
import {computed, reactive} from 'vue';
import {getRequests} from "@/requests/api";

export const RequestStatusUnknown = 0
export const RequestStatusPending = 1
export const RequestStatusRunning = 2
export const RequestStatusSuccess = 3
export const RequestStatusFailure = 4

export const RequestStatusName = (status) => {
switch (status) {
case RequestStatusPending:
return 'pending'
case RequestStatusRunning:
return 'running'
case RequestStatusSuccess:
return 'success'
case RequestStatusFailure:
return 'failure'
default:
return 'unknown'
}
}
export const useRequestsStore = defineStore('requests', () => {
const requests = reactive([])

Expand Down
18 changes: 18 additions & 0 deletions web/ui/src/stores/spiders.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export const SpiderStatusRunning = 3
export const SpiderStatusIdle = 4
export const SpiderStatusStopping = 5
export const SpiderStatusStopped = 6
export const SpiderStatusName = (status) => {
switch (status) {
case SpiderStatusReady:
return 'ready'
case SpiderStatusStarting:
return 'starting'
case SpiderStatusRunning:
return 'running'
case SpiderStatusIdle:
return 'idle'
case SpiderStatusStopping:
return 'stopping'
case SpiderStatusStopped:
return 'stopped'
default:
return 'unknown'
}
}

export const useSpidersStore = defineStore('spiders', () => {
const spiders = reactive([])
Expand Down
23 changes: 17 additions & 6 deletions web/ui/src/stores/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ export const TaskStatusUnknown = 0
export const TaskStatusPending = 1
export const TaskStatusRunning = 2
export const TaskStatusSuccess = 3
export const TaskStatusError = 4
export const TaskStatusFailure = 4

export const TaskStatusName = (status) => {
switch (status) {
case TaskStatusPending:
return 'pending'
case TaskStatusRunning:
return 'running'
case TaskStatusSuccess:
return 'success'
case TaskStatusFailure:
return 'failure'
default:
return 'unknown'
}
}

export const useTasksStore = defineStore('tasks', () => {
const tasks = reactive([])
Expand All @@ -27,9 +42,5 @@ export const useTasksStore = defineStore('tasks', () => {
return tasks.length
})

const CountError = computed(() => {
return tasks.filter(v => v.status === 4).length
})

return {tasks, GetTasks, Count, CountError}
return {tasks, GetTasks, Count}
})
Loading

0 comments on commit 91aead8

Please sign in to comment.