Обсуждение: Script Hangs on

Поиск
Список
Период
Сортировка

Script Hangs on

От
Ariunbold Gerelt-Od
Дата:
By Php scripts I am connecting to database SERVER:

<?php
$port=9939;
$host=\"210.111.1.10\";
pg=pg_connect($port,$host);
?>

But problem is if somebody suddenly switch off
the server(210.111.1.10), the script hangs on.
How would my script check that host is open before connecting?




Re: Script Hangs on

От
Grant
Дата:
> <?php
> $port=9939;
> $host=\"210.111.1.10\";
> pg=pg_connect($port,$host);
> ?>

Hehe, don't escape the quotes around your variables.

Here's a function I use to connect to the database.

$dbhost = '210.111.1.10';
$dbuser = 'postgres';
$dbpasswd = 'meep';
$dbname = 'your_db';

function establish_db_connection() {
    global $dbhost;
    global $dbuser;
    global $dbpasswd;
    global $dbname;
    global $dbconn1;
    global $admin_email;

    $dbconn1 = @pg_connect ("host=$dbhost dbname=$dbname user=$dbuser
password=$dbpasswd");
    if (!($dbconn1)) {
        echo "<BR><CENTER>Could not establish database connection.
Administrators have been notified.</CENTER><BR>";
        // mail("$admin_email", "$group database connectivity is down.",
"The database is currently down", "From: $group@$SERVER_NAME\n");
        exit;
    }
}

> But problem is if somebody suddenly switch off
> the server(210.111.1.10), the script hangs on.
> How would my script check that host is open before connecting?

Edit /usr/local/lib/php.ini and make sure persistent connections is
turned off.

Goodluck.


Re: Script Hangs on

От
Ariunbold Gerelt-Od
Дата:
Frist ,How in script could I Ping?
Second,from command line Ping also hangs.


Csaba Bobak wrote:

> > <?php
> > $port=9939;
> > $host=\"210.111.1.10\";
> > pg=pg_connect($port,$host);
> > ?>
>
> > But problem is if somebody suddenly switch off
> > the server(210.111.1.10), the script hangs on.
> > How would my script check that host is open before connecting?
>
> A simple ping maybe? :-)
>
> Csaba
>
> __________
> This message went through virus scan at Trend Ltd. which stated
> the message was clean of viri appeared before 2001.05.24.


Re: Script Hangs on

От
Csaba Bobak
Дата:
> <?php
> $port=9939;
> $host=\"210.111.1.10\";
> pg=pg_connect($port,$host);
> ?>

> But problem is if somebody suddenly switch off
> the server(210.111.1.10), the script hangs on.
> How would my script check that host is open before connecting?


A simple ping maybe? :-)


Csaba



__________
This message went through virus scan at Trend Ltd. which stated
the message was clean of viri appeared before 2001.05.24.

Re: Script Hangs on

От
Melvyn Sopacua
Дата:
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@domain.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..");
}
?>