Skip site navigation (1) Skip section navigation (2)

Re: Script Hangs on

From: Melvyn Sopacua <mdev(at)idg(dot)nl>
To: PostgreSQL Adminlist <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Script Hangs on
Date: 2001-06-06 12:42:45
Message-ID: 5.0.2.1.2.20010606134350.02aa3ca0@tmp.idg.nl (view raw or flat)
Thread:
Lists: pgsql-adminpgsql-php
At 07:21 6-6-01, you wrote:

>    $dbconn1 = @pg_connect ("host=$dbhost dbname=$dbname user=$dbuser
>password=$dbpasswd");
>
>Edit /usr/local/lib/php.ini and make sure persistent connections is
>turned off.

This will work, as long as the machine is up, but pgsql isn't. However, if the
machine isn't up, the timeout is tremendous.

Use this, or view source online at 
http://melvyn.idg.nl/phpsources/db_connect_plus.phps:
<?
//These are included from the global application configuration
$cfg_vars['data_root']='/cgi-data';
$cfg_vars['appname']='your app';
$cfg_vars['appemail']='your app email sender';
$cfg_vars['pg_admin']='pgadmin(at)domain(dot)com';

function db_connect_plus()
{
         global $cfg_vars;
         $primary='your_ip_primary';
         $secondary='your_ip_secondary';
         $port='your_port';
         $username='username';
         $password='blabla';
         $timeout=10;
         $now=time();
         $skipcheck=10;
         $primcheck=$cfg_vars[data_root].'/primary_down';
         $seccheck=$cfg_vars[data_root].'/secondary_down';
         $appname=$cfg_vars['appname'];
         $appemail=$cfg_vars['appemail'];
         $connect='pg_connect'; // Choose your connection type (db type + 
persistent/not persistent) here.
         if(file_exists($primcheck))
         {
                 $lastmod=filemtime($primcheck);
                 if($now - $lastmod > $skipcheck)
                 {
                         if($sp=fsockopen($primary, $port, &$errno, 
&$errstr, $timeout))
                         {
                                 fclose($sp);
                                 unlink($primcheck);
                                 $is_mailed=mail($cfg_vars[pg_admin], 
"Primary $primary back up.", date("l dS of F Y h:i:s A", $now), "From: 
$appname <$appemail>");
                                 return $connect($primary, $username, 
$password);
                         }
                         else
                         {
                                 unlink($primcheck);
                                 touch($primcheck);
                                 mail($cfg_vars[pg_admin], "Primary 
$primary down", "$errno\n$errstr", "From: $appname <$appemail>\nX-Priority: 
1 (Highest)\nX-MSMail-Priority: High");
                         }
                 }
         }
         else
         {
                 if($sp=fsockopen($primary, $port, &$errno, &$errstr, 
$timeout))
                 {
                         fclose($sp);
                         return $connect($primary, $username, $password);
                 }
                 else
                 {
                         touch($primcheck);
                         mail($cfg_vars[pg_admin], "Primary $primary down", 
"$errno\n$errstr", "From: $appname <$appemail>\nX-Priority: 1 
(Highest)\nX-MSMail-Priority: High");
                 }
         }
         //If we get this far, the primary is down, so do the secondary.
         if(file_exists($seccheck))
         {
                 $lastmod=filemtime($seccheck);
                 if($now - $lastmod > $skipcheck)
                 {
                         if($sp=fsockopen($secondary, $port, &$errno, 
&$errstr, $timeout))
                         {
                                 fclose($sp);
                                 unlink($seccheck);
                                 mail($cfg_vars[pg_admin], "Secondary 
$secondary back up.", date("l dS of F Y h:i:s A", $now), "From: $appname 
<$appemail>");
                                 return $connect($secondary, $username, 
$password);
                         }
                         else
                         {
                                 unlink($seccheck);
                                 touch($seccheck);
                                 mail($cfg_vars[pg_admin], "secondary 
$secondary down", "$errno\n$errstr", "From: $appname 
<$appemail>\nX-Priority: 1 (Highest)\nX-MSMail-Priority: High");
                         }
                 }
         }
         else
         {
                 if($sp=fsockopen($secondary, $port, &$errno, &$errstr, 
$timeout))
                 {
                         fclose($sp);
                         return $connect($secondary, $username, $password);
                 }
                 else
                 {
                         touch($seccheck);
                         mail($cfg_vars[pg_admin], "secondary $secondary 
down", "$errno\n$errstr", "From: $appname <$appemail>");
                 }
         }
         //if we get this far, both are down!
         return 0;
}

$cs=db_connect_plus();
if(!$cs)
{
         die("Oh oh - 2 database servers down. And you think you're 
redundant..");
}
?>


In response to

pgsql-admin by date

Next:From: Nick FankhauserDate: 2001-06-06 14:55:55
Subject: Intentionally splitting data in a table across files.
Previous:From: Sam StatonDate: 2001-06-06 10:07:07
Subject: Re: What CASE tools and clients for Postgres?

pgsql-php by date

Next:From: Carlos Felipe ZirbesDate: 2001-06-07 17:11:15
Subject: Pool of connections
Previous:From: Ariunbold Gerelt-OdDate: 2001-06-06 06:01:05
Subject: Re: [ADMIN] Script Hangs on

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group