Implemented rcube::sleep() method for disconnecting all external connection in long-running/sleeping scripts

pull/5335/merge
Aleksander Machniak 8 years ago
parent a227589eb2
commit c1c0a0d815

@ -209,6 +209,26 @@ class rcube
}
$this->memcache = new Memcache;
$this->memcache_init();
// test connection and failover (will result in $this->mc_available == 0 on complete failure)
$this->memcache->increment('__CONNECTIONTEST__', 1); // NOP if key doesn't exist
if (!$this->mc_available) {
$this->memcache = false;
}
}
return $this->memcache;
}
/**
* Get global handle for memcache access
*
* @return object Memcache
*/
protected function memcache_init()
{
$this->mc_available = 0;
// add all configured hosts to pool
@ -228,16 +248,6 @@ class rcube
$this->mc_available += intval($this->memcache->addServer(
$host, $port, $pconnect, 1, $timeout, $retry_interval, false, array($this, 'memcache_failure')));
}
// test connection and failover (will result in $this->mc_available == 0 on complete failure)
$this->memcache->increment('__CONNECTIONTEST__', 1); // NOP if key doesn't exist
if (!$this->mc_available) {
$this->memcache = false;
}
}
return $this->memcache;
}
/**
@ -1021,6 +1031,40 @@ class rcube
$this->shutdown_functions[] = $function;
}
/**
* When you're going to sleep the script execution for a longer time
* it is good to close all external connections (sql, memcache, SMTP, IMAP).
*
* No action is required on wake up, all connections will be
* re-established automatically.
*/
public function sleep()
{
foreach ($this->caches as $cache) {
if (is_object($cache)) {
$cache->close();
}
}
if ($this->storage) {
$this->storage->close();
}
if ($this->db) {
$this->db->closeConnection();
}
if ($this->memcache) {
$this->memcache->close();
// after close() need to re-init memcache
$this->memcache_init();
}
if ($this->smtp) {
$this->smtp->disconnect();
}
}
/**
* Quote a given string.
* Shortcut function for rcube_utils::rep_specialchars_output()

@ -236,6 +236,10 @@ class rcube_cache
// reset internal cache index, thanks to this we can force index reload
$this->index = null;
$this->index_changed = false;
$this->cache = array();
$this->cache_sums = array();
$this->cache_changes = array();
}
/**
@ -642,15 +646,17 @@ class rcube_cache
$this->max_packet -= 2000;
}
else if ($this->type == 'memcache') {
$stats = $this->db->getStats();
if ($stats = $this->db->getStats()) {
$remaining = $stats['limit_maxbytes'] - $stats['bytes'];
$this->max_packet = min($remaining / 5, $this->max_packet);
}
}
else if ($this->type == 'apc' && function_exists('apc_sma_info')) {
$stats = apc_sma_info();
if ($stats = apc_sma_info()) {
$this->max_packet = min($stats['avail_mem'] / 5, $this->max_packet);
}
}
}
return $this->max_packet;
}

@ -231,6 +231,10 @@ class rcube_cache_shared
// reset internal cache index, thanks to this we can force index reload
$this->index = null;
$this->index_changed = false;
$this->cache = array();
$this->cache_sums = array();
$this->cache_changes = array();
}
/**
@ -629,15 +633,17 @@ class rcube_cache_shared
$this->max_packet -= 2000;
}
else if ($this->type == 'memcache') {
$stats = $this->db->getStats();
if ($stats = $this->db->getStats()) {
$remaining = $stats['limit_maxbytes'] - $stats['bytes'];
$this->max_packet = min($remaining / 5, $this->max_packet);
}
}
else if ($this->type == 'apc' && function_exists('apc_sma_info')) {
$stats = apc_sma_info();
if ($stats = apc_sma_info()) {
$this->max_packet = min($stats['avail_mem'] / 5, $this->max_packet);
}
}
}
return $this->max_packet;
}

@ -785,6 +785,20 @@ class rcube_db
$this->last_result = null;
}
/**
* Terminate database connection.
*/
public function closeConnection()
{
$this->db_connected = false;
$this->db_index = 0;
// release statement and connection resources
$this->last_result = null;
$this->dbh = null;
$this->dbhs = array();
}
/**
* Formats input so it can be safely used in a query
*

@ -601,4 +601,18 @@ class rcube_db_oracle extends rcube_db
return $this->last_result = $this->dbh->rollBack();
}
/**
* Terminate database connection.
*/
public function closeConnection()
{
// release statement and close connection(s)
$this->last_result = null;
foreach ($this->dbhs as $dbh) {
oci_close($dbh);
}
parent::closeConnection();
}
}

@ -204,7 +204,9 @@ class rcube_imap extends rcube_storage
*/
public function close()
{
$this->connect_done = false;
$this->conn->closeConnection();
if ($this->mcache) {
$this->mcache->close();
}

Loading…
Cancel
Save