refactor(worker): use Run instead of Start and Wait

This commit is contained in:
bigeagle 2016-04-24 23:21:03 +08:00
parent a6e8e9e2d9
commit 9339fba074
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A
6 changed files with 58 additions and 36 deletions

View File

@ -44,6 +44,13 @@ func newCmdProvider(c cmdConfig) (*cmdProvider, error) {
return provider, nil return provider, nil
} }
func (p *cmdProvider) Run() error {
if err := p.Start(); err != nil {
return err
}
return p.Wait()
}
func (p *cmdProvider) Start() error { func (p *cmdProvider) Start() error {
env := map[string]string{ env := map[string]string{
"TUNASYNC_MIRROR_NAME": p.Name(), "TUNASYNC_MIRROR_NAME": p.Name(),

View File

@ -103,18 +103,11 @@ func (m *mirrorJob) Run(managerChan chan<- jobMessage, semaphore chan empty) err
// start syncing // start syncing
managerChan <- jobMessage{tunasync.Syncing, m.Name(), ""} managerChan <- jobMessage{tunasync.Syncing, m.Name(), ""}
err = provider.Start()
if err != nil {
logger.Error(
"failed to start syncing job for %s: %s",
m.Name(), err.Error(),
)
return err
}
var syncErr error var syncErr error
syncDone := make(chan error, 1) syncDone := make(chan error, 1)
go func() { go func() {
err := provider.Wait() err := provider.Run()
if !stopASAP { if !stopASAP {
syncDone <- err syncDone <- err
} }

View File

@ -3,6 +3,7 @@ package worker
import ( import (
"errors" "errors"
"os" "os"
"sync"
"time" "time"
) )
@ -21,7 +22,8 @@ type mirrorProvider interface {
// name // name
Name() string Name() string
// TODO: implement Run, Terminate and Hooks // run mirror job in background
Run() error
// run mirror job in background // run mirror job in background
Start() error Start() error
// Wait job to finish // Wait job to finish
@ -46,6 +48,8 @@ type mirrorProvider interface {
} }
type baseProvider struct { type baseProvider struct {
sync.Mutex
ctx *Context ctx *Context
name string name string
interval time.Duration interval time.Duration
@ -118,21 +122,35 @@ func (p *baseProvider) setLogFile() error {
p.cmd.SetLogFile(nil) p.cmd.SetLogFile(nil)
return nil return nil
} }
if p.logFile == nil {
logFile, err := os.OpenFile(p.LogFile(), os.O_WRONLY|os.O_CREATE, 0644) logFile, err := os.OpenFile(p.LogFile(), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil { if err != nil {
logger.Error("Error opening logfile %s: %s", p.LogFile(), err.Error()) logger.Error("Error opening logfile %s: %s", p.LogFile(), err.Error())
return err return err
} }
p.logFile = logFile p.logFile = logFile
p.cmd.SetLogFile(logFile) }
p.cmd.SetLogFile(p.logFile)
return nil return nil
} }
func (p *baseProvider) Wait() error { func (p *baseProvider) Run() error {
if p.logFile != nil { panic("Not Implemented")
defer p.logFile.Close()
} }
func (p *baseProvider) Start() error {
panic("Not Implemented")
}
func (p *baseProvider) Wait() error {
defer func() {
p.Lock()
if p.logFile != nil {
p.logFile.Close()
p.logFile = nil
}
p.Unlock()
}()
return p.cmd.Wait() return p.cmd.Wait()
} }
@ -141,9 +159,14 @@ func (p *baseProvider) Terminate() error {
if p.cmd == nil { if p.cmd == nil {
return errors.New("provider command job not initialized") return errors.New("provider command job not initialized")
} }
p.Lock()
if p.logFile != nil { if p.logFile != nil {
p.logFile.Close() p.logFile.Close()
p.logFile = nil
} }
p.Unlock()
err := p.cmd.Terminate() err := p.cmd.Terminate()
return err return err
} }

View File

@ -116,9 +116,7 @@ echo $AOSP_REPO_BIN
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(readedScriptContent, ShouldResemble, []byte(scriptContent)) So(readedScriptContent, ShouldResemble, []byte(scriptContent))
err = provider.Start() err = provider.Run()
So(err, ShouldBeNil)
err = provider.Wait()
So(err, ShouldBeNil) So(err, ShouldBeNil)
loggedContent, err := ioutil.ReadFile(provider.LogFile()) loggedContent, err := ioutil.ReadFile(provider.LogFile())
@ -134,9 +132,7 @@ echo $AOSP_REPO_BIN
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(readedScriptContent, ShouldResemble, []byte(scriptContent)) So(readedScriptContent, ShouldResemble, []byte(scriptContent))
err = provider.Start() err = provider.Run()
So(err, ShouldBeNil)
err = provider.Wait()
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
}) })
@ -148,15 +144,12 @@ sleep 5
err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755) err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
So(err, ShouldBeNil) So(err, ShouldBeNil)
err = provider.Start()
So(err, ShouldBeNil)
go func() { go func() {
err = provider.Wait() err = provider.Run()
ctx.So(err, ShouldNotBeNil) ctx.So(err, ShouldNotBeNil)
}() }()
time.Sleep(2) time.Sleep(1 * time.Second)
err = provider.Terminate() err = provider.Terminate()
So(err, ShouldBeNil) So(err, ShouldBeNil)

View File

@ -55,6 +55,13 @@ func newRsyncProvider(c rsyncConfig) (*rsyncProvider, error) {
return provider, nil return provider, nil
} }
func (p *rsyncProvider) Run() error {
if err := p.Start(); err != nil {
return err
}
return p.Wait()
}
func (p *rsyncProvider) Start() error { func (p *rsyncProvider) Start() error {
env := map[string]string{} env := map[string]string{}
if p.password != "" { if p.password != "" {

View File

@ -15,6 +15,8 @@ import (
// it's an alternative to python-sh or go-sh // it's an alternative to python-sh or go-sh
// TODO: cgroup excution // TODO: cgroup excution
var errProcessNotStarted = errors.New("Process Not Started")
type cmdJob struct { type cmdJob struct {
cmd *exec.Cmd cmd *exec.Cmd
workingDir string workingDir string
@ -62,11 +64,8 @@ func (c *cmdJob) SetLogFile(logFile *os.File) {
} }
func (c *cmdJob) Terminate() error { func (c *cmdJob) Terminate() error {
if c.cmd == nil { if c.cmd == nil || c.cmd.Process == nil {
return nil return errProcessNotStarted
}
if c.cmd.Process == nil {
return nil
} }
err := unix.Kill(c.cmd.Process.Pid, syscall.SIGTERM) err := unix.Kill(c.cmd.Process.Pid, syscall.SIGTERM)
if err != nil { if err != nil {