]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuehandler.php
move opening brace of class declaration to next line
[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
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 run()
79     {
80         if (!$this->start()) {
81             return false;
82         }
83         $this->log(LOG_INFO, 'checking for queued notices');
84         $transport = $this->transport();
85         do {
86             $qi = Queue_item::top($transport);
87             if ($qi) {
88                 $this->log(LOG_INFO, 'Got item enqueued '.common_exact_date($qi->created));
89                 $notice = Notice::staticGet($qi->notice_id);
90                 if ($notice) {
91                     $this->log(LOG_INFO, 'broadcasting notice ID = ' . $notice->id);
92                     # XXX: what to do if broadcast fails?
93                     $result = $this->handle_notice($notice);
94                     if (!$result) {
95                         $this->log(LOG_WARNING, 'Failed broadcast for notice ID = ' . $notice->id);
96                         $orig = $qi;
97                         $qi->claimed = null;
98                         $qi->update($orig);
99                         $this->log(LOG_WARNING, 'Abandoned claim for notice ID = ' . $notice->id);
100                         continue;
101                     }
102                     $this->log(LOG_INFO, 'finished broadcasting notice ID = ' . $notice->id);
103                     $notice->free();
104                     unset($notice);
105                     $notice = null;
106                 } else {
107                     $this->log(LOG_WARNING, 'queue item for notice that does not exist');
108                 }
109                 $qi->delete();
110                 $qi->free();
111                 unset($qi);
112                 $this->idle(0);
113             } else {
114                 $this->clear_old_claims();
115                 $this->idle(5);
116             }    
117         } while (true);
118         if (!$this->finish()) {
119             return false;
120         }
121         return true;
122     }
123
124     function idle($timeout=0)
125     {
126         if ($timeout>0) {
127             sleep($timeout);
128         }
129     }
130     
131     function clear_old_claims()
132     {
133         $qi = new Queue_item();
134         $qi->transport = $this->transport();
135         $qi->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
136         $qi->update(DB_DATAOBJECT_WHEREADD_ONLY);
137         $qi->free();
138         unset($qi);
139     }
140     
141     function log($level, $msg)
142     {
143         common_log($level, $this->class_name() . ' ('. $this->get_id() .'): '.$msg);
144     }
145 }
146