diff --git a/src/jobservice/job/status.go b/src/jobservice/job/status.go index 6b2d85218..6e3878534 100644 --- a/src/jobservice/job/status.go +++ b/src/jobservice/job/status.go @@ -72,10 +72,26 @@ func (s Status) Code() int { // if < 0, s before another status // if == 0, same status // if > 0, s after another status +// Deprecated func (s Status) Compare(another Status) int { return s.Code() - another.Code() } +// Before return true if the status s is before another +func (s Status) Before(another Status) bool { + return s.Code()-another.Code() < 0 +} + +// After return true if the status s is after another +func (s Status) After(another Status) bool { + return s.Code()-another.Code() > 0 +} + +// Equal return true if the status s is the same of another +func (s Status) Equal(another Status) bool { + return s.Code()-another.Code() == 0 +} + // String returns the raw string value of the status func (s Status) String() string { return string(s) diff --git a/src/jobservice/period/basic_scheduler.go b/src/jobservice/period/basic_scheduler.go index 57f7d6cd5..2797780b2 100644 --- a/src/jobservice/period/basic_scheduler.go +++ b/src/jobservice/period/basic_scheduler.go @@ -145,7 +145,7 @@ func (bs *basicScheduler) UnSchedule(policyID string) error { // Mark job status to stopped to block execution. // The executions here should not be in the final states, // double confirmation: only stop the can-stop ones. - if job.RunningStatus.Compare(job.Status(e.Info.Status)) >= 0 { + if job.RunningStatus.After(job.Status(e.Info.Status)) || job.RunningStatus.Equal(job.Status(e.Info.Status)) { if err := eTracker.Stop(); err != nil { logger.Errorf("Stop execution %s error: %s", eID, err) } else { diff --git a/src/jobservice/worker/cworker/c_worker.go b/src/jobservice/worker/cworker/c_worker.go index a69f2bf21..82cfd7887 100644 --- a/src/jobservice/worker/cworker/c_worker.go +++ b/src/jobservice/worker/cworker/c_worker.go @@ -336,7 +336,7 @@ func (w *basicWorker) StopJob(jobID string) error { } // General or scheduled job - if job.RunningStatus.Compare(job.Status(t.Job().Info.Status)) < 0 { + if job.RunningStatus.Before(job.Status(t.Job().Info.Status)) { // Job has been in the final states logger.Warningf("Trying to stop a(n) %s job: ID=%s, Kind=%s", t.Job().Info.Status, jobID, t.Job().Info.JobKind) // Under this situation, the non-periodic job we're trying to stop has already been in the "non-running(stopped)" status. diff --git a/src/jobservice/worker/cworker/reaper.go b/src/jobservice/worker/cworker/reaper.go index 68a80758e..4e3a1ff9e 100644 --- a/src/jobservice/worker/cworker/reaper.go +++ b/src/jobservice/worker/cworker/reaper.go @@ -353,9 +353,11 @@ func compare(j *job.StatsInfo) int { } // Revision is same, then compare the status - st := job.Status(j.Status).Compare(job.Status(j.HookAck.Status)) - if st != 0 { - return st + switch { + case job.Status(j.Status).Before(job.Status(j.HookAck.Status)): + return -1 + case job.Status(j.Status).After(job.Status(j.HookAck.Status)): + return 1 } // Revision and status are same, then compare the checkin diff --git a/src/pkg/scan/vuln/util.go b/src/pkg/scan/vuln/util.go index 34e735daf..451518278 100644 --- a/src/pkg/scan/vuln/util.go +++ b/src/pkg/scan/vuln/util.go @@ -88,7 +88,7 @@ func mergeScanStatus(s1, s2 string) string { return job.SuccessStatus.String() } - if j1.Compare(j2) > 0 { + if j1.After(j2) { return s1 }