2021-12-03 03:01:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/** @file */
|
2016-05-20 04:22:04 +00:00
|
|
|
|
|
|
|
namespace Zotlabs\Daemon;
|
|
|
|
|
2018-06-05 01:40:11 +00:00
|
|
|
use Zotlabs\Lib\Libsync;
|
2022-01-25 01:26:12 +00:00
|
|
|
use Zotlabs\Lib\Channel;
|
2022-02-12 20:43:29 +00:00
|
|
|
use Zotlabs\Lib\Addon;
|
2018-06-01 04:05:09 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
class Cron
|
|
|
|
{
|
|
|
|
|
|
|
|
public static function run($argc, $argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
$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. Cron deferred to next scheduled run.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a lockfile. If it exists, but is over an hour old, it's stale. Ignore it.
|
|
|
|
$lockfile = 'cache/cron';
|
|
|
|
if (
|
|
|
|
(file_exists($lockfile)) && (filemtime($lockfile) > (time() - 3600))
|
|
|
|
&& (! get_config('system', 'override_cron_lockfile'))
|
|
|
|
) {
|
|
|
|
logger("cron: Already running");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
// Create a lockfile. Needs two vars, but $x doesn't need to contain anything.
|
|
|
|
file_put_contents($lockfile, $x);
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
logger('cron: start');
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
// run queue delivery process in the background
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
Run::Summon([ 'Queue' ]);
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
Run::Summon([ 'Poller' ]);
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
// maintenance for mod sharedwithme - check for updated items and remove them
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
require_once('include/sharedwithme.php');
|
|
|
|
apply_updates();
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
// expire any expired items
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
$r = q(
|
|
|
|
"select id,item_wall from item where expires > '2001-01-01 00:00:00' and expires < %s
|
2016-05-20 04:22:04 +00:00
|
|
|
and item_deleted = 0 ",
|
2021-12-03 03:01:39 +00:00
|
|
|
db_utcnow()
|
|
|
|
);
|
|
|
|
if ($r) {
|
|
|
|
require_once('include/items.php');
|
|
|
|
foreach ($r as $rr) {
|
|
|
|
drop_item($rr['id'], false, (($rr['item_wall']) ? DROPITEM_PHASE1 : DROPITEM_NORMAL));
|
|
|
|
if ($rr['item_wall']) {
|
|
|
|
// The notifier isn't normally invoked unless item_drop is interactive.
|
|
|
|
Run::Summon([ 'Notifier', 'drop', $rr['id'] ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// delete expired access tokens
|
|
|
|
|
|
|
|
$r = q(
|
|
|
|
"select atoken_id from atoken where atoken_expires > '%s' and atoken_expires < %s",
|
|
|
|
dbesc(NULL_DATE),
|
|
|
|
db_utcnow()
|
|
|
|
);
|
|
|
|
if ($r) {
|
|
|
|
require_once('include/security.php');
|
|
|
|
foreach ($r as $rr) {
|
|
|
|
atoken_delete($rr['atoken_id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that every channel pings their directory occasionally.
|
|
|
|
|
|
|
|
$r = q(
|
|
|
|
"select channel_id from channel where channel_dirdate < %s - INTERVAL %s and channel_removed = 0",
|
|
|
|
db_utcnow(),
|
|
|
|
db_quoteinterval('7 DAY')
|
|
|
|
);
|
|
|
|
if ($r) {
|
|
|
|
foreach ($r as $rr) {
|
|
|
|
Run::Summon([ 'Directory', $rr['channel_id'], 'force' ]);
|
|
|
|
if ($interval) {
|
|
|
|
@time_sleep_until(microtime(true) + (float) $interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish any applicable items that were set to be published in the future
|
|
|
|
// (time travel posts). Restrict to items that have come of age in the last
|
|
|
|
// couple of days to limit the query to something reasonable.
|
|
|
|
|
|
|
|
$r = q(
|
|
|
|
"select id from item where item_delayed = 1 and created <= %s and created > '%s' ",
|
|
|
|
db_utcnow(),
|
|
|
|
dbesc(datetime_convert('UTC', 'UTC', 'now - 2 days'))
|
|
|
|
);
|
|
|
|
if ($r) {
|
|
|
|
foreach ($r as $rr) {
|
|
|
|
$x = q(
|
|
|
|
"update item set item_delayed = 0 where id = %d",
|
|
|
|
intval($rr['id'])
|
|
|
|
);
|
|
|
|
if ($x) {
|
|
|
|
$z = q(
|
|
|
|
"select * from item where id = %d",
|
|
|
|
intval($message_id)
|
|
|
|
);
|
|
|
|
if ($z) {
|
|
|
|
xchan_query($z);
|
|
|
|
$sync_item = fetch_post_tags($z);
|
|
|
|
Libsync::build_sync_packet(
|
|
|
|
$sync_item[0]['uid'],
|
|
|
|
[
|
|
|
|
'item' => [ encode_item($sync_item[0], true) ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Run::Summon([ 'Notifier','wall-new',$rr['id'] ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once('include/attach.php');
|
|
|
|
attach_upgrade();
|
|
|
|
|
|
|
|
$abandon_days = intval(get_config('system', 'account_abandon_days'));
|
|
|
|
if ($abandon_days < 1) {
|
|
|
|
$abandon_days = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// once daily run birthday_updates and then expire in background
|
|
|
|
|
|
|
|
// FIXME: add birthday updates, both locally and for xprof for use
|
|
|
|
// by directory servers
|
|
|
|
|
|
|
|
$d1 = intval(get_config('system', 'last_expire_day'));
|
|
|
|
$d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
|
|
|
|
|
|
|
|
// Allow somebody to staggger daily activities if they have more than one site on their server,
|
|
|
|
// or if it happens at an inconvenient (busy) hour.
|
|
|
|
|
|
|
|
$h1 = intval(get_config('system', 'cron_hour'));
|
|
|
|
$h2 = intval(datetime_convert('UTC', 'UTC', 'now', 'G'));
|
|
|
|
|
|
|
|
|
|
|
|
if (($d2 != $d1) && ($h1 == $h2)) {
|
|
|
|
Run::Summon([ 'Cron_daily' ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = ''
|
2016-05-20 04:22:04 +00:00
|
|
|
and xchan_photo_date < %s - INTERVAL %s",
|
2021-12-03 03:01:39 +00:00
|
|
|
db_utcnow(),
|
|
|
|
db_quoteinterval('1 DAY')
|
|
|
|
);
|
|
|
|
if ($r) {
|
|
|
|
require_once('include/photo_factory.php');
|
|
|
|
foreach ($r as $rr) {
|
|
|
|
$photos = import_remote_xchan_photo($rr['xchan_photo_l'], $rr['xchan_hash']);
|
|
|
|
if ($photos) {
|
|
|
|
$x = q(
|
|
|
|
"update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'
|
2020-08-25 04:36:56 +00:00
|
|
|
where xchan_hash = '%s'",
|
2021-12-03 03:01:39 +00:00
|
|
|
dbesc($photos[0]),
|
|
|
|
dbesc($photos[1]),
|
|
|
|
dbesc($photos[2]),
|
|
|
|
dbesc($photos[3]),
|
|
|
|
dbesc($rr['xchan_hash'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
$generation = 0;
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
$restart = false;
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
if (($argc > 1) && ($argv[1] == 'restart')) {
|
|
|
|
$restart = true;
|
|
|
|
$generation = intval($argv[2]);
|
|
|
|
if (! $generation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2022-02-12 20:43:29 +00:00
|
|
|
Addon::reload_all();
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
$d = datetime_convert();
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
// TODO check to see if there are any cronhooks before wasting a process
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
if (! $restart) {
|
|
|
|
Run::Summon([ 'Cronhooks' ]);
|
|
|
|
}
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
set_config('system', 'lastcron', datetime_convert());
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
//All done - clear the lockfile
|
|
|
|
@unlink($lockfile);
|
2016-05-20 04:22:04 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-20 04:22:04 +00:00
|
|
|
}
|