FIX: Restore unicorn workers when upgrader is met with an exception.

FIX: Restore unicorn workers when upgrader is met with an exception.

diff --git a/lib/docker_manager/upgrader.rb b/lib/docker_manager/upgrader.rb
index c5432bf..88c4dd3 100644
--- a/lib/docker_manager/upgrader.rb
+++ b/lib/docker_manager/upgrader.rb
@@ -47,10 +47,12 @@ class DockerManager::Upgrader
     reload_unicorn(launcher_pid)
 
     percent(10)
+    reloaded = false
+    num_workers_spun_down = workers - min_workers
 
-    if workers > min_workers
+    if num_workers_spun_down.positive?
       log "Stopping #{workers - min_workers} Unicorn worker(s), to free up memory"
-      (workers - min_workers).times { Process.kill("TTOU", unicorn_master_pid) }
+      (num_workers_spun_down).times { Process.kill("TTOU", unicorn_master_pid) }
     end
 
     if ENV["UNICORN_SIDEKIQS"].to_i > 0
@@ -78,6 +80,7 @@ class DockerManager::Upgrader
 
     percent(80)
     reload_unicorn(launcher_pid)
+    reloaded = true
 
     percent(90)
     log("Running post deploy migrations")
@@ -99,6 +102,11 @@ class DockerManager::Upgrader
       log(message)
     end
 
+    if num_workers_spun_down.positive? && !reloaded
+      log "Spinning up #{num_workers_spun_down} Unicorn worker(s) that were stopped initially"
+      (num_workers_spun_down).times { Process.kill("TTIN", unicorn_master_pid) }
+    end
+
     raise ex
   ensure
     @repos.each(&:stop_upgrading)

GitHub sha: f0d181a8