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