Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect after 30 minutes of inactivity, done better #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion ApnsPHP/Push/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
class ApnsPHP_Push_Server extends ApnsPHP_Push
{
const MAIN_LOOP_USLEEP = 200000; /**< @type integer Main loop sleep time in micro seconds. */
const MAIN_LOOP_GRACE = 1800; /**< @type integer Main loop grace time in seconds. */
const SHM_SIZE = 524288; /**< @type integer Shared memory size in bytes useful to store message queues. */
const SHM_MESSAGES_QUEUE_KEY_START = 1000; /**< @type integer Message queue start identifier for messages. For every process 1 is added to this number. */
const SHM_ERROR_MESSAGES_QUEUE_KEY = 999; /**< @type integer Message queue identifier for not delivered messages. */
Expand Down Expand Up @@ -272,9 +273,26 @@ public function getErrors($bEmpty = true)
*/
protected function _mainLoop()
{
$last_message = time();

while (true) {
pcntl_signal_dispatch();

if ( $last_message<(time()-self::MAIN_LOOP_GRACE) ) {
$last_message = time();

try {
$this->_log('WARNING: reconnecting (Last message: '. strftime('%F %T', $last_message) .')...');

parent::disconnect();
usleep(self::MAIN_LOOP_USLEEP);
parent::connect();
} catch (ApnsPHP_Exception $e) {
$this->_log('ERROR: ' . $e->getMessage() . ', exiting...');
exit(1);
}
}

if (posix_getppid() != $this->_nParentPid) {
$this->_log("INFO: Parent process {$this->_nParentPid} died unexpectedly, exiting...");
break;
Expand All @@ -287,6 +305,8 @@ protected function _mainLoop()

$aQueue = $this->_getQueue(self::SHM_MESSAGES_QUEUE_KEY_START, $this->_nCurrentProcess);
foreach($aQueue as $message) {
$last_message = time();

parent::add($message);
}
$this->_setQueue(self::SHM_MESSAGES_QUEUE_KEY_START, $this->_nCurrentProcess);
Expand Down Expand Up @@ -335,4 +355,4 @@ protected function _setQueue($nQueueKey, $nProcess = 0, $aQueue = array())
}
return shm_put_var($this->_hShm, $nQueueKey + $nProcess, $aQueue);
}
}
}