Skip to content

Commit

Permalink
Use USR2 signal to force update to all the repos
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranza committed Jul 18, 2018
1 parent c7885e7 commit 8e0b890
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
}

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1)
signal.Notify(signalCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2)

go s.Run(*address)

Expand All @@ -91,6 +91,10 @@ func main() {
logrus.Info("toggling debug log level")
toggleDebugLogLevel()

case syscall.SIGUSR2:
logrus.Info("Received USR2, forcing an update in all the repositories")
s.UpdateAll()

case syscall.SIGINT:
logrus.Info("Shutting down gracefully")
s.Shutdown()
Expand Down
57 changes: 33 additions & 24 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,39 @@ func (ws *WebHooksServer) WebHookHandler(w http.ResponseWriter, r *http.Request)

metrics.HooksAcceptedTotal.WithLabelValues(hookPayload.Repository.FullName).Inc()

ws.wg.Add(1)
go func(repo Repository) {
defer ws.wg.Done()

startFetch := time.Now()
if err := repo.Fetch(); err != nil {
logrus.Errorf("failed to fetch repo %s: %s", repo.origin, err)
metrics.HooksFailedTotal.WithLabelValues(repo.origin.ToPath()).Inc()
return
}
metrics.GitLatencySecondsTotal.WithLabelValues("fetch", repo.origin.ToPath()).Observe(((time.Now().Sub(startFetch)).Seconds()))
metrics.HooksUpdatedTotal.WithLabelValues(repo.origin.ToPath()).Inc()

startPush := time.Now()
if err := repo.Push(); err != nil {
logrus.Errorf("failed to push repo %s to %s: %s", repo.origin, repo.target, err)
metrics.HooksFailedTotal.WithLabelValues(repo.target.ToPath()).Inc()
return
}
metrics.GitLatencySecondsTotal.WithLabelValues("push", repo.target.ToPath()).Observe(((time.Now().Sub(startPush)).Seconds()))
metrics.HooksUpdatedTotal.WithLabelValues(repo.target.ToPath()).Inc()

logrus.Debugf("updated repository %s in %s", repo.origin, repo.target)
}(repo)
go ws.updateRepository(repo)

w.WriteHeader(http.StatusAccepted)
}

// UpdateAll triggers an update for all the repositories
func (ws *WebHooksServer) UpdateAll() {
for _, repo := range ws.repositories {
ws.updateRepository(repo)
}
}

func (ws *WebHooksServer) updateRepository(repo Repository) {
ws.wg.Add(1)
defer ws.wg.Done()

startFetch := time.Now()
if err := repo.Fetch(); err != nil {
logrus.Errorf("failed to fetch repo %s: %s", repo.origin, err)
metrics.HooksFailedTotal.WithLabelValues(repo.origin.ToPath()).Inc()
return
}
metrics.GitLatencySecondsTotal.WithLabelValues("fetch", repo.origin.ToPath()).Observe(((time.Now().Sub(startFetch)).Seconds()))
metrics.HooksUpdatedTotal.WithLabelValues(repo.origin.ToPath()).Inc()

startPush := time.Now()
if err := repo.Push(); err != nil {
logrus.Errorf("failed to push repo %s to %s: %s", repo.origin, repo.target, err)
metrics.HooksFailedTotal.WithLabelValues(repo.target.ToPath()).Inc()
return
}
metrics.GitLatencySecondsTotal.WithLabelValues("push", repo.target.ToPath()).Observe(((time.Now().Sub(startPush)).Seconds()))
metrics.HooksUpdatedTotal.WithLabelValues(repo.target.ToPath()).Inc()

logrus.Debugf("updated repository %s in %s", repo.origin, repo.target)
}

0 comments on commit 8e0b890

Please sign in to comment.