many jabber queue management changes

Added a method to QueueManager to let subclasses do stuff when idle.
Needed so that XMPP queue manager can service its message queue.

Cleaned up jabber_broadcast_message quite a bit. Use custom joins
instead of loop-and-query, should fix some problems with users who are
getting messages even after turning off notification. Only build $msg
and $entry once, and use the XMPPHP function for messages with a
payload, rather than rolling our own.

darcs-hash:20080829200352-84dde-427e4ca8c81d4222a36f78e7c580b611ff0bf765.gz
This commit is contained in:
Evan Prodromou 2008-08-29 16:03:52 -04:00
parent a0f726fb6c
commit f5fe013657
3 changed files with 79 additions and 76 deletions

View File

@ -25,25 +25,6 @@ require_once('XMPPHP/XMPP.php');
class Laconica_XMPP extends XMPPHP_XMPP { class Laconica_XMPP extends XMPPHP_XMPP {
function messageplus($to, $body, $type = 'chat', $subject = null, $payload = null) {
$to = htmlspecialchars($to);
$body = htmlspecialchars($body);
$subject = htmlspecialchars($subject);
$jid = jabber_daemon_address();
$out = "<message from='$jid' to='$to' type='$type'>";
if($subject) $out .= "<subject>$subject</subject>";
$out .= "<body>$body</body>";
if($payload) $out .= $payload;
$out .= "</message>";
$cnt = strlen($out);
common_log(LOG_DEBUG, "Sending $cnt chars to $to");
$this->send($out);
common_log(LOG_DEBUG, 'Done.');
}
public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) { public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
if($type == 'available') $type = ''; if($type == 'available') $type = '';
$to = htmlspecialchars($to); $to = htmlspecialchars($to);
@ -134,7 +115,7 @@ function jabber_send_notice($to, $notice) {
} }
$msg = jabber_format_notice($profile, $notice); $msg = jabber_format_notice($profile, $notice);
$entry = jabber_format_entry($profile, $notice); $entry = jabber_format_entry($profile, $notice);
$conn->messageplus($to, $msg, 'chat', NULL, $entry); $conn->message($to, $msg, 'chat', NULL, $entry);
return true; return true;
} }
@ -142,6 +123,8 @@ function jabber_send_notice($to, $notice) {
function jabber_format_entry($profile, $notice) { function jabber_format_entry($profile, $notice) {
# FIXME: notice url might be remote
$noticeurl = common_local_url('shownotice', $noticeurl = common_local_url('shownotice',
array('notice' => $notice->id)); array('notice' => $notice->id));
$msg = jabber_format_notice($profile, $notice); $msg = jabber_format_notice($profile, $notice);
@ -168,6 +151,10 @@ function jabber_format_entry($profile, $notice) {
$html .= "\n</body>\n"; $html .= "\n</body>\n";
$html .= "\n</html>\n"; $html .= "\n</html>\n";
$address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
$address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
$address .= "</addresses>\n";
$event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n"; $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
$event .= "<items xmlns='http://jabber.org/protocol/pubsub' "; $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
$event .= "node='" . common_local_url('public') . "'>\n"; $event .= "node='" . common_local_url('public') . "'>\n";
@ -175,7 +162,7 @@ function jabber_format_entry($profile, $notice) {
$event .= "</items>\n"; $event .= "</items>\n";
$event .= "</event>\n"; $event .= "</event>\n";
# FIXME: include the pubsub event, too. # FIXME: include the pubsub event, too.
return $html . $entry; return $html . $entry . $address;
# return $entry . "\n" . $event; # return $entry . "\n" . $event;
} }
@ -234,63 +221,54 @@ function jabber_broadcast_notice($notice) {
return true; return true;
} }
$profile = Profile::staticGet($notice->profile_id); $profile = Profile::staticGet($notice->profile_id);
if (!$profile) { if (!$profile) {
common_log(LOG_WARNING, 'Refusing to broadcast notice with ' . common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
'unknown profile ' . common_log_objstring($notice), 'unknown profile ' . common_log_objstring($notice),
__FILE__); __FILE__);
return false; return false;
} }
$sent_to = array();
# First, get users who this is a direct reply to
$reply = new Reply();
$reply->notice_id = $notice->id;
if ($reply->find()) {
while ($reply->fetch()) {
$user = User::staticGet($reply->profile_id);
if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
common_log(LOG_INFO,
'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
__FILE__);
$success = jabber_send_notice($user->jabber, $notice);
if ($success) {
# Remember so we don't send twice
$sent_to[$user->id] = true;
} else {
# XXX: Not sure, but I think that's the right thing to do
common_log(LOG_WARNING,
'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
__FILE__);
return false;
}
}
}
}
# Now, get users subscribed to this profile
# XXX: use a join here rather than looping through results
$sub = new Subscription();
$sub->subscribed = $notice->profile_id;
if ($sub->find()) { $msg = jabber_format_notice($profile, $notice);
while ($sub->fetch()) { $entry = jabber_format_entry($profile, $notice);
$user = User::staticGet($sub->subscriber);
if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) { $sent_to = array();
common_log(LOG_INFO, $conn = jabber_connect();
'Sending notice ' . $notice->id . ' to ' . $user->jabber,
__FILE__); # First, get users to whom this is a direct reply
$success = jabber_send_notice($user->jabber, $notice); $user = new User();
if ($success) { $user->query('SELECT user.id, user.jabber ' .
$sent_to[$user->id] = true; 'FROM user JOIN reply ON user.id = reply.profile_id ' .
} else { 'WHERE reply.notice_id = ' . $notice->id . ' ' .
# XXX: Not sure, but I think that's the right thing to do 'AND user.jabber is not null ' .
common_log(LOG_WARNING, 'AND user.jabbernotify = 1 ' .
'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.', 'AND user.jabberreplies = 1 ');
__FILE__);
return false; while ($user->fetch()) {
} common_log(LOG_INFO,
} 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
__FILE__);
$conn->message($user->jabber, $msg, 'chat', NULL, $entry);
}
# Now, get users subscribed to this profile
$user = new User();
$user->query('SELECT user.id, user.jabber ' .
'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
'AND user.jabber is not null ' .
'AND user.jabbernotify = 1 ');
while ($user->fetch()) {
if (!array_key_exists($user->id, $sent_to)) {
common_log(LOG_INFO,
'Sending notice ' . $notice->id . ' to ' . $user->jabber,
__FILE__);
$conn->message($user->jabber, $msg, 'chat', NULL, $entry);
} }
} }
return true; return true;
} }
@ -305,11 +283,17 @@ function jabber_public_notice($notice) {
# = false? I think not # = false? I think not
if ($public && $notice->is_local) { if ($public && $notice->is_local) {
$msg = jabber_format_notice($profile, $notice);
$entry = jabber_format_entry($profile, $notice);
$sent_to = array();
$conn = jabber_connect();
foreach ($public as $address) { foreach ($public as $address) {
common_log(LOG_INFO, common_log(LOG_INFO,
'Sending notice ' . $notice->id . ' to public listener ' . $address, 'Sending notice ' . $notice->id . ' to public listener ' . $address,
__FILE__); __FILE__);
jabber_send_notice($address, $notice); $conn->message($address, $msg, 'chat', NULL, $entry);
} }
} }

View File

@ -57,7 +57,6 @@ class QueueHandler {
function handle_queue() { function handle_queue() {
$this->log(LOG_INFO, 'checking for queued notices'); $this->log(LOG_INFO, 'checking for queued notices');
$cnt = 0;
$transport = $this->transport(); $transport = $this->transport();
do { do {
$qi = Queue_item::top($transport); $qi = Queue_item::top($transport);
@ -82,14 +81,23 @@ class QueueHandler {
$this->log(LOG_WARNING, 'queue item for notice that does not exist'); $this->log(LOG_WARNING, 'queue item for notice that does not exist');
} }
$qi->delete(); $qi->delete();
$cnt++; $this->idle();
} else { } else {
$this->clear_old_claims(); $this->clear_old_claims();
sleep(10); $start = microtime();
$this->idle();
$used = microtime() - $start;
if ($used < 5000000) {
usleep(5000000 - $used);
}
} }
} while (true); } while (true);
} }
function idle() {
return true;
}
function clear_old_claims() { function clear_old_claims() {
$qi = new Queue_item(); $qi = new Queue_item();
$qi->transport = $this->transport(); $qi->transport = $this->transport();

View File

@ -34,6 +34,8 @@ require_once(INSTALLDIR . '/lib/queuehandler.php');
set_error_handler('common_error_handler'); set_error_handler('common_error_handler');
class XmppQueueHandler extends QueueHandler { class XmppQueueHandler extends QueueHandler {
var $conn = NULL;
function transport() { function transport() {
return 'jabber'; return 'jabber';
@ -42,15 +44,24 @@ class XmppQueueHandler extends QueueHandler {
function start() { function start() {
# Low priority; we don't want to receive messages # Low priority; we don't want to receive messages
$this->conn = jabber_connect($this->_id, NULL, -1); $this->conn = jabber_connect($this->_id, NULL, -1);
$this->conn->addEventHandler('message', 'forward_message', $this);
return !is_null($this->conn); return !is_null($this->conn);
} }
function handle_notice($notice) { function handle_notice($notice) {
return jabber_broadcast_notice($notice); return jabber_broadcast_notice($notice);
} }
function idle() {
# Process the queue for a second
$this->conn->processTime(1);
}
function finish() { function finish() {
} }
function forward_message(&$pl) {
}
} }
mb_internal_encoding('UTF-8'); mb_internal_encoding('UTF-8');