git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] git-instaweb: Wait for server to start before running web browser

From
Jakub Narebski <jnareb@gmail.com>
Date
May 28, 2010, 19:11 UTC
Message-ID
<1275073885-26537-3-git-send-email-jnareb@gmail.com>
In-Reply-To
<1275073885-26537-1-git-send-email-jnareb@gmail.com>

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>
---
This was required to test support for 'plackup' web server in git-instaweb
(the next patch in this series), because default pure-Perl web server used
by Plack, namely HTTP::Server::PSGI, can take a while to start.
 git-instaweb.sh |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/git-instaweb.sh b/git-instaweb.sh
index a8c5dc0..dc8478f 100755
--- a/git-instaweb.sh
+++ b/git-instaweb.sh
@@ -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
-- 
1.7.0.1
Previous: Jakub NarebskiNext: Pavan Kumar Sunkara
Message 3 of 9 in “git-instaweb: Support for 'plackup' and improvements”
  1. 0/3 git-instaweb: Support for 'plackup' and improvementsJakub Narebski, May 28, 2010
  2. 1/3 git-instaweb: Remove pidfile after stopping web serverJakub Narebski, May 28, 2010
  3. 2/3 git-instaweb: Wait for server to start before running web browserJakub Narebski, May 28, 2010
  4. Pavan Kumar SunkaraMay 28, 2010
  5. Jakub NarebskiMay 28, 2010
  6. 3/3 git-instaweb: Add support for running gitweb via 'plackup'Jakub Narebski, May 28, 2010
  7. Eric WongMay 29, 2010
  8. Eric WongMay 29, 2010
  9. Petr BaudisMay 31, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.