streams/include/poller.php

244 lines
6.4 KiB
PHP
Raw Normal View History

2013-02-26 01:09:40 +00:00
<?php /** @file */
2011-04-16 06:40:43 +00:00
2012-12-31 00:14:29 +00:00
require_once('boot.php');
require_once('include/cli_startup.php');
function poller_run($argv, $argc){
2010-08-17 03:47:40 +00:00
2012-12-31 00:14:29 +00:00
cli_startup();
2011-04-16 06:40:43 +00:00
2012-12-31 00:14:29 +00:00
$a = get_app();
$maxsysload = intval(get_config('system','maxloadavg'));
if($maxsysload < 1)
$maxsysload = 50;
if(function_exists('sys_getloadavg')) {
$load = sys_getloadavg();
if(intval($load[0]) > $maxsysload) {
logger('system: load ' . $load . ' too high. Poller deferred to next scheduled run.');
return;
}
}
2010-11-26 02:31:33 +00:00
logger('poller: start');
2010-11-26 02:22:54 +00:00
// run queue delivery process in the background
2010-11-22 23:30:52 +00:00
2011-02-23 23:16:12 +00:00
proc_run('php',"include/queue.php");
2011-01-31 16:16:35 +00:00
2011-09-26 12:43:22 +00:00
// expire any expired accounts
q("UPDATE account
2013-03-26 04:32:12 +00:00
SET account_flags = (account_flags | %d)
where not (account_flags & %d)
and account_expires != '0000-00-00 00:00:00'
and account_expires < UTC_TIMESTAMP() ",
intval(ACCOUNT_EXPIRED),
intval(ACCOUNT_EXPIRED)
);
2011-09-26 12:43:22 +00:00
2013-05-24 00:24:15 +00:00
// publish any applicable items that were set to be published in the future
// (time travel posts)
$r = q("select id from item where ( item_restrict & %d ) and created <= UTC_TIMESTAMP() ",
2013-05-24 00:24:15 +00:00
intval(ITEM_DELAYED_PUBLISH)
);
if($r) {
foreach($r as $rr) {
$x = q("update item set item_restrict = ( item_restrict ^ %d ) where id = %d limit 1",
intval(ITEM_DELAYED_PUBLISH),
intval($rr['id'])
);
if($x) {
proc_run('php','include/notifer.php','wall-new',$rr['id']);
}
}
}
2013-05-24 00:24:15 +00:00
2011-09-30 04:20:19 +00:00
$abandon_days = intval(get_config('system','account_abandon_days'));
if($abandon_days < 1)
$abandon_days = 0;
2011-10-14 07:20:37 +00:00
// once daily run birthday_updates and then expire in background
2011-03-16 00:31:49 +00:00
// FIXME: add birthday updates, both locally and for xprof for use
// by directory servers
2011-03-16 00:31:49 +00:00
$d1 = get_config('system','last_expire_day');
$d2 = intval(datetime_convert('UTC','UTC','now','d'));
if($d2 != intval($d1)) {
2011-10-14 07:20:37 +00:00
// If this is a directory server, request a sync with an upstream
// directory at least once a day, up to once every poll interval.
// Pull remote changes and push local changes.
// potential issue: how do we keep from creating an endless update loop?
$dirmode = get_config('system','directory_mode');
if($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) {
require_once('include/dir_fns.php');
sync_directories($dirmode);
}
2011-03-16 00:31:49 +00:00
set_config('system','last_expire_day',$d2);
// Uncomment when expire protocol component is working
// proc_run('php','include/expire.php');
proc_run('php','include/cli_suggest.php');
2011-03-16 00:31:49 +00:00
}
2013-06-04 05:34:35 +00:00
// update any photos which didn't get imported properly
// This should be rare
$r = q("select xchan_photo_l, xchan_hash from xchan where xchan_photo_l != '' and xchan_photo_m = ''
and xchan_photo_date < UTC_TIMESTAMP() - INTERVAL 1 DAY");
2013-06-04 05:34:35 +00:00
if($r) {
require_once('include/photo/photo_driver.php');
foreach($r as $rr) {
$photos = import_profile_photo($rr['xchan_photo_l'],$rr['xchan_hash']);
$x = q("update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'
where xchan_hash = '%s' limit 1",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
dbesc($photos[3]),
dbesc($rr['xchan_hash'])
);
}
}
$manual_id = 0;
$generation = 0;
2012-08-31 01:47:07 +00:00
$force = false;
$restart = false;
2013-08-16 00:36:00 +00:00
if(($argc > 1) && ($argv[1] == 'force'))
2010-11-05 03:47:44 +00:00
$force = true;
2013-08-16 00:36:00 +00:00
if(($argc > 1) && ($argv[1] == 'restart')) {
$restart = true;
2013-08-16 00:36:00 +00:00
$generation = intval($argv[2]);
if(! $generation)
killme();
}
2013-08-16 00:36:00 +00:00
if(($argc > 1) && intval($argv[1])) {
$manual_id = intval($argv[1]);
$force = true;
2011-01-24 04:09:34 +00:00
}
$interval = intval(get_config('system','poll_interval'));
if(! $interval)
$interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
2012-12-31 00:14:29 +00:00
$sql_extra = (($manual_id) ? " AND abook_id = $manual_id " : "");
2011-01-24 04:09:34 +00:00
2011-04-26 11:39:27 +00:00
reload_plugins();
2011-03-09 10:12:32 +00:00
$d = datetime_convert();
2011-04-26 11:39:27 +00:00
2012-12-31 00:14:29 +00:00
//TODO check to see if there are any cronhooks before wasting a process
if(! $restart)
proc_run('php','include/cronhooks.php');
2011-03-09 10:12:32 +00:00
2011-08-24 01:17:35 +00:00
// Only poll from those with suitable relationships,
// and which have a polling address and ignore Diaspora since
// we are unable to match those posts with a Diaspora GUID and prevent duplicates.
2011-09-30 04:20:19 +00:00
$abandon_sql = (($abandon_days)
2012-12-31 00:14:29 +00:00
? sprintf(" AND account_lastlog > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
2011-09-30 04:20:19 +00:00
: ''
);
2013-08-16 00:36:00 +00:00
$contacts = q("SELECT abook_id, abook_updated, abook_connected, abook_closeness, abook_channel
2012-12-31 07:51:39 +00:00
FROM abook LEFT JOIN account on abook_account = account_id where 1
2011-01-24 04:09:34 +00:00
$sql_extra
2012-12-31 07:51:39 +00:00
AND (( abook_flags = %d ) OR ( abook_flags = %d ))
AND (( account_flags = %d ) OR ( account_flags = %d )) $abandon_sql ORDER BY RAND()",
intval(ABOOK_FLAG_HIDDEN),
intval(0),
intval(ACCOUNT_OK),
intval(ACCOUNT_UNVERIFIED) // FIXME
2012-12-31 00:14:29 +00:00
);
2010-07-19 03:49:54 +00:00
2012-12-31 00:14:29 +00:00
if(! $contacts) {
return;
}
2010-07-19 03:49:54 +00:00
2012-12-31 00:14:29 +00:00
foreach($contacts as $contact) {
$update = false;
$t = $contact['abook_updated'];
2013-01-30 03:28:19 +00:00
$c = $contact['abook_connected'];
2012-12-31 00:14:29 +00:00
2013-01-30 03:28:19 +00:00
if($c == $t) {
if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
$update = true;
}
else {
// if we've never connected with them, start the mark for death countdown from now
2013-08-16 00:36:00 +00:00
if($c == '0000-00-00 00:00:00') {
2013-01-30 03:28:19 +00:00
$r = q("update abook set abook_connected = '%s' where abook_id = %d limit 1",
dbesc(datetime_convert()),
2013-08-16 00:36:00 +00:00
intval($contact['abook_id'])
2013-01-30 03:28:19 +00:00
);
$c = datetime_convert();
$update = true;
}
// He's dead, Jim
2013-08-16 00:36:00 +00:00
if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 30 day")) > 0) {
$r = q("update abook set abook_flags = (abook_flags | %d) where abook_id = %d limit 1",
2013-01-30 03:28:19 +00:00
intval(ABOOK_FLAG_ARCHIVED),
intval($contact['abook_id'])
);
$update = false;
continue;
}
// might be dead, so maybe don't poll quite so often
// recently deceased, so keep up the regular schedule for 3 days
2013-08-16 00:36:00 +00:00
if((strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 3 day")) > 0)
&& (strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 1 day")) > 0))
2013-01-30 03:28:19 +00:00
$update = true;
// After that back off and put them on a morphine drip
2013-08-16 00:36:00 +00:00
if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 2 day")) > 0) {
2013-01-30 03:28:19 +00:00
$update = true;
}
}
2012-12-31 00:14:29 +00:00
if((! $update) && (! $force))
continue;
proc_run('php','include/onepoll.php',$contact['abook_id']);
if($interval)
@time_sleep_until(microtime(true) + (float) $interval);
}
2011-04-16 15:45:08 +00:00
return;
}
2010-07-19 03:49:54 +00:00
if (array_search(__file__,get_included_files())===0){
poller_run($argv,$argc);
killme();
}