]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
Merge branch 'invite-enabled' of git://gitorious.org/~jeff-themovie/laconica/jeff...
[quix0rs-gnu-social.git] / lib / queuehandler.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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 define('CLAIM_TIMEOUT', 1200);
21
22 if (!defined('LACONICA')) { exit(1); }
23
24 require_once(INSTALLDIR.'/lib/daemon.php');
25 require_once(INSTALLDIR.'/classes/Queue_item.php');
26 require_once(INSTALLDIR.'/classes/Notice.php');
27
28 class QueueHandler extends Daemon
29 {
30
31     var $_id = 'generic';
32
33     function QueueHandler($id=null)
34     {
35         if ($id) {
36             $this->set_id($id);
37         }
38     }
39
40     function class_name()
41     {
42         return ucfirst($this->transport()) . 'Handler';
43     }
44
45     function name()
46     {
47         return strtolower($this->class_name().'.'.$this->get_id());
48     }
49
50     function get_id()
51     {
52         return $this->_id;
53     }
54
55     function set_id($id)
56     {
57         $this->_id = $id;
58     }
59
60     function transport()
61     {
62         return null;
63     }
64
65     function start()
66     {
67     }
68
69     function finish()
70     {
71     }
72
73     function handle_notice($notice)
74     {
75         return true;
76     }
77
78     function db_dispatch() {
79         do {
80             $qi = Queue_item::top($this->transport());
81             if ($qi) {
82                 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
83                 $notice = Notice::staticGet($qi->notice_id);
84                 if ($notice) {
85                     $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
86                     # XXX: what to do if broadcast fails?
87                     $result = $this->handle_notice($notice);
88                     if (!$result) {
89                         $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
90                         $orig = $qi;
91                         $qi->claimed = null;
92                         $qi->update($orig);
93                         $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
94                         continue;
95                     }
96                     $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
97                     $notice->free();
98                     unset($notice);
99                     $notice = null;
100                 } else {
101                     $this->log(LOG_WARNING, 'queue item for notice that does not exist');
102                 }
103                 $qi->delete();
104                 $qi->free();
105                 unset($qi);
106                 $this->idle(0);
107             } else {
108                 $this->clear_old_claims();
109                 $this->idle(5);
110             }
111         } while (true);
112     }
113
114     function stomp_dispatch() {
115
116         // use an external message queue system via STOMP
117         require_once("Stomp.php");
118
119         $server = common_config('queue','stomp_server');
120         $username = common_config('queue', 'stomp_username');
121         $password = common_config('queue', 'stomp_password');
122
123         $con = new Stomp($server);
124
125         if (!$con->connect($username, $password)) {
126             $this->log(LOG_ERR, 'Failed to connect to queue server');
127             return false;
128         }
129
130         $queue_basename = common_config('queue','queue_basename');
131         // subscribe to the relevant queue (format: basename-transport)
132         $con->subscribe('/queue/'.$queue_basename.'-'.$this->transport());
133
134         do {
135             $frame = $con->readFrame();
136             if ($frame) {
137                 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($frame->headers['created']));
138
139                 // XXX: Now the queue handler receives only the ID of the
140                 // notice, and it has to get it from the DB
141                 // A massive improvement would be avoid DB query by transmitting
142                 // all the notice details via queue server...
143                 $notice = Notice::staticGet($frame->body);
144
145                 if ($notice) {
146                     $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
147                     $result = $this->handle_notice($notice);
148                     if ($result) {
149                         // if the msg has been handled positively, ack it
150                         // and the queue server will remove it from the queue
151                         $con->ack($frame);
152                         $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
153                     }
154                     else {
155                         // no ack
156                         $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
157                     }
158                     $notice->free();
159                     unset($notice);
160                     $notice = null;
161                 } else {
162                     $this->log(LOG_WARNING, 'queue item for notice that does not exist');
163                 }
164             }
165         } while (true);
166
167         $con->disconnect();
168     }
169
170     function run()
171     {
172         if (!$this->start()) {
173             return false;
174         }
175         $this->log(LOG_INFO, 'checking for queued notices');
176         if (common_config('queue','subsystem') == 'stomp') {
177             $this->stomp_dispatch();
178         }
179         else {
180             $this->db_dispatch();
181         }
182         if (!$this->finish()) {
183             return false;
184         }
185         return true;
186     }
187
188     function idle($timeout=0)
189     {
190         if ($timeout>0) {
191             sleep($timeout);
192         }
193     }
194
195     function clear_old_claims()
196     {
197         $qi = new Queue_item();
198         $qi->transport = $this->transport();
199         $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
200         $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
201         $qi->free();
202         unset($qi);
203     }
204
205     function log($level, $msg)
206     {
207         common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
208     }
209 }
210