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