unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* How to automate the restarting of Unicorn?
@ 2011-10-08  3:20 Pai-Hung Chen
  2011-10-09 23:31 ` Alex Sharp
  0 siblings, 1 reply; 6+ messages in thread
From: Pai-Hung Chen @ 2011-10-08  3:20 UTC (permalink / raw)
  To: mongrel-unicorn

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
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automate the restarting of Unicorn?
  2011-10-08  3:20 How to automate the restarting of Unicorn? Pai-Hung Chen
@ 2011-10-09 23:31 ` Alex Sharp
  2011-10-10  6:04   ` Devin Ben-Hur
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Sharp @ 2011-10-09 23:31 UTC (permalink / raw)
  To: unicorn list; +Cc: Pai-Hung Chen

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

_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automate the restarting of Unicorn?
  2011-10-09 23:31 ` Alex Sharp
@ 2011-10-10  6:04   ` Devin Ben-Hur
  2011-10-10 20:34     ` Pai-Hung Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Devin Ben-Hur @ 2011-10-10  6:04 UTC (permalink / raw)
  To: unicorn list; +Cc: phc, ajsharp

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 ]
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automate the restarting of Unicorn?
  2011-10-10  6:04   ` Devin Ben-Hur
@ 2011-10-10 20:34     ` Pai-Hung Chen
  2011-10-10 21:27       ` Eric Wong
  0 siblings, 1 reply; 6+ messages in thread
From: Pai-Hung Chen @ 2011-10-10 20:34 UTC (permalink / raw)
  To: mongrel-unicorn

> 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.rb

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. Could someone in the "authority" help?

Thanks,
Pai-Hung



_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automate the restarting of Unicorn?
  2011-10-10 20:34     ` Pai-Hung Chen
@ 2011-10-10 21:27       ` Eric Wong
  2011-10-10 22:18         ` Pai-Hung Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Wong @ 2011-10-10 21:27 UTC (permalink / raw)
  To: mongrel-unicorn; +Cc: Pai-Hung Chen

Pai-Hung Chen <paihungchen@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.rb

100% 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 :)
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automate the restarting of Unicorn?
  2011-10-10 21:27       ` Eric Wong
@ 2011-10-10 22:18         ` Pai-Hung Chen
  0 siblings, 0 replies; 6+ messages in thread
From: Pai-Hung Chen @ 2011-10-10 22:18 UTC (permalink / raw)
  To: mongrel-unicorn

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

_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-10-10 22:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-08  3:20 How to automate the restarting of Unicorn? Pai-Hung Chen
2011-10-09 23:31 ` Alex Sharp
2011-10-10  6:04   ` Devin Ben-Hur
2011-10-10 20:34     ` Pai-Hung Chen
2011-10-10 21:27       ` Eric Wong
2011-10-10 22:18         ` Pai-Hung Chen

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).