I have a script that connect to server. I need to be able to reconnect on a disconnect. The first reconnect part of my script works on the initial connect.. But if the connection is broken during the run, doesn't try to reconnect at all. Saying that 0 is greater than 120.
PHP Code:
<?php
// Run "forever"
set_time_limit(0);
/**
* Database setup, connection and db selection
*/
include('dbInclude.php');
/**
* Error logging
*/
include('classes/ErrorLogger.class.php');
$err = new ErrorLogger('/var/www/html/agent/errors.log');
/**
* Configuration of reconnection stuff
*/
$reconnect = 1; // Reconnect?
$maxRetries = 120; // Max reconnect retries
$retryWait = 1000000; // Wait before reconnection try in milliseconds
$tries = 0;
// Open the socket to the Asterisk Manager
$socket = @fsockopen('127.0.0.1','5038', $errno, $errstr);
do
{
// have we hit the max # of retries?
if($tries >= $maxRetries)
{
$err->logError('manager.php', 'fsocketopen', 'Could not connect after '.intval($tries).' retries');
die('Could not connect to the manager');
}
++$tries;
usleep($retryWait);
}while(!$socket);
}
else
{
$err->logError('manager.php', 'fsocketopen', 'Could not connect - No retries');
die('Could not connect to the manager');
}
}
// Login to the manager and turn events on
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: admin\r\n");
fputs($socket, "Secret: xxxx\r\n\r\n");
fputs($socket, "Events: on\r\n");
// Initialize an empty buffer - prevent a warning
$buffer = "";
// Array to track the calls
$calls = array();
stream_set_blocking($socket, TRUE);
// Loop the the CLI output
do
{
// Double check our stream
if(feof($socket))
{
// Try again?
if($reconnect === 1)
{
$tries = 0;
$socket = 0;
do
{
// have we hit the max # of retries?
if($tries >= $maxRetries)
{
$err->logError('manager.php', 'fsocketopen', 'Could not reconnect after '.$tries.' retries');
die('Could not reconnect to the manager');
}
Do you know which comparison is failing? Where does it say that "0 is greater than 120"?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
do { // have we hit the max # of retries? if($tries >= $maxRetries) { $err->logError('manager.php', 'fsocketopen', 'Could not reconnect after '.$tries.' retries'); die('Could not reconnect to the manager'); }
You might want to add a error_reporting(E_ALL); to the start of the script to make sure your error log is catching all errors/notices, at least for now. Maybe the script is timing out, maxing out on memory, or some other non-obvious thing?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I have a script that connect to server. I need to be able to reconnect on a disconnect. The first reconnect part of my script works on the initial connect.. But if the connection is broken during the run, doesn't try to reconnect at all. Saying that 0 is greater than 120.
PHP Code:
<?php
// Run "forever"
set_time_limit(0);
/**
* Database setup, connection and db selection
*/
include('dbInclude.php');
/**
* Error logging
*/
include('classes/ErrorLogger.class.php');
$err = new ErrorLogger('/var/www/html/agent/errors.log');
/**
* Configuration of reconnection stuff
*/
$reconnect = 1; // Reconnect?
$maxRetries = 120; // Max reconnect retries
$retryWait = 1000000; // Wait before reconnection try in milliseconds
$tries = 0;
// Open the socket to the Asterisk Manager
$socket = @fsockopen('127.0.0.1','5038', $errno, $errstr);
do
{
// have we hit the max # of retries?
if($tries >= $maxRetries)
{
$err->logError('manager.php', 'fsocketopen', 'Could not connect after '.intval($tries).' retries');
die('Could not connect to the manager');
}
++$tries;
usleep($retryWait);
}while(!$socket);
}
else
{
$err->logError('manager.php', 'fsocketopen', 'Could not connect - No retries');
die('Could not connect to the manager');
}
}
// Login to the manager and turn events on
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: admin\r\n");
fputs($socket, "Secret: xxxx\r\n\r\n");
fputs($socket, "Events: on\r\n");
// Initialize an empty buffer - prevent a warning
$buffer = "";
// Array to track the calls
$calls = array();
stream_set_blocking($socket, TRUE);
// Loop the the CLI output
do
{
// Double check our stream
if(feof($socket))
{
// Try again?
if($reconnect === 1)
{
$tries = 0;
$socket = 0;
do
{
// have we hit the max # of retries?
if($tries >= $maxRetries)
{
$err->logError('manager.php', 'fsocketopen', 'Could not reconnect after '.$tries.' retries');
die('Could not reconnect to the manager');
}
// Login back in.
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: admin\r\n");
fputs($socket, "Secret: xxxx\r\n\r\n");
fputs($socket, "Events: on\r\n");
}
else
{
$err->logError('manager.php', 'fsocketopen', 'Stream disconnected - No retries');
die('Stream to manager disconnected');
}
}
}
Im not really sure, but maybe something like this will work.. I see your redeclaring $server several times. Maybe if you just use someting like this:
PHP Code:
// Connection failed
if(!$socket = @fsockopen('127.0.0.1','5038', $errno, $errstr)){
// Try again?
if($reconnect === 1){
$tries = 0;
do {
// have we hit the max # of retries?
if($tries >= $maxRetries){
$err->logError('manager.php', 'fsocketopen', 'Could not connect after '.intval($tries).' retries');
die('Could not connect to the manager');
}
++$tries;
usleep($retryWait);
}
while(!$socket = fsockopen('127.0.0.1','5038', $errno, $errstr));
}
}
Going to be honest though, I havent a clue. just maybe a suggestion. its something I would try in my trial-and-error process.
During my test on the code last night, I would stop the server that it was connected to.. Then start it back up and it worked. But this morning, the script died with the message basically stating 2 is greater than 120. I'm thinking that when ever that server get's restarted, there's something that could killing that process, but I don't see how it would end up triggering the errors. I did a couple more changes this afternoon, some of my error handling was jacked up.. I'll find out again in the morning.
Ok - after fixing my error logging calls, I ended up with another story for why the script was dying. My MySQL wrapper wasn't reselecting the DB after reconnect. Doh.. Of course we'll see tomorrow if it's all good.
Here's the wrapper I've made...
PHP Code:
<?php
class DBWrapper
{
private $host = 0; // DB host address or url
private $user = 0; // DB user name
private $pass = 0; // DB password
private $link = 0; // DB Link - Keeps the sessions seperated
private $db = 0; // DB DB
private $sql = 0; // MySQL query string
private $res = 0; // Results from query
private $reconnect = 0; // Reconnect?
private $maxRetries = 0; // Max reconnect retries
private $retryWait = 0; // Wait before reconnection try in milliseconds
private $error = 0; // Error messages
public function __construct($config)
{
// Configure the private properties from an array
foreach($config as $key=>$val)
{
$this->{$key} = $val;
}
// Connect to the DB
$this->Connect(1);
}
// Connect to the DB
private function Connect($init = 0)
{
$this->link = @mysql_connect($this->host, $this->user, $this->pass, 1);
// Could not connect
if(!$this->link)
{
// Try to reconnect
if($this->reconnect)
{
$this->link = 0;
$tries = 0;
do
{
// have we hit the max # of retries?
if($this->maxRetries <= $tries)
{
if($init === 1)
{
$this->error = 'Could not connect to: '.$this->host;
}
else
{
$this->error = 'Could not reconnect to: '.$this->host;
}
return 0;
}
}while(!$this->$link);
}
else
{
$this->error = 'Could not connect to: '.$this->host;
return 0;
}
}
// Make sure the DB is selected
$this->SelectDB();
return 1;
}
// Select the DB
private function SelectDB()
{
if(!mysql_select_db($this->db, $this->link))
{
$this->error = 'Could not select the DB: '.$this->db;
return 0;
}
return 1;
}
public function Query($sql)
{
// Store query sting
$this->sql = $sql;
// Query the DB
$this->res = mysql_query($sql, $this->link);
// Error
if(!$this->res)
{
// Disconnected?
switch(mysql_errno($this->link))
{
// Disconnected error code
case 2001:
case 2002:
case 2003:
case 2004:
case 2005:
case 2006:
case 2011:
case 2012:
case 2013:
// Reconnect?
if($this->reconnect === 1)
{
if(!$this->Connect())
{
$this->error = $this->error.'<br />Query was ::: '.$sql;
return 0;
}
}
else
{
$this->error = $sql.' ::: '.mysql_error($this->link);
return 0;
}
break;
Bookmarks