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