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); }
22 require_once(INSTALLDIR.'/lib/daemon.php');
23 require_once(INSTALLDIR.'/classes/Queue_item.php');
24 require_once(INSTALLDIR.'/classes/Notice.php');
26 define('CLAIM_TIMEOUT', 1200);
27 define('QUEUE_HANDLER_MISS_IDLE', 10);
28 define('QUEUE_HANDLER_HIT_IDLE', 0);
31 * Base class for queue handlers.
33 * As extensions of the Daemon class, each queue handler has the ability
34 * to launch itself in the background, at which point it'll pass control
35 * to the configured QueueManager class to poll for updates.
37 * Subclasses must override at least the following methods:
43 * Some subclasses will also want to override the idle handler:
46 class QueueHandler extends Daemon
49 function __construct($id=null, $daemonize=true)
51 parent::__construct($daemonize);
59 * How many seconds a polling-based queue manager should wait between
60 * checks for new items to handle.
62 * Defaults to 60 seconds; override to speed up or slow down.
64 * @return int timeout in seconds
73 return ucfirst($this->transport()) . 'Handler';
78 return strtolower($this->class_name().'.'.$this->get_id());
82 * Return transport keyword which identifies items this queue handler
83 * services; must be defined for all subclasses.
85 * Must be 8 characters or less to fit in the queue_item database.
86 * ex "email", "jabber", "sms", "irc", ...
96 * Initialization, run when the queue handler starts.
97 * If this function indicates failure, the handler run will be aborted.
99 * @fixme run() will abort if this doesn't return true,
100 * but some subclasses don't bother.
101 * @return boolean true on success, false on failure
108 * Cleanup, run when the queue handler ends.
109 * If this function indicates failure, a warning will be logged.
111 * @fixme run() will throw warnings if this doesn't return true,
112 * but many subclasses don't bother.
113 * @return boolean true on success, false on failure
120 * Here's the meat of your queue handler -- you're handed a Notice
121 * object, which you may do as you will with.
123 * If this function indicates failure, a warning will be logged
124 * and the item is placed back in the queue to be re-run.
126 * @param Notice $notice
127 * @return boolean true on success, false on failure
129 function handle_notice($notice)
135 * Setup and start of run loop for this queue handler as a daemon.
136 * Most of the heavy lifting is passed on to the QueueManager's service()
137 * method, which passes control back to our handle_notice() method for
138 * each notice that comes in on the queue.
140 * Most of the time this won't need to be overridden in a subclass.
142 * @return boolean true on success, false on failure
146 if (!$this->start()) {
147 $this->log(LOG_WARNING, 'failed to start');
151 $this->log(LOG_INFO, 'checking for queued notices');
153 $queue = $this->transport();
154 $timeout = $this->timeout();
156 $qm = QueueManager::get();
158 $qm->service($queue, $this);
160 $this->log(LOG_INFO, 'finished servicing the queue');
162 if (!$this->finish()) {
163 $this->log(LOG_WARNING, 'failed to clean up');
167 $this->log(LOG_INFO, 'terminating normally');
173 * Called by QueueHandler after each handled item or empty polling cycle.
174 * This is a good time to e.g. service your XMPP connection.
176 * Doesn't need to be overridden if there's no maintenance to do.
178 * @param int $timeout seconds to sleep if there's nothing to do
180 function idle($timeout=0)
187 function log($level, $msg)
189 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
192 function getSockets()