@matheusbsbs / matheus-bagliano / commits / d94775e1f9
Fork 0

git-instaweb: Wait for server to start before running web browser

Add generic httpd_is_ready subroutine, which busy-waits for web server to be started, by checking if $port is opened on localhost. This is used to avoid situation where web browser is started before web server is ready to accept connection, and fails. It uses IO::Socket::INET module, which is core Perl module since v5.6.0. Alternate solution, possible for those web servers that can run arbitrary code hooks after they bind the listen socket (after they start accepting connections), would be to use some kind of blocking mechanism: FIFO or lockfile, see http://thread.gmane.org/gmane.comp.version-control.git/147337/focus=147566 This can be always added later, as a web server specific branch in httpd_is_ready function. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Acked-by: Petr Baudis <pasky@suse.cz> Acked-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed May 28, 2010 at 21:11 UTC d94775e1f9a9239a823b9aa52d754f9ec2635e6d
1 file changed +15 -2
git-instaweb.sh
+15 -2
@@ -117,6 +117,19 @@ stop_httpd () {
117 rm -f "$fqgitdir/pid"
118 }
119
120 +httpd_is_ready () {
121 + "$PERL" -MIO::Socket::INET -e "
122 +local \$| = 1; # turn on autoflush
123 +exit if (IO::Socket::INET->new('127.0.0.1:$port'));
124 +print 'Waiting for \'$httpd\' to start ..';
125 +do {
126 + print '.';
127 + sleep(1);
128 +} until (IO::Socket::INET->new('127.0.0.1:$port'));
129 +print qq! (done)\n!;
130 +"
131 +}
132 +
133 while test $# != 0
134 do
135 case "$1" in
@@ -414,7 +427,7 @@ start_httpd
427 url=http://127.0.0.1:$port
428
429 if test -n "$browser"; then
417 - git web--browse -b "$browser" $url || echo $url
430 + httpd_is_ready && git web--browse -b "$browser" $url || echo $url
431 else
419 - git web--browse -c "instaweb.browser" $url || echo $url
432 + httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
433 fi