Implement jobrun package, abstraction for cron-like goroutines.

Unlike cron, there is no overtaking though.
This commit is contained in:
Christian Schwarz
2017-04-29 18:26:43 +02:00
parent d9ecfc8eb4
commit 526255a9ef
3 changed files with 178 additions and 10 deletions

View File

@ -4,9 +4,12 @@ import (
"errors"
"fmt"
"github.com/urfave/cli"
"github.com/zrepl/zrepl/jobrun"
"github.com/zrepl/zrepl/rpc"
"github.com/zrepl/zrepl/sshbytestream"
"io"
"sync"
"time"
)
type Role uint
@ -18,6 +21,7 @@ const (
var conf Config
var handler Handler
var runner *jobrun.JobRunner
func main() {
@ -37,6 +41,8 @@ func main() {
return
}
handler = Handler{}
runner = jobrun.NewJobRunner()
return
}
app.Commands = []cli.Command{
@ -73,7 +79,51 @@ func doSink(c *cli.Context) (err error) {
func doRun(c *cli.Context) error {
fmt.Printf("%#v", conf)
// Do every pull, do every push
// Scheduling
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
runner.Start()
}()
for i := range conf.Pulls {
pull := conf.Pulls[i]
j := jobrun.Job{
Name: fmt.Sprintf("pull%d", i),
Interval: time.Duration(5 * time.Second),
Repeats: true,
RunFunc: func() error {
fmt.Printf("%v: %#v\n", time.Now(), pull)
time.Sleep(10 * time.Second)
fmt.Printf("%v: %#v\n", time.Now(), pull)
return nil
},
}
runner.AddJob(j)
}
for i := range conf.Pushs {
push := conf.Pushs[i]
j := jobrun.Job{
Name: fmt.Sprintf("push%d", i),
Interval: time.Duration(5 * time.Second),
Repeats: true,
RunFunc: func() error {
fmt.Printf("%v: %#v\n", time.Now(), push)
return nil
},
}
runner.AddJob(j)
}
wg.Wait()
return nil
}