Merge pull request #13554 from shaobo322/reform_compare_method

reform job status compare method
This commit is contained in:
Steven Zou 2021-01-08 12:49:39 +08:00 committed by GitHub
commit 1f79ce1181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 6 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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.

View File

@ -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

View File

@ -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
}