I built it before, but it's not quite easy.
The first step is to add an API that returns the git SHA of the current version of the deployed code. For example, you are deploying AAAA. Now you are deploying the BBBB and it will be returned. For example, let's say you add the api "/ check / version" , which returns the SHA.
Here's an example Rails controller to implement this API. It assumes that the capistrano REVISION file is present and reads the current SHA version into memory when the application loads:
class ChecksController VERSION = File.read(File.join(Rails.root, 'REVISION')) rescue 'UNKNOWN' def version render(:text => VERSION) end end
Then you can poll the local unicorn for SHA through your API and wait for it to change in the new version.
Here's an example using Capistrano that compares the current version of an SHA application with a recently deployed version of an SHA application:
namespace :deploy do desc "Compare running app version to deployed app version" task :check_release_version, :roles => :app, :except => { :no_release => true } do timeout_at = Time.now + 60 while( Time.now < timeout_at) do expected_version = capture("cat /data/server/current/REVISION") running_version = capture("curl -f http://localhost:8080/checks/version; exit 0") if expected_version.strip == running_version.strip puts "deploy:check_release_version: OK" break else puts "=[WARNING]===========================================================" puts "= Stale Code Version" puts "=[Expected]==========================================================" puts expected_version puts "=[Running]===========================================================" puts running_version puts "=====================================================================" Kernel.sleep(10) end end end end
You will want to configure polling timeouts / retries according to the average application launch time. This example assumes a capistrano structure with the application in /data/server/current and a local unicorn on port 8080 .
source share