Hi, I have a question about automating the restarting of Unicorn with the following steps, and much appreciate your help. Assumption: (i) "pid" is set to unicorn.pid in the config file; (ii) a Unicorn process is already running. (1) kill -USR2 `cat unicorn.pid` (via Capistrano deployment task for example) (2) Inside the "before_exec" hook in the config file, do: kill -QUIT `cat unicorn.pid.oldbin` My question is: By the time the "before_exec" hook is executed, is it guaranteed that unicorn.pid.oldbin ahas lready created (otherwise we have a racing condition here)? Or is there a better way to achieve what I want? Please Cc me as I am not on the mailing list. Please also let me know if there is a better place to ask this question. Thanks, Pai-Hung
On Friday, October 7, 2011 at 8:20 PM, Pai-Hung Chen wrote:> My question is: By the time the "before_exec" hook is executed, is it > guaranteed that unicorn.pid.oldbin ahas lready created (otherwise we > have a racing condition here)? Or is there a better way to achieve > what I want?I''m not 100% positive, but about 98% -- yes, you can count on the fact that the *.oldbin file will exist on disk before the before_exec block is executed. Eric can confirm this, but I''ve never had a problem with it...> > Please Cc me as I am not on the mailing list. > > Please also let me know if there is a better place to ask this question.You''re in the right place -- this is a pretty active mailing list :) -- Alex Sharp github.com/ajsharp twitter.com/ajsharp alexjsharp.com
On 10/09/2011 04:31 PM, Alex Sharp wrote:> On Friday, October 7, 2011 at 8:20 PM, Pai-Hung Chen wrote: >> My question is: By the time the "before_exec" hook is executed, is it >> guaranteed that unicorn.pid.oldbin ahas lready created (otherwise we >> have a racing condition here)? Or is there a better way to achieve >> what I want? > I''m not 100% positive, but about 98% -- yes, you can count on the fact that the *.oldbin file will exist on disk before the before_exec block is executed. Eric can confirm this, but I''ve never had a problem with it...No need to rely on authority, just read the source: https://github.com/defunkt/unicorn/blob/master/lib/unicorn/http_server.rb Unicorn::HttpServer#pid=(path) setter writes the pid file (lines 179-206). before_exec is called on line 439 within a fork block in Unicorn::HttpServer#reexec (this is the only call to before_exec in the code) prior to that, self.pid= is called passing oldpid whose value is "#{pid}.oldbin" (lines 398-401). [ line numbers may change if you''re not looking at commit c7ba76a21c ]
> On 10/10/2011 08:04 AM, Devin Ben-Hur wrote:> >> My question is: By the time the "before_exec" hook is executed, is it > >> guaranteed that unicorn.pid.oldbin ahas lready created (otherwise we > >> have a racing condition here)? Or is there a better way to achieve > >> what I want? > > I''m not 100% positive, but about 98% -- yes, you can count on the fact thatthe *.oldbin file will exist on> > disk before the before_exec block is executed. Eric can confirm this, butI''ve never had a problem with it...> No need to rely on authority, just read the source: > https://github.com/defunkt/unicorn/blob/master/lib/unicorn/http_server.rbThanks for the pointer. The current source code clearly shows before_exec is called after the .oldbin file is created. I''d like to know if this is considered an "invariant" going forward. Could someone in the "authority" help? Thanks, Pai-Hung
Pai-Hung Chen <paihungchen at hotmail.com> wrote:> > On 10/10/2011 08:04 AM, Devin Ben-Hur wrote: > > >> My question is: By the time the "before_exec" hook is executed, is it > > >> guaranteed that unicorn.pid.oldbin ahas lready created (otherwise we > > >> have a racing condition here)? Or is there a better way to achieve > > >> what I want? > > > I''m not 100% positive, but about 98% -- yes, you can count on the fact that > the *.oldbin file will exist on > > > disk before the before_exec block is executed. Eric can confirm this, but > I''ve never had a problem with it... > > No need to rely on authority, just read the source: > > https://github.com/defunkt/unicorn/blob/master/lib/unicorn/http_server.rb100% correct, source code never lies.> Thanks for the pointer. The current source code clearly shows before_exec is > called after the .oldbin file is created. I''d like to know if this is considered > an "invariant" going forward.Yes, there''s no way to safely handle the pid file otherwise because of race conditions you mentioned. The oldbin pid file must exist because the new process (which calls before_exec) can clobber the regular pid file once fork is called.> Could someone in the "authority" help?Fwiw, I''m only reluctantly an "authority". I''m against the notion of authority. I am and forever will be nobody :)
Eric Wong wrote:> Yes, there''s no way to safely handle the pid file otherwise because of > race conditions you mentioned. The oldbin pid file must exist because > the new process (which calls before_exec) can clobber the regular pid > file once fork is called.Thanks for the confirmation :) Much appreciated! Pai-Hung