3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Base class for queue handlers.
25 * As extensions of the Daemon class, each queue handler has the ability
26 * to launch itself in the background, at which point it'll pass control
27 * to the configured QueueManager class to poll for updates.
29 * Subclasses must override at least the following methods:
33 #class QueueHandler extends Daemon
37 # function __construct($id=null, $daemonize=true)
39 # parent::__construct($daemonize);
47 * How many seconds a polling-based queue manager should wait between
48 * checks for new items to handle.
50 * Defaults to 60 seconds; override to speed up or slow down.
52 * @fixme not really compatible with global queue manager
53 * @return int timeout in seconds
60 # function class_name()
62 # return ucfirst($this->transport()) . 'Handler';
67 # return strtolower($this->class_name().'.'.$this->get_id());
71 * Return transport keyword which identifies items this queue handler
72 * services; must be defined for all subclasses.
74 * Must be 8 characters or less to fit in the queue_item database.
75 * ex "email", "jabber", "sms", "irc", ...
85 * Here's the meat of your queue handler -- you're handed a Notice
86 * object, which you may do as you will with.
88 * If this function indicates failure, a warning will be logged
89 * and the item is placed back in the queue to be re-run.
91 * @param Notice $notice
92 * @return boolean true on success, false on failure
94 function handle_notice($notice)
100 * Setup and start of run loop for this queue handler as a daemon.
101 * Most of the heavy lifting is passed on to the QueueManager's service()
102 * method, which passes control back to our handle_notice() method for
103 * each notice that comes in on the queue.
105 * Most of the time this won't need to be overridden in a subclass.
107 * @return boolean true on success, false on failure
111 if (!$this->start()) {
112 $this->log(LOG_WARNING, 'failed to start');
116 $this->log(LOG_INFO, 'checking for queued notices');
118 $queue = $this->transport();
119 $timeout = $this->timeout();
121 $qm = QueueManager::get();
123 $qm->service($queue, $this);
125 $this->log(LOG_INFO, 'finished servicing the queue');
127 if (!$this->finish()) {
128 $this->log(LOG_WARNING, 'failed to clean up');
132 $this->log(LOG_INFO, 'terminating normally');
138 function log($level, $msg)
140 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);