-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liujianping
committed
May 9, 2019
1 parent
53bbc88
commit a7d3a0f
Showing
5 changed files
with
178 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/liujianping/job/config" | ||
"github.com/liujianping/job/exec" | ||
"github.com/x-mod/routine" | ||
) | ||
|
||
//JOBs type | ||
type JOBs struct { | ||
jds []*config.JD | ||
report *exec.Reporter | ||
} | ||
|
||
//NewJOBs new JOBs | ||
func NewJOBs(jds []*config.JD, report *exec.Reporter) *JOBs { | ||
return &JOBs{ | ||
jds: jds, | ||
report: report, | ||
} | ||
} | ||
|
||
//Len of JOBs | ||
func (jobs JOBs) Len() int { | ||
return len(jobs.jds) | ||
} | ||
|
||
//Less Cmp of JOBs | ||
func (jobs JOBs) Less(i, j int) bool { | ||
return jobs.jds[i].Order.Weight < jobs.jds[j].Order.Weight | ||
} | ||
|
||
//Swap of JOBs | ||
func (jobs JOBs) Swap(i, j int) { | ||
jobs.jds[i], jobs.jds[j] = jobs.jds[j], jobs.jds[i] | ||
} | ||
|
||
//Sort of JOBs | ||
func (jobs JOBs) Sort() { | ||
sort.Sort(jobs) | ||
} | ||
|
||
//Execute impl executor | ||
func (jobs JOBs) Execute(ctx context.Context) error { | ||
jmap := make(map[string]chan error, jobs.Len()) | ||
var tail chan error | ||
for _, jd := range jobs.jds { | ||
if len(jd.Order.Precondition) == 0 { | ||
job := exec.NewJob(jd, jobs.report) | ||
ch := routine.Go(ctx, job) | ||
jmap[job.String()] = ch | ||
tail = ch | ||
if jd.Order.Wait { | ||
<-ch | ||
} | ||
} | ||
} | ||
for _, jd := range jobs.jds { | ||
for _, pre := range jd.Order.Precondition { | ||
if ch, ok := jmap[pre]; ok { | ||
<-ch | ||
} | ||
job := exec.NewJob(jd, jobs.report) | ||
ch := routine.Go(ctx, job) | ||
jmap[job.String()] = ch | ||
tail = ch | ||
if jd.Order.Wait { | ||
<-ch | ||
} | ||
} | ||
} | ||
return <-tail | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.