]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
invite fancy url
[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->free();
91                                         unset($notice);
92                                         $notice = NULL;
93                                 } else {
94                                         $this->log(LOG_WARNING, 'queue item for notice that does not exist');
95                                 }
96                                 $qi->delete();
97                                 $qi->free();
98                                 unset($qi);
99                                 $this->idle(0);
100                         } else {
101                                 $this->clear_old_claims();
102                                 $this->idle(5);
103                         }       
104                 } while (true);
105                 if (!$this->finish()) {
106                         return false;
107                 }
108                 return true;
109         }
110
111         function idle($timeout=0) {
112                 if ($timeout>0) {
113                         sleep($timeout);
114                 }
115         }
116         
117         function clear_old_claims() {
118                 $qi = new Queue_item();
119                 $qi->transport = $this->transport();
120                 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
121                 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
122                 $qi->free();
123                 unset($qi);
124         }
125         
126         function log($level, $msg) {
127                 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
128         }
129 }
130