From b952fd8f8f568dedf8e5ae8c498eb4a2c739b35e Mon Sep 17 00:00:00 2001 From: Benny Ng Date: Tue, 5 Apr 2016 18:53:45 +0800 Subject: [PATCH] revert to old Caddyfile on failed in-process restart --- caddy/restart.go | 10 ++++++++++ caddy/restart_windows.go | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/caddy/restart.go b/caddy/restart.go index 99dab19e4..6237c9603 100644 --- a/caddy/restart.go +++ b/caddy/restart.go @@ -182,8 +182,18 @@ func restartInProc(newCaddyfile Input) error { return err } + caddyfileMu.Lock() + oldCaddyfile := caddyfile + caddyfileMu.Unlock() + err = Start(newCaddyfile) if err != nil { + // revert to old Caddyfile + if oldErr := Start(oldCaddyfile); oldErr != nil { + log.Printf("[ERROR] Restart: in-process restart failed and cannot revert to old Caddyfile: %v", oldErr) + } else { + wg.Done() // take down our barrier + } return err } diff --git a/caddy/restart_windows.go b/caddy/restart_windows.go index f179ba693..b54f38a3e 100644 --- a/caddy/restart_windows.go +++ b/caddy/restart_windows.go @@ -7,11 +7,12 @@ import "log" func Restart(newCaddyfile Input) error { log.Println("[INFO] Restarting") + caddyfileMu.Lock() + oldCaddyfile := caddyfile if newCaddyfile == nil { - caddyfileMu.Lock() newCaddyfile = caddyfile - caddyfileMu.Unlock() } + caddyfileMu.Unlock() wg.Add(1) // barrier so Wait() doesn't unblock @@ -22,6 +23,12 @@ func Restart(newCaddyfile Input) error { err = Start(newCaddyfile) if err != nil { + // revert to old Caddyfile + if oldErr := Start(oldCaddyfile); oldErr != nil { + log.Printf("[ERROR] Restart: in-process restart failed and cannot revert to old Caddyfile: %v", oldErr) + } else { + wg.Done() // take down our barrier + } return err }