From c354c11f77a618686a0fb44081b3692485276223 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Sun, 4 Jul 2021 17:20:23 +0000 Subject: [PATCH] Automatic reload every 5 minutes --- Makefile | 1 - supervisor/src/main.go | 48 +++++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 6286b1d..f6495b2 100644 --- a/Makefile +++ b/Makefile @@ -229,7 +229,6 @@ deploy-config: # Generate deployment config file deploy: deploy-config # Upload deployment config to S3 and update ASG instances aws s3 cp $(BUILD)/config.json $(S3_CONFIG) - tools/force-update-asg.bash ### Infrastructure diff --git a/supervisor/src/main.go b/supervisor/src/main.go index bbd2f49..fa6fe65 100644 --- a/supervisor/src/main.go +++ b/supervisor/src/main.go @@ -74,6 +74,27 @@ func (sv *supervisor) status(status string) { log.Println("active: " + status) } +func (sv *supervisor) scheduleReload() string { + uuid := "" + sv.reloadLock.Lock() + if !sv.reloadInProgress { + sv.reloadInProgress = true + sv.reloadUUID = uuidlib.New().String() + uuid = sv.reloadUUID + go sv.reloadWithScheduling() + } else { + if sv.reloadInProgress { + uuid = sv.reloadNextUUID + } else { + sv.reloadNextUUID = uuidlib.New().String() + uuid = sv.reloadNextUUID + } + sv.reloadNeeded = true + } + sv.reloadLock.Unlock() + return uuid +} + func (sv *supervisor) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/api/supervisor") { if r.URL.Path == "/api/supervisor/v1/reload" { @@ -81,23 +102,7 @@ func (sv *supervisor) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed) return } - uuid := "" - sv.reloadLock.Lock() - if !sv.reloadInProgress { - sv.reloadInProgress = true - sv.reloadUUID = uuidlib.New().String() - uuid = sv.reloadUUID - go sv.reloadWithScheduling() - } else { - if sv.reloadInProgress { - uuid = sv.reloadNextUUID - } else { - sv.reloadNextUUID = uuidlib.New().String() - uuid = sv.reloadNextUUID - } - sv.reloadNeeded = true - } - sv.reloadLock.Unlock() + uuid := sv.scheduleReload() fmt.Fprintln(w, uuid) return } @@ -179,6 +184,14 @@ func (sv *supervisor) reloadWithScheduling() { sv.reloadUUID = sv.reloadNextUUID sv.reloadNextUUID = "" go sv.reloadWithScheduling() + } else { + go func() { + // Arguably slightly incorrect but it's fine + // if we reload slightly more than once per 5 + // minutes. + time.Sleep(5 * time.Minute) + sv.scheduleReload() + }() } sv.reloadLock.Unlock() } @@ -389,6 +402,7 @@ func main() { awsAccountNumber: *ident.Account, reloadJobs: map[string]*reloadJob{}, } + go sv.scheduleReload() log.Println("listening on http://0.0.0.0:80") log.Fatalln(http.ListenAndServe("0.0.0.0:80", sv)) }