[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

FastCGI: handling the caching of ruby scripts.

Jonas Hartmann

6/24/2005 1:25:00 PM

FastCGIs idea of caching scripts on first startup is a great thing but:

- Sometimes you need to reload scripts
- If you are developing, reloading scripts that have been modified is
needed.

My idea was to support two ways of reloading.
1.) A GET/POST variable from HTTP can force the script to stops its
FCGI process and launches itself again after that.

2.) The script has to detect if the user-agent send an force-refresh.
(Shift+Reload in Firefox for example). If that is the case, the script
stops its FCGI process and launches itself again after that. This
behaviour can be disabled by a global variable.

I do not have any idea how to make neither 1.) nor 2.) work. How do I
detect if the user-agent send an force-reload/cache-ignore?

I cannot make the last example on this Page:
http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
working yet...

Any hints, tipps, suggestions?

--
Jonas


7 Answers

Dick Davies

6/24/2005 1:47:00 PM

0

* Jonas Hartmann <Mail@Jonas-Hartmann.com> [0625 14:25]:
> FastCGIs idea of caching scripts on first startup is a great thing but:
>
> - Sometimes you need to reload scripts
> - If you are developing, reloading scripts that have been modified is
> needed.

It's not so much that it caches, just that it doesn't exit.

> My idea was to support two ways of reloading.
> 1.) A GET/POST variable from HTTP can force the script to stops its
> FCGI process and launches itself again after that.
>
> 2.) The script has to detect if the user-agent send an force-refresh.
> (Shift+Reload in Firefox for example). If that is the case, the script
> stops its FCGI process and launches itself again after that. This
> behaviour can be disabled by a global variable.
>
> I do not have any idea how to make neither 1.) nor 2.) work. How do I
> detect if the user-agent send an force-reload/cache-ignore?

I'm not sure that's the 'right way' - do you really want clients to tell your
app when to restart?

> I cannot make the last example on this Page:
> http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
> working yet...

That should work. I did something similar (which it looks like I've since deleted)
that killed itself if it's file had changed and let apache restart it.

These days I use lighttpd running as myself to frontend my app, so I either restart it
or kill the fcgi children myself and let lighty restart them.

--
'Terrify ants into believing they have been invaded by "War Of The Worlds" style\nMartians by standing 3 pin plugs on end around their holes."'
-- J.T. Thropton.
Rasputin :: Jack of All Trades - Master of Nuns


Jonas Hartmann

6/24/2005 2:05:00 PM

0

Jonas Hartmann wrote:
> FastCGIs idea of caching scripts on first startup is a great thing but:
>
> - Sometimes you need to reload scripts
> - If you are developing, reloading scripts that have been modified is
> needed.
>
> My idea was to support two ways of reloading.
> 1.) A GET/POST variable from HTTP can force the script to stops its FCGI
> process and launches itself again after that.
>
> 2.) The script has to detect if the user-agent send an force-refresh.
> (Shift+Reload in Firefox for example). If that is the case, the script
> stops its FCGI process and launches itself again after that. This
> behaviour can be disabled by a global variable.
>
> I do not have any idea how to make neither 1.) nor 2.) work. How do I
> detect if the user-agent send an force-reload/cache-ignore?
>
> I cannot make the last example on this Page:

I have found a simple solution and updated this page:
> http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
> working yet...
>
> Any hints, tipps, suggestions?
>
> --
> Jonas
>
>
>



Brian Candler

6/25/2005 9:29:00 AM

0

On Fri, Jun 24, 2005 at 10:25:06PM +0900, Jonas Hartmann wrote:
> I cannot make the last example on this Page:
> http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
> working yet...

Which one - the "FastCgiConfig -autoUpdate" or the Thread.new ?

Both will only work if the main application file itself has updated, not any
libraries which it 'requires'

You could write something which checks all the files in $". However, those
paths are not absolute, and each one would have to be searched against $: to
find them.

Hmm, I don't see a Ruby function to search for a file in a path (strange,
maybe I'm missing something), so this gets a bit more long-winded than I
expected:

---- 8< -----------------------------------------------------------------
#!/usr/bin/env ruby

require 'fcgi'
# ... more requires

def path_search(file, pathlist)
return file if file[0..0] == File::SEPARATOR and File.exist?(file)
pathlist.each do |p|
fn = p+File::SEPARATOR+file
return fn if File.exist?(fn)
end
nil
end

Thread.new([File.expand_path(__FILE__)]) do |fname|
old_stats = {}
while true
(fname+$").each do |f|
stat = File.stat(path_search(f,$:)) rescue nil
if stat and old_stats[f]
exit if stat.mtime != old_stats[f].mtime or
stat.ino != old_stats[f].ino
end
old_stats[f] = stat
end
sleep 10
end
end

#... rest if program
---- 8< -----------------------------------------------------------------

To debug this, set "Thread.abort_on_exception=true" at the top.

I think this should work even if the number of files 'required' by the
program increases over time.

Regards,

Brian.


Jonas Hartmann

6/25/2005 10:49:00 AM

0

Brian Candler wrote:
> On Fri, Jun 24, 2005 at 10:25:06PM +0900, Jonas Hartmann wrote:
>
>>I cannot make the last example on this Page:
>>http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
>>working yet...
>
>
> Which one - the "FastCgiConfig -autoUpdate" or the Thread.new ?
>
> Both will only work if the main application file itself has updated, not any
> libraries which it 'requires'
>
> You could write something which checks all the files in $". However, those
> paths are not absolute, and each one would have to be searched against $: to
> find them.
>
> Hmm, I don't see a Ruby function to search for a file in a path (strange,
> maybe I'm missing something), so this gets a bit more long-winded than I
> expected:
>
> ---- 8< -----------------------------------------------------------------
> #!/usr/bin/env ruby
>
> require 'fcgi'
> # ... more requires
>
> def path_search(file, pathlist)
> return file if file[0..0] == File::SEPARATOR and File.exist?(file)
> pathlist.each do |p|
> fn = p+File::SEPARATOR+file
> return fn if File.exist?(fn)
> end
> nil
> end
>
> Thread.new([File.expand_path(__FILE__)]) do |fname|
> old_stats = {}
> while true
> (fname+$").each do |f|
> stat = File.stat(path_search(f,$:)) rescue nil
> if stat and old_stats[f]
> exit if stat.mtime != old_stats[f].mtime or
> stat.ino != old_stats[f].ino
> end
> old_stats[f] = stat
> end
> sleep 10
> end
> end
>
> #... rest if program
> ---- 8< -----------------------------------------------------------------
>
> To debug this, set "Thread.abort_on_exception=true" at the top.
>
> I think this should work even if the number of files 'required' by the
> program increases over time.
>
> Regards,
>
> Brian.
>
>


You are sure the -autoUpdate flag does not check files and dependencies?

foo requires bar
foo changes => autoUpdate is invoked
bar changes => foo requires a different file?

what about load and evil(eval)?


Dick Davies

6/25/2005 1:52:00 PM

0

* Brian Candler <B.Candler@pobox.com> [0629 10:29]:
> On Fri, Jun 24, 2005 at 10:25:06PM +0900, Jonas Hartmann wrote:
> > I cannot make the last example on this Page:
> > http://rubygarden.org/ruby/ruby?FCGIRubyWhyArentMyChange...
> > working yet...
>
> Which one - the "FastCgiConfig -autoUpdate" or the Thread.new ?
>
> Both will only work if the main application file itself has updated, not any

You could check out the require_dependency function in Rails, it handles
this fairly elegantly.

--
'Bus drivers. Pretend you're an airline pilot by wedging your accelerator pedal
down with a heavy book, securing the steering wheel with some old rope, and then strolling back along the bus chatting casually to the passengers.'
-- Top Tips
Rasputin :: Jack of All Trades - Master of Nuns


Brian Candler

6/26/2005 7:22:00 AM

0

On Sat, Jun 25, 2005 at 07:49:29PM +0900, Jonas Hartmann wrote:
> You are sure the -autoUpdate flag does not check files and dependencies?

It's an Apache (mod_fastcgi) flag; mod_fastcgi doesn't know anything about
Ruby, to know which libraries the program depends on.

> foo requires bar
> foo changes => autoUpdate is invoked

yes

> bar changes => foo requires a different file?

But if you change bar without changing foo, Apache doesn't know that foo has
to be reloaded.

As for foo itself, it has probably done 'require bar' once at the top of the
file. Even if foo has 'require bar' inside the main loop, require will see
that bar is in $" and will not attempt to reload it.

> what about load and evil(eval)?

load will probably work, but if you load every library inside your main
loop, it will be slow. As for eval, what are you thinking of passing to
eval?

Regards,

Brian.


Fwanklin Hummel - 18 Linwood St, Unit 3, Boston, MA 02119 (617) 541-3834

7/27/2011 2:48:00 AM

0

The DemocRAT Hall Of Shame http://www.democrathallof... asks
"Why do you always LIE?"


Courtesy of Buster Norris:

On Sat, 13 Nov 2010 05:39:22 -0800 (PST), Franklin Hummel
<franklin.hummel@gmail.com> wrote:
>In our America, KAATN, there always seems to be about 20-25% on our
>citizens, now known as the Republicans and the Teabaggers......

LIAR!!!!!!!!!!!!!!!!!!!

45%!!!!!!!!!! HAAAAAAAAAAAAAAA!!!!!!!!!!!!!

CBS News/New York Times Poll. Oct. 21-26, 2010.
Democratic Party
Favorable 46%
Not favorable 48%

CNN/Opinion Research Corporation Poll. Sept. 1-2, 2010.
Democratic Party
Favorable 44%
Not favorable 49%

USA Today/Gallup Poll. May 24-25, 2010.
Democratic Party
Favorable 44%
Not favorable 49%

FOX News/Opinion Dynamics Poll. April 6-7, 2010.
Democratic Party
Favorable 42%
Not favorable 49%

http://www.pollingreport.com/institut2.htm...

Posted from:
The DemocRATs Hall of Shame!
http://www.democrathallof...