Remove global trigger channel

Instead change syncRepo to return a boolean which is whether a change
occured.
This commit is contained in:
Thomas Jackson 2019-01-22 12:01:18 -08:00
parent 5c63dc9cb5
commit 96714ebed2
2 changed files with 16 additions and 18 deletions

View File

@ -200,13 +200,14 @@ func main() {
log.V(0).Infof("starting up: %q", os.Args) log.V(0).Infof("starting up: %q", os.Args)
// Startup webhooks goroutine // Startup webhooks goroutine
go ServeWebhooks() webhookTriggerChan := make(chan struct{})
go ServeWebhooks(webhookTriggerChan)
initialSync := true initialSync := true
failCount := 0 failCount := 0
for { for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout))
if err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest); err != nil { if changed, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest); err != nil {
if initialSync || (*flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures) { if initialSync || (*flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures) {
log.Errorf("error syncing repo: %v", err) log.Errorf("error syncing repo: %v", err)
os.Exit(1) os.Exit(1)
@ -218,6 +219,9 @@ func main() {
cancel() cancel()
time.Sleep(waitTime(*flWait)) time.Sleep(waitTime(*flWait))
continue continue
} else if changed {
// Trigger webhooks to be called
webhookTriggerChan <- struct{}{}
} }
if initialSync { if initialSync {
if *flOneTime { if *flOneTime {
@ -304,9 +308,6 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) error {
log.V(1).Infof("pruned old worktrees") log.V(1).Infof("pruned old worktrees")
} }
// Trigger webhooks to be called
WebhookCallTriggerChannel <- struct{}{}
return nil return nil
} }
@ -407,7 +408,8 @@ func revIsHash(ctx context.Context, rev, gitRoot string) (bool, error) {
} }
// syncRepo syncs the branch of a given repository to the destination at the given rev. // syncRepo syncs the branch of a given repository to the destination at the given rev.
func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, dest string) error { // returns (1) whether a change occured and (2) an error if one happened
func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, dest string) (bool, error) {
target := path.Join(gitRoot, dest) target := path.Join(gitRoot, dest)
gitRepoPath := path.Join(target, ".git") gitRepoPath := path.Join(target, ".git")
hash := rev hash := rev
@ -416,18 +418,18 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot,
case os.IsNotExist(err): case os.IsNotExist(err):
err = cloneRepo(ctx, repo, branch, rev, depth, gitRoot) err = cloneRepo(ctx, repo, branch, rev, depth, gitRoot)
if err != nil { if err != nil {
return err return false, err
} }
hash, err = hashForRev(ctx, rev, gitRoot) hash, err = hashForRev(ctx, rev, gitRoot)
if err != nil { if err != nil {
return err return false, err
} }
case err != nil: case err != nil:
return fmt.Errorf("error checking if repo exists %q: %v", gitRepoPath, err) return false, fmt.Errorf("error checking if repo exists %q: %v", gitRepoPath, err)
default: default:
local, remote, err := getRevs(ctx, target, branch, rev) local, remote, err := getRevs(ctx, target, branch, rev)
if err != nil { if err != nil {
return err return false, err
} }
log.V(2).Infof("local hash: %s", local) log.V(2).Infof("local hash: %s", local)
log.V(2).Infof("remote hash: %s", remote) log.V(2).Infof("remote hash: %s", remote)
@ -436,11 +438,11 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot,
hash = remote hash = remote
} else { } else {
log.V(1).Infof("no update required") log.V(1).Infof("no update required")
return nil return false, nil
} }
} }
return addWorktreeAndSwap(ctx, gitRoot, dest, branch, rev, hash) return true, addWorktreeAndSwap(ctx, gitRoot, dest, branch, rev, hash)
} }
// getRevs returns the local and upstream hashes for rev. // getRevs returns the local and upstream hashes for rev.

View File

@ -8,10 +8,6 @@ import (
"time" "time"
) )
// Trigger channel for webhook requests. If anything is received into this channel
// it triggers the webhook goroutine to send new requests.
var WebhookCallTriggerChannel = make(chan struct{})
// Webhook collection // Webhook collection
var WebhookArray = []Webhook{} var WebhookArray = []Webhook{}
@ -66,10 +62,10 @@ func (w *Webhook) Do() error {
} }
// Wait for trigger events from the channel, and send webhooks when triggered // Wait for trigger events from the channel, and send webhooks when triggered
func ServeWebhooks() { func ServeWebhooks(ch chan struct{}) {
for { for {
// Wait for trigger // Wait for trigger
<-WebhookCallTriggerChannel <-ch
// Calling webhook - one after another // Calling webhook - one after another
for _, v := range WebhookArray { for _, v := range WebhookArray {