3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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');
26 class QueueHandler extends Daemon {
30 function QueueHandler($id=NULL) {
36 function class_name() {
37 return ucfirst($this->transport()) . 'Handler';
41 return strtolower($this->class_name().'.'.$this->get_id());
48 function set_id($id) {
52 function transport() {
62 function handle_notice($notice) {
67 if (!$this->start()) {
70 $this->log(LOG_INFO, 'checking for queued notices');
71 $transport = $this->transport();
73 $qi = Queue_item::top($transport);
75 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
76 $notice = Notice::staticGet($qi->notice_id);
78 $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
79 # XXX: what to do if broadcast fails?
80 $result = $this->handle_notice($notice);
82 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
86 $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
89 $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
94 $this->log(LOG_WARNING, 'queue item for notice that does not exist');
101 $this->clear_old_claims();
105 if (!$this->finish()) {
111 function idle($timeout=0) {
117 function clear_old_claims() {
118 $qi = new Queue_item();
119 $qi->transport = $this->transport();
120 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
121 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
126 function log($level, $msg) {
127 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);