3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 define('CLAIM_TIMEOUT', 1200);
22 if (!defined('LACONICA')) { exit(1); }
24 require_once(INSTALLDIR.'/lib/daemon.php');
25 require_once(INSTALLDIR.'/classes/Queue_item.php');
26 require_once(INSTALLDIR.'/classes/Notice.php');
28 class QueueHandler extends Daemon
33 function QueueHandler($id=null)
42 return ucfirst($this->transport()) . 'Handler';
47 return strtolower($this->class_name().'.'.$this->get_id());
73 function handle_notice($notice)
78 function db_dispatch() {
80 $qi = Queue_item::top($this->transport());
82 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
83 $notice = Notice::staticGet($qi->notice_id);
85 $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
86 # XXX: what to do if broadcast fails?
87 $result = $this->handle_notice($notice);
89 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
93 $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
96 $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
101 $this->log(LOG_WARNING, 'queue item for notice that does not exist');
108 $this->clear_old_claims();
114 function stomp_dispatch() {
116 // use an external message queue system via STOMP
117 require_once("Stomp.php");
119 $server = common_config('queue','stomp_server');
120 $username = common_config('queue', 'stomp_username');
121 $password = common_config('queue', 'stomp_password');
123 $con = new Stomp($server);
125 if (!$con->connect($username, $password)) {
126 $this->log(LOG_ERR, 'Failed to connect to queue server');
130 $queue_basename = common_config('queue','queue_basename');
131 // subscribe to the relevant queue (format: basename-transport)
132 $con->subscribe('/queue/'.$queue_basename.'-'.$this->transport());
135 $frame = $con->readFrame();
137 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($frame->headers['created']));
139 // XXX: Now the queue handler receives only the ID of the
140 // notice, and it has to get it from the DB
141 // A massive improvement would be avoid DB query by transmitting
142 // all the notice details via queue server...
143 $notice = Notice::staticGet($frame->body);
146 $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
147 $result = $this->handle_notice($notice);
149 // if the msg has been handled positively, ack it
150 // and the queue server will remove it from the queue
152 $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
156 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
162 $this->log(LOG_WARNING, 'queue item for notice that does not exist');
172 if (!$this->start()) {
175 $this->log(LOG_INFO, 'checking for queued notices');
176 if (common_config('queue','subsystem') == 'stomp') {
177 $this->stomp_dispatch();
180 $this->db_dispatch();
182 if (!$this->finish()) {
188 function idle($timeout=0)
195 function clear_old_claims()
197 $qi = new Queue_item();
198 $qi->transport = $this->transport();
199 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
200 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
205 function log($level, $msg)
207 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);