]> git.mxchange.org Git - friendica.git/blob - include/poller.php
break up poller into separate processes
[friendica.git] / include / poller.php
1 <?php
2
3 require_once("boot.php");
4
5
6 function poller_run($argv, $argc){
7         global $a, $db;
8
9         if(is_null($a)) {
10                 $a = new App;
11         }
12   
13         if(is_null($db)) {
14             @include(".htconfig.php");
15         require_once("dba.php");
16             $db = new dba($db_host, $db_user, $db_pass, $db_data);
17         unset($db_host, $db_user, $db_pass, $db_data);
18         };
19
20
21         require_once('include/session.php');
22         require_once('include/datetime.php');
23         require_once('library/simplepie/simplepie.inc');
24         require_once('include/items.php');
25         require_once('include/Contact.php');
26         require_once('include/email.php');
27         require_once('include/socgraph.php');
28         require_once('include/pidfile.php');
29
30         load_config('config');
31         load_config('system');
32
33         $lockpath = get_config('system','lockpath');
34         if ($lockpath != '') {
35                 $pidfile = new pidfile($lockpath, 'poller.lck');
36                 if($pidfile->is_already_running()) {
37                         logger("poller: Already running");
38                         exit;
39                 }
40         }
41
42         $a->set_baseurl(get_config('system','url'));
43
44         load_hooks();
45
46         logger('poller: start');
47         
48         // run queue delivery process in the background
49
50         proc_run('php',"include/queue.php");
51         
52         // expire any expired accounts
53
54         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0 
55                 AND `account_expires_on` != '0000-00-00 00:00:00' 
56                 AND `account_expires_on` < UTC_TIMESTAMP() ");
57   
58         $abandon_days = intval(get_config('system','account_abandon_days'));
59         if($abandon_days < 1)
60                 $abandon_days = 0;
61
62         
63
64         // once daily run birthday_updates and then expire in background
65
66         $d1 = get_config('system','last_expire_day');
67         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
68
69         if($d2 != intval($d1)) {
70
71                 update_contact_birthdays();
72
73                 update_suggestions();
74
75                 set_config('system','last_expire_day',$d2);
76                 proc_run('php','include/expire.php');
77         }
78
79         // clear old cache
80         Cache::clear();
81
82         // clear item cache files if they are older than one day
83         $cache = get_config('system','itemcache');
84         if (($cache != '') and is_dir($cache)) {
85                 if ($dh = opendir($cache)) {
86                         while (($file = readdir($dh)) !== false) {
87                                 $fullpath = $cache."/".$file;
88                                 if ((filetype($fullpath) == "file") and filectime($fullpath) < (time() - 86400))
89                                         unlink($fullpath);
90                         }
91                         closedir($dh);
92                 }
93         }
94
95         $manual_id  = 0;
96         $generation = 0;
97         $hub_update = false;
98         $force      = false;
99         $restart    = false;
100
101         if(($argc > 1) && ($argv[1] == 'force'))
102                 $force = true;
103
104         if(($argc > 1) && ($argv[1] == 'restart')) {
105                 $restart = true;
106                 $generation = intval($argv[2]);
107                 if(! $generation)
108                         killme();               
109         }
110
111         if(($argc > 1) && intval($argv[1])) {
112                 $manual_id = intval($argv[1]);
113                 $force     = true;
114         }
115
116         $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
117
118         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
119
120         reload_plugins();
121
122         $d = datetime_convert();
123
124         if(! $restart)
125                 proc_run('php','include/cronhooks.php');
126
127         // Only poll from those with suitable relationships,
128         // and which have a polling address and ignore Diaspora since 
129         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
130
131         $abandon_sql = (($abandon_days) 
132                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days)) 
133                 : '' 
134         );
135
136         $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
137                 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
138                 AND NOT `network` IN ( '%s', '%s' )
139                 $sql_extra 
140                 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 
141                 AND `contact`.`archive` = 0 
142                 AND `user`.`account_expired` = 0 $abandon_sql ORDER BY RAND()",
143                 intval(CONTACT_IS_SHARING),
144                 intval(CONTACT_IS_FRIEND),
145                 dbesc(NETWORK_DIASPORA),
146                 dbesc(NETWORK_FACEBOOK)
147         );
148
149         if(! count($contacts)) {
150                 return;
151         }
152
153         foreach($contacts as $c) {
154
155                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
156                         intval($c['id'])
157                 );
158
159                 if((! $res) || (! count($res)))
160                         continue;
161
162                 foreach($res as $contact) {
163
164                         $xml = false;
165
166                         if($manual_id)
167                                 $contact['last-update'] = '0000-00-00 00:00:00';
168
169                         if($contact['network'] === NETWORK_DFRN)
170                                 $contact['priority'] = 2;
171
172                         if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
173                                 $contact['priority'] = 2;
174
175                         if($contact['priority'] || $contact['subhub']) {
176
177                                 $hub_update = true;
178                                 $update     = false;
179
180                                 $t = $contact['last-update'];
181
182                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
183                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
184                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
185                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'. 
186
187
188                                 if($contact['subhub']) {
189                                         $interval = get_config('system','pushpoll_frequency');
190                                         $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
191                                         $hub_update = false;
192         
193                                         if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
194                                                         $hub_update = true;
195                                 }
196                                 else
197                                         $hub_update = false;
198
199                                 /**
200                                  * Based on $contact['priority'], should we poll this site now? Or later?
201                                  */                     
202
203                                 switch ($contact['priority']) {
204                                         case 5:
205                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
206                                                         $update = true;
207                                                 break;                                  
208                                         case 4:
209                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
210                                                         $update = true;
211                                                 break;
212                                         case 3:
213                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
214                                                         $update = true;
215                                                 break;
216                                         case 2:
217                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
218                                                         $update = true;
219                                                 break;
220                                         case 1:
221                                         default:
222                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
223                                                         $update = true;
224                                                 break;
225                                 }
226                                 if((! $update) && (! $force))
227                                         continue;
228                         }
229
230                         proc_run('php','include/onepoll.php',$contact['id']);
231                         if($interval)
232                                 @time_sleep_until(microtime(true) + (float) $interval);
233                 }
234         }
235
236         return;
237 }
238
239 if (array_search(__file__,get_included_files())===0){
240   poller_run($argv,$argc);
241   killme();
242 }