]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
747e7b49303dd8bd5267615a9dcf48b334342c08
[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 class QueueHandler {
23
24         var $_id = 'generic';
25
26         function QueueHandler($id=NULL) {
27                 if ($id) {
28                         $this->set_id($id);
29                 }
30         }
31         
32         function class_name() {
33                 return ucfirst($this->transport()) . 'Handler';
34         }
35         
36         function get_id() {
37                 return $this->_id;
38         }
39
40         function set_id($id) {
41                 $this->_id = $id;
42         }
43         
44         function transport() {
45                 return NULL;
46         }
47         
48         function start() {
49         }
50         
51         function finish() {
52         }
53
54         function handle_notice($notice) {
55                 return true;
56         }
57         
58         function handle_queue() {
59                 $this->log(LOG_INFO, 'checking for queued notices');
60                 $cnt = 0;
61                 $transport = $this->transport();
62                 do {
63                         $qi = Queue_item::top($transport);
64                         if ($qi) {
65                                 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
66                                 $notice = Notice::staticGet($qi->notice_id);
67                                 if ($notice) {
68                                         $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
69                                         # XXX: what to do if broadcast fails?
70                                         $result = $this->handle_notice($notice);
71                                         if (!$result) {
72                                                 $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
73                                                 $orig = $qi;
74                                                 $qi->claimed = NULL;
75                                                 $qi->update($orig);
76                                                 $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
77                                                 continue;
78                                         }
79                                         $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
80                                         $notice = NULL;
81                                 } else {
82                                         $this->log(LOG_WARNING, 'queue item for notice that does not exist');
83                                 }
84                                 $qi->delete();
85                                 $cnt++;
86                         } else {
87                                 $this->clear_old_claims();
88                                 sleep(10);
89                         }       
90                 } while (true);
91         }
92
93         function clear_old_claims() {
94                 $qi = new Queue_item();
95                 $qi->transport = $this->transport();
96                 $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
97                 $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
98         }
99         
100         function log($level, $msg) {
101                 common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
102         }
103 }
104