]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
add some processTime() to the send loops to avoid filling the buffer
[quix0rs-gnu-social.git] / lib / queuehandler.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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
26 class QueueHandler extends Daemon {
27
28         var $_id = 'generic';
29
30         function QueueHandler($id=NULL) {
31                 if ($id) {
32                         $this->set_id($id);
33                 }
34         }
35         
36         function class_name() {
37                 return ucfirst($this->transport()) . 'Handler';
38         }
39
40         function name() {
41                 return strtolower($this->class_name().'.'.$this->get_id());
42         }
43         
44         function get_id() {
45                 return $this->_id;
46         }
47
48         function set_id($id) {
49                 $this->_id = $id;
50         }
51         
52         function transport() {
53                 return NULL;
54         }
55         
56         function start() {
57         }
58         
59         function finish() {
60         }
61
62         function handle_notice($notice) {
63                 return true;
64         }
65         
66         function run() {
67                 if (!$this->start()) {
68                         return false;
69                 }
70                 $this->log(LOG_INFO, 'checking for queued notices');
71                 $transport = $this->transport();
72                 do {
73                         $qi = Queue_item::top($transport);
74                         if ($qi) {
75                                 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
76                                 $notice = Notice::staticGet($qi->notice_id);
77                                 if ($notice) {
78                                         $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
79                                         # XXX: what to do if broadcast fails?
80                                         $result = $this->handle_notice($notice);
81                                         if (!$result) {
82                                                 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
83                                                 $orig = $qi;
84                                                 $qi->claimed = NULL;
85                                                 $qi->update($orig);
86                                                 $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
87                                                 continue;
88                                         }
89                                         $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
90                                         $notice = NULL;
91                                 } else {
92                                         $this->log(LOG_WARNING, 'queue item for notice that does not exist');
93                                 }
94                                 $qi->delete();
95                                 $this->idle(0);
96                         } else {
97                                 $this->clear_old_claims();
98                                 $this->idle(5);
99                         }       
100                 } while (true);
101                 if (!$this->finish()) {
102                         return false;
103                 }
104                 return true;
105         }
106
107         function idle($timeout=0) {
108                 if ($timeout>0) {
109                         sleep($timeout);
110                 }
111         }
112         
113         function clear_old_claims() {
114                 $qi = new Queue_item();
115                 $qi->transport = $this->transport();
116                 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
117                 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
118         }
119         
120         function log($level, $msg) {
121                 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
122         }
123 }
124