]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
613be6e33085ae0b01dda9f95626ed849bb3c7af
[quix0rs-gnu-social.git] / lib / queuehandler.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 /**
23  * Base class for queue handlers.
24  *
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.
28  *
29  * Subclasses must override at least the following methods:
30  * - transport
31  * - handle_notice
32  */
33 #class QueueHandler extends Daemon
34 class QueueHandler
35 {
36
37 #    function __construct($id=null, $daemonize=true)
38 #    {
39 #        parent::__construct($daemonize);
40 #
41 #        if ($id) {
42 #            $this->set_id($id);
43 #        }
44 #    }
45
46     /**
47      * How many seconds a polling-based queue manager should wait between
48      * checks for new items to handle.
49      *
50      * Defaults to 60 seconds; override to speed up or slow down.
51      *
52      * @fixme not really compatible with global queue manager
53      * @return int timeout in seconds
54      */
55 #    function timeout()
56 #    {
57 #        return 60;
58 #    }
59
60 #    function class_name()
61 #    {
62 #        return ucfirst($this->transport()) . 'Handler';
63 #    }
64
65 #    function name()
66 #    {
67 #        return strtolower($this->class_name().'.'.$this->get_id());
68 #    }
69
70     /**
71      * Return transport keyword which identifies items this queue handler
72      * services; must be defined for all subclasses.
73      *
74      * Must be 8 characters or less to fit in the queue_item database.
75      * ex "email", "jabber", "sms", "irc", ...
76      *
77      * @return string
78      */
79     function transport()
80     {
81         return null;
82     }
83
84     /**
85      * Here's the meat of your queue handler -- you're handed a Notice
86      * object, which you may do as you will with.
87      *
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.
90      *
91      * @param Notice $notice
92      * @return boolean true on success, false on failure
93      */
94     function handle_notice($notice)
95     {
96         return true;
97     }
98
99     /**
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.
104      *
105      * Most of the time this won't need to be overridden in a subclass.
106      *
107      * @return boolean true on success, false on failure
108      */
109     function run()
110     {
111         if (!$this->start()) {
112             $this->log(LOG_WARNING, 'failed to start');
113             return false;
114         }
115
116         $this->log(LOG_INFO, 'checking for queued notices');
117
118         $queue   = $this->transport();
119         $timeout = $this->timeout();
120
121         $qm = QueueManager::get();
122
123         $qm->service($queue, $this);
124
125         $this->log(LOG_INFO, 'finished servicing the queue');
126
127         if (!$this->finish()) {
128             $this->log(LOG_WARNING, 'failed to clean up');
129             return false;
130         }
131
132         $this->log(LOG_INFO, 'terminating normally');
133
134         return true;
135     }
136
137
138     function log($level, $msg)
139     {
140         common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
141     }
142 }
143