From c4c6aa228d8767a33820613568f1ebe9e0172297 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sat, 6 Oct 2012 18:59:32 +0000 Subject: [PATCH] Improved robustness of server startup --- Makefile | 4 ++-- Net-Server-NNTP/lib/Net/Server/NNTP.pm | 14 ++++++++++++-- newsd | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 306b50b..54a369c 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,8 @@ control.ctl: newsdb: -dropdb news createdb -O news -T template0 -E SQL_ASCII news - psql -d news -c 'alter database news set search_path to nntp,public' - psql -d news -c 'alter database news set client_min_messages to warning' + $(PSQL) -c 'alter database news set search_path to nntp,public' + $(PSQL) -c 'alter database news set client_min_messages to warning' preserve: sudo su -c 'psql -1 --set ON_ERROR_STOP=1 -U news -d news -f preserve.sql' news diff --git a/Net-Server-NNTP/lib/Net/Server/NNTP.pm b/Net-Server-NNTP/lib/Net/Server/NNTP.pm index 3b6eb21..bcc881f 100644 --- a/Net-Server-NNTP/lib/Net/Server/NNTP.pm +++ b/Net-Server-NNTP/lib/Net/Server/NNTP.pm @@ -1108,10 +1108,20 @@ sub connect_to_storage { # TODO use a config parameter optionally here my $dsn = $ENV{'DBI_DSN'}; - $dsn = 'dbi:Pg:dbname=news' unless defined $dsn; + $dsn = qq{dbi:Pg:dbname=news} unless defined $dsn; $s->log(4, "connecting to $dsn"); - $s->{db} = DBI->connect($dsn,'','',{AutoCommit => 0,RaiseError=>1}); + eval { + $s->{db} = DBI->connect($dsn,'','',{AutoCommit => 0,RaiseError=>1}); + $s->log(4, "Can't connect to DB: $DBI::errstr") unless $s->{db}; + }; + if ($@) { + $s->log(4, "Can't connect to DB: $DBI::errstr"); + die $@; + } + $s->log(4, "connected to $dsn"); + + # TODO abort if can't connect $s->{db}->{PrintError} = 0; $s->{db}->do("set CLIENT_ENCODING to 'SQL_ASCII'"); diff --git a/newsd b/newsd index a84a5a3..babb9f6 100755 --- a/newsd +++ b/newsd @@ -3,6 +3,8 @@ use strict; use warnings; +$ENV{PGHOST} ||= '/var/run/postgresql'; + use Net::Server::NNTP::Postgres; use Carp; -- 2.40.0