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);
26 function QueueHandler($id=NULL) {
32 function class_name() {
33 return ucfirst($this->transport()) . 'Handler';
40 function set_id($id) {
44 function transport() {
54 function handle_notice($notice) {
58 function handle_queue() {
59 $this->log(LOG_INFO, 'checking for queued notices');
61 $transport = $this->transport();
63 $qi = Queue_item::top($transport);
65 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
66 $notice = Notice::staticGet($qi->notice_id);
68 $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
69 # XXX: what to do if broadcast fails?
70 $result = $this->handle_notice($notice);
72 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
76 $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
79 $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
82 $this->log(LOG_WARNING, 'queue item for notice that does not exist');
87 $this->clear_old_claims();
93 function clear_old_claims() {
94 $qi = new Queue_item();
95 $qi->transport = $this->transport();
96 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
97 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
100 function log($level, $msg) {
101 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);