Can I send a notification when the Unicorn wizard finishes restarting?

I am running a series of Rails / Sinatra applications behind nginx + unicorn, with zero downtime deployment. I like this setting, but it takes Unicorn some time to finish the restart, so I would like to send some kind of notification when it ends.

The only callbacks that I can find in Unicorn docs are related to desktop reversal, but I don't think they will work for this.

This is what I am looking for from generosity: the old master of the unicorn starts a new master, who then starts his workers, and then the old master stops his workers and allows the new master to take over. I want to execute some ruby ​​code when this handover is complete.

Ideally, I do not want to implement any complex process monitoring to do this. If this is the only way, so be it. But before I go along this route, I am looking for simpler options.

+6
source share
2 answers

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 .

+2
source

If you have full access to this box, you can script Unicorn script to run another script that checks for the presence of /proc/<unicorn-pid>/exe , which will refer to the current process.

See: Linux Launcher Detection

Update

Based on the changes in the question, I see two options: none of them are wonderful, but they are nonetheless parameters ...

  • You may have a cron job that runs a Ruby script every minute that monitors the mtime PID directory and then ensures that the PID files exist (as this will tell you that the file has been changed in the directory and the process is running), then an additional code if both conditions are true. Again, this is ugly and it is a cron that runs every minute, but with minimal setup.

  • I know that you want to avoid complicated monitoring, but here's how I try ... I would use monit to track these processes, and when they restart, open a Ruby script that sleeps (to ensure that it starts), then checks the status of the processes ( possibly using monit again). If all of this is true, run the optional Ruby code.

Option # 1 is not clean, but when I write the monit option, I feel even better.

0
source

Source: https://habr.com/ru/post/957549/


All Articles