git-instaweb: Wait for server to start before running web browser
authorJakub Narebski <jnareb@gmail.com>
Fri, 28 May 2010 19:11:24 +0000 (21:11 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 2 Jun 2010 18:49:33 +0000 (11:49 -0700)
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>
git-instaweb.sh
index a8c5dc0ee2202606176caae38fdcbb297296cabd..dc8478f1fe66b6e394a8c58c75384aaab42edc91 100755 (executable)
@@ -117,6 +117,19 @@ stop_httpd () {
        rm -f "$fqgitdir/pid"
 }
 
+httpd_is_ready () {
+       "$PERL" -MIO::Socket::INET -e "
+local \$| = 1; # turn on autoflush
+exit if (IO::Socket::INET->new('127.0.0.1:$port'));
+print 'Waiting for \'$httpd\' to start ..';
+do {
+       print '.';
+       sleep(1);
+} until (IO::Socket::INET->new('127.0.0.1:$port'));
+print qq! (done)\n!;
+"
+}
+
 while test $# != 0
 do
        case "$1" in
@@ -414,7 +427,7 @@ start_httpd
 url=http://127.0.0.1:$port
 
 if test -n "$browser"; then
-       git web--browse -b "$browser" $url || echo $url
+       httpd_is_ready && git web--browse -b "$browser" $url || echo $url
 else
-       git web--browse -c "instaweb.browser" $url || echo $url
+       httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
 fi