Skip to content

Commit

Permalink
feature(worker): implemented mirror role (master/slave) option
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed Apr 30, 2016
1 parent 8399417 commit 56459f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/worker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ command = "/tmp/tunasync/bin/myrsync2.sh"
upstream = "https://aosp.google.com/"
interval = 2
mirror_dir = "/tmp/tunasync/git/AOSP"
role = "slave"
[mirrors.env]
REPO = "/usr/local/bin/aosp-repo"

Expand Down
1 change: 1 addition & 0 deletions worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type mirrorConfig struct {
MirrorDir string `toml:"mirror_dir"`
LogDir string `toml:"log_dir"`
Env map[string]string `toml:"env"`
Role string `toml:"role"`

ExecOnSuccess string `toml:"exec_on_success"`
ExecOnFailure string `toml:"exec_on_failure"`
Expand Down
6 changes: 6 additions & 0 deletions worker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type mirrorProvider interface {
WorkingDir() string
LogDir() string
LogFile() string
IsMaster() bool

// enter context
EnterContext() *Context
Expand All @@ -59,6 +60,7 @@ type baseProvider struct {
ctx *Context
name string
interval time.Duration
isMaster bool

cmd *cmdJob
isRunning atomic.Value
Expand Down Expand Up @@ -92,6 +94,10 @@ func (p *baseProvider) Interval() time.Duration {
return p.interval
}

func (p *baseProvider) IsMaster() bool {
return p.isMaster
}

func (p *baseProvider) WorkingDir() string {
if v, ok := p.ctx.Get(_WorkingDirKey); ok {
if s, ok := v.(string); ok {
Expand Down
15 changes: 14 additions & 1 deletion worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ func (w *Worker) initProviders() {
}
logDir = formatLogDir(logDir, mirror)

// IsMaster
isMaster := true
if mirror.Role == "slave" {
isMaster = false
} else {
if mirror.Role != "" && mirror.Role != "master" {
logger.Warningf("Invalid role configuration for %s", mirror.Name)
}
}

var provider mirrorProvider

switch mirror.Provider {
Expand All @@ -105,6 +115,7 @@ func (w *Worker) initProviders() {
env: mirror.Env,
}
p, err := newCmdProvider(pc)
p.isMaster = isMaster
if err != nil {
panic(err)
}
Expand All @@ -123,6 +134,7 @@ func (w *Worker) initProviders() {
interval: time.Duration(mirror.Interval) * time.Minute,
}
p, err := newRsyncProvider(rc)
p.isMaster = isMaster
if err != nil {
panic(err)
}
Expand All @@ -142,6 +154,7 @@ func (w *Worker) initProviders() {
interval: time.Duration(mirror.Interval) * time.Minute,
}
p, err := newTwoStageRsyncProvider(rc)
p.isMaster = isMaster
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -395,7 +408,7 @@ func (w *Worker) updateStatus(jobMsg jobMessage) {
smsg := MirrorStatus{
Name: jobMsg.name,
Worker: w.cfg.Global.Name,
IsMaster: true,
IsMaster: p.IsMaster(),
Status: jobMsg.status,
Upstream: p.Upstream(),
Size: "unknown",
Expand Down

0 comments on commit 56459f2

Please sign in to comment.