Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping authored May 9, 2019
2 parents 0890cc9 + 0985403 commit fdf3196
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 149 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: go
go:
- 1.11.x
env:
- GO111MODULE=on
script:
- GOOS=windows go install -mod vendor github.com/liujianping/job
- GOOS=linux go install -mod vendor github.com/liujianping/job
- GOOS=darwin go install -mod vendor github.com/liujianping/job
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ make your short-term command as a long-term job
Brew install

````bash
$ brew tap liujianping/tap && brew install job
$: brew tap liujianping/tap && brew install job
````

OR go source install
OR

````
$ git clone https://github.com/liujianping/job.git
$ cd job
$ go build -mod vendor
````

OR without go module enable,

````
$: go get -u github.com/liujianping/job
````bash
$: git clone https://github.com/liujianping/job.git
$: cd job
$: go build -mod vendor
````

## Usage

````shell
$ job -h
````bash

$: job -h
Job, make your short-term command as a long-term job

Usage:
Expand Down Expand Up @@ -71,7 +66,7 @@ Flags:
#### Output Job

````bash
$ job -n 10 -i 500ms -T 3s -o -- curl https://www.baidu.com
$: job -n 10 -i 500ms -T 3s -o -- curl https://www.baidu.com
Job:
name: ""
command:
Expand Down Expand Up @@ -160,7 +155,7 @@ Job:
#### Local Report

````bash
$ job -n 10 -i 500ms -c 5 -R -- echo hello
$: job -n 10 -i 500ms -c 5 -R -- echo hello

Uptime: 5.1037 secs

Expand Down Expand Up @@ -201,3 +196,6 @@ Code distribution:
[0] 50 responses
````

## TODO

- metrics report to prometheus push gateway support
21 changes: 21 additions & 0 deletions build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package build

import "fmt"

var (
version = "dev"
commit = "none"
date = "unknown"
)

//Print version
func Print() {
fmt.Printf("%v, commit %v, built at %v\n", version, commit, date)
}

//Info build info
func Info(v, m, d string) {
version = v
commit = m
date = d
}
76 changes: 76 additions & 0 deletions cmd/jobs.go
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
}
Loading

0 comments on commit fdf3196

Please sign in to comment.